all repos — Legends-RPG @ ccacb83f93507b106dde26ae904c4ac538998086

A fantasy mini-RPG built with Python and Pygame.

data/states/shop.py (view raw)

  1"""
  2This class is the parent class of all shop states.
  3This includes weapon, armour, magic and potion shops.
  4It also includes the inn.  These states are scaled
  5twice as big as a level state. The self.gui controls
  6all the textboxes.
  7"""
  8
  9import copy
 10import pygame as pg
 11from .. import tools, setup, shopgui
 12from .. import constants as c
 13
 14
 15class Shop(tools._State):
 16    """Basic shop state"""
 17    def __init__(self):
 18        super(Shop, self).__init__()
 19        self.key = None
 20        self.sell_items = None
 21        self.music = setup.MUSIC['shop_theme']
 22        self.volume = 0.4
 23
 24    def startup(self, current_time, game_data):
 25        """Startup state"""
 26        self.game_data = game_data
 27        self.current_time = current_time
 28        self.state_dict = self.make_state_dict()
 29        self.state = 'transition in'
 30        self.next = c.TOWN
 31        self.get_image = tools.get_image
 32        self.dialogue = self.make_dialogue()
 33        self.accept_dialogue = self.make_accept_dialogue()
 34        self.accept_sale_dialogue = self.make_accept_sale_dialogue()
 35        self.items = self.make_purchasable_items()
 36        self.background = self.make_background()
 37        self.gui = shopgui.Gui(self)
 38        self.transition_rect = setup.SCREEN.get_rect()
 39        self.transition_alpha = 255
 40
 41    def make_state_dict(self):
 42        """
 43        Make a dictionary for all state methods.
 44        """
 45        state_dict = {'normal': self.normal_update,
 46                      'transition in': self.transition_in,
 47                      'transition out': self.transition_out}
 48
 49        return state_dict
 50
 51    def make_dialogue(self):
 52        """
 53        Make the list of dialogue phrases.
 54        """
 55        raise NotImplementedError
 56
 57    def make_accept_dialogue(self):
 58        """
 59        Make the dialogue for when the player buys an item.
 60        """
 61        return ['Item purchased.']
 62
 63    def make_accept_sale_dialogue(self):
 64        """
 65        Make the dialogue for when the player sells an item.
 66        """
 67        return ['Item sold.']
 68
 69    def make_purchasable_items(self):
 70        """
 71        Make the list of items to be bought at shop.
 72        """
 73        raise NotImplementedError
 74
 75    def make_background(self):
 76        """
 77        Make the level surface.
 78        """
 79        background = pg.sprite.Sprite()
 80        surface = pg.Surface(c.SCREEN_SIZE).convert()
 81        surface.fill(c.BLACK_BLUE)
 82        background.image = surface
 83        background.rect = background.image.get_rect()
 84
 85        player = self.make_sprite('player', 96, 32, 150)
 86        shop_owner = self.make_sprite(self.key, 32, 32, 600)
 87        counter = self.make_counter()
 88
 89        background.image.blit(player.image, player.rect)
 90        background.image.blit(shop_owner.image, shop_owner.rect)
 91        background.image.blit(counter.image, counter.rect)
 92
 93        return background
 94
 95    def make_sprite(self, key, coordx, coordy, x, y=304):
 96        """
 97        Get the image for the player.
 98        """
 99        spritesheet = setup.GFX[key]
100        surface = pg.Surface((32, 32))
101        surface.set_colorkey(c.BLACK)
102        image = self.get_image(coordx, coordy, 32, 32, spritesheet)
103        rect = image.get_rect()
104        surface.blit(image, rect)
105
106        surface = pg.transform.scale(surface, (96, 96))
107        rect = surface.get_rect(left=x, centery=y)
108        sprite = pg.sprite.Sprite()
109        sprite.image = surface
110        sprite.rect = rect
111
112        return sprite
113
114    def make_counter(self):
115        """
116        Make the counter to conduct business.
117        """
118        sprite_sheet = copy.copy(setup.GFX['house'])
119        sprite = pg.sprite.Sprite()
120        sprite.image = self.get_image(102, 64, 26, 82, sprite_sheet)
121        sprite.image = pg.transform.scale2x(sprite.image)
122        sprite.rect = sprite.image.get_rect(left=550, top=225)
123
124        return sprite
125
126    def update(self, surface, keys, current_time):
127        """
128        Update scene.
129        """
130        state_function = self.state_dict[self.state]
131        state_function(surface, keys, current_time)
132
133    def normal_update(self, surface, keys, current_time):
134        """
135        Update level normally.
136        """
137        self.gui.update(keys, current_time)
138        self.draw_level(surface)
139
140    def transition_in(self, surface, *args):
141        """
142        Transition into level.
143        """
144        transition_image = pg.Surface(self.transition_rect.size)
145        transition_image.fill(c.TRANSITION_COLOR)
146        transition_image.set_alpha(self.transition_alpha)
147        self.draw_level(surface)
148        surface.blit(transition_image, self.transition_rect)
149        self.transition_alpha -= c.TRANSITION_SPEED 
150        if self.transition_alpha <= 0:
151            self.state = 'normal'
152            self.transition_alpha = 0
153
154    def transition_out(self, surface, *args):
155        """
156        Transition level to new scene.
157        """
158        transition_image = pg.Surface(self.transition_rect.size)
159        transition_image.fill(c.TRANSITION_COLOR)
160        transition_image.set_alpha(self.transition_alpha)
161        self.draw_level(surface)
162        surface.blit(transition_image, self.transition_rect)
163        self.transition_alpha += c.TRANSITION_SPEED 
164        if self.transition_alpha >= 255:
165            self.done = True
166
167    def draw_level(self, surface):
168        """
169        Blit graphics to game surface.
170        """
171        surface.blit(self.background.image, self.background.rect)
172        self.gui.draw(surface)
173
174
175class Inn(Shop):
176    """
177    Where our hero gets rest.
178    """
179    def __init__(self):
180        super(Inn, self).__init__()
181        self.name = c.INN
182        self.key = 'innman'
183
184    def make_dialogue(self):
185        """
186        Make the list of dialogue phrases.
187        """
188        return ["Welcome to the " + self.name + "!",
189                "Would you like a room to restore your health?"]
190
191    def make_accept_dialogue(self):
192        """
193        Make the dialogue for when the player buys an item.
194        """
195        return ['Your health has been replenished and your game saved!']
196
197    def make_purchasable_items(self):
198        """Make list of items to be chosen"""
199        dialogue = 'Rent a room (30 gold)'
200
201        item = {'type': 'room',
202                'price': 30,
203                'quantity': 0,
204                'power': None,
205                'dialogue': dialogue}
206
207        return [item]
208
209
210class WeaponShop(Shop):
211    """A place to buy weapons"""
212    def __init__(self):
213        super(WeaponShop, self).__init__()
214        self.name = c.WEAPON_SHOP
215        self.key = 'weaponman'
216        self.sell_items = ['Long Sword', 'Rapier']
217
218
219    def make_dialogue(self):
220        """Make the list of dialogue phrases"""
221        return ["Welcome to the " + self.name + "!",
222                "What weapon would you like to buy?"]
223
224
225    def make_purchasable_items(self):
226        """Make list of items to be chosen"""
227        longsword_dialogue = 'Long Sword (150 gold)'
228        rapier_dialogue = 'Rapier (50 gold)'
229
230        item2 = {'type': 'Long Sword',
231                'price': 150,
232                'quantity': 1,
233                'power': 11,
234                'dialogue': longsword_dialogue}
235
236        item1 = {'type': 'Rapier',
237                 'price': 50,
238                 'quantity': 1,
239                 'power': 9,
240                 'dialogue': rapier_dialogue}
241
242        return [item1, item2]
243
244
245class ArmorShop(Shop):
246    """A place to buy armor"""
247    def __init__(self):
248        super(ArmorShop, self).__init__()
249        self.name = c.ARMOR_SHOP
250        self.key = 'armorman'
251        self.sell_items = ['Chain Mail', 'Wooden Shield']
252
253
254    def make_dialogue(self):
255        """Make the list of dialogue phrases"""
256        return ["Welcome to the " + self.name + "!",
257                "Would piece of armor would you like to buy?"]
258
259
260    def make_purchasable_items(self):
261        """Make list of items to be chosen"""
262        chainmail_dialogue = 'Chain Mail (50 gold)'
263        shield_dialogue = 'Wooden Shield (75 gold)'
264
265        item = {'type': 'Chain Mail',
266                'price': 50,
267                'quantity': 1,
268                'power': 2,
269                'dialogue': chainmail_dialogue}
270
271        item2 = {'type': 'Wooden Shield',
272                 'price': 75,
273                 'quantity': 1,
274                 'power': 3,
275                 'dialogue': shield_dialogue}
276
277        return [item, item2]
278
279
280class MagicShop(Shop):
281    """A place to buy magic"""
282    def __init__(self):
283        super(MagicShop, self).__init__()
284        self.name = c.MAGIC_SHOP
285        self.key = 'magiclady'
286
287
288    def make_dialogue(self):
289        """Make the list of dialogue phrases"""
290        return ["Welcome to the " + self.name + "!",
291                "Would magic spell would you like to buy?"]
292
293
294    def make_purchasable_items(self):
295        """Make list of items to be chosen"""
296        fire_dialogue = 'Fire Blast (150 gold)'
297        cure_dialogue = 'Cure (50 gold)'
298
299        item1 = {'type': 'Cure',
300                 'price': 50,
301                 'quantity': 1,
302                 'magic points': 25,
303                 'power': 50,
304                 'dialogue': cure_dialogue}
305
306        item2 = {'type': 'Fire Blast',
307                'price': 150,
308                'quantity': 1,
309                'magic points': 75,
310                'power': 10,
311                'dialogue': fire_dialogue}
312
313        return [item1, item2]
314
315
316class PotionShop(Shop):
317    """A place to buy potions"""
318    def __init__(self):
319        super(PotionShop, self).__init__()
320        self.name = c.POTION_SHOP
321        self.key = 'potionlady'
322        self.sell_items = 'Healing Potion'
323
324
325    def make_dialogue(self):
326        """Make the list of dialogue phrases"""
327        return ["Welcome to the " + self.name + "!",
328                "What potion would you like to buy?"]
329
330
331    def make_purchasable_items(self):
332        """Make list of items to be chosen"""
333        healing_dialogue = 'Healing Potion (15 gold)'
334        ether_dialogue = 'Ether Potion (15 gold)'
335
336
337        item = {'type': 'Healing Potion',
338                'price': 15,
339                'quantity': 1,
340                'power': None,
341                'dialogue': healing_dialogue}
342
343        item2 = {'type': 'Ether Potion',
344                 'price': 15,
345                 'quantity': 1,
346                 'power': None,
347                 'dialogue': ether_dialogue}
348
349        return [item, item2]
350