all repos — Legends-RPG @ c5dc6b5321b8c948f5f1becc48ffa927c3b73209

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
 22    def startup(self, current_time, game_data):
 23        """Startup state"""
 24        self.game_data = game_data
 25        self.current_time = current_time
 26        self.state = 'normal'
 27        self.next = c.TOWN
 28        self.get_image = tools.get_image
 29        self.dialogue = self.make_dialogue()
 30        self.accept_dialogue = self.make_accept_dialogue()
 31        self.accept_sale_dialogue = self.make_accept_sale_dialogue()
 32        self.items = self.make_purchasable_items()
 33        self.background = self.make_background()
 34        self.gui = shopgui.Gui(self)
 35
 36
 37    def make_dialogue(self):
 38        """Make the list of dialogue phrases"""
 39        raise NotImplementedError
 40
 41
 42    def make_accept_dialogue(self):
 43        """Make the dialogue for when the player buys an item"""
 44        return ['Item purchased.']
 45
 46
 47    def make_accept_sale_dialogue(self):
 48        """Make the dialogue for when the player sells an item"""
 49        return ['Item sold.']
 50
 51
 52    def make_purchasable_items(self):
 53        """Make the list of items to be bought at shop"""
 54        raise NotImplementedError
 55
 56
 57    def make_background(self):
 58        """Make the level surface"""
 59        background = pg.sprite.Sprite()
 60        surface = pg.Surface(c.SCREEN_SIZE).convert()
 61        surface.fill(c.BLACK_BLUE)
 62        background.image = surface
 63        background.rect = background.image.get_rect()
 64
 65        player = self.make_sprite('player', 96, 32, 150)
 66        shop_owner = self.make_sprite(self.key, 32, 32, 600)
 67        counter = self.make_counter()
 68
 69        background.image.blit(player.image, player.rect)
 70        background.image.blit(shop_owner.image, shop_owner.rect)
 71        background.image.blit(counter.image, counter.rect)
 72
 73        return background
 74
 75
 76    def make_sprite(self, key, coordx, coordy, x, y=304):
 77        """Get the image for the player"""
 78        spritesheet = setup.GFX[key]
 79        surface = pg.Surface((32, 32))
 80        surface.set_colorkey(c.BLACK)
 81        image = self.get_image(coordx, coordy, 32, 32, spritesheet)
 82        rect = image.get_rect()
 83        surface.blit(image, rect)
 84
 85        surface = pg.transform.scale(surface, (96, 96))
 86        rect = surface.get_rect(left=x, centery=y)
 87        sprite = pg.sprite.Sprite()
 88        sprite.image = surface
 89        sprite.rect = rect
 90
 91        return sprite
 92
 93
 94    def make_counter(self):
 95        """Make the counter to conduct business"""
 96        sprite_sheet = copy.copy(setup.GFX['house'])
 97        sprite = pg.sprite.Sprite()
 98        sprite.image = self.get_image(102, 64, 26, 82, sprite_sheet)
 99        sprite.image = pg.transform.scale2x(sprite.image)
100        sprite.rect = sprite.image.get_rect(left=550, top=225)
101
102        return sprite
103
104
105    def update(self, surface, keys, current_time):
106        """Update level state"""
107        self.gui.update(keys, current_time)
108        self.draw_level(surface)
109
110
111    def draw_level(self, surface):
112        """Blit graphics to game surface"""
113        surface.blit(self.background.image, self.background.rect)
114        self.gui.draw(surface)
115
116
117
118class Inn(Shop):
119    """Where our hero gets rest"""
120    def __init__(self):
121        super(Inn, self).__init__()
122        self.name = c.INN
123        self.key = 'innman'
124
125    def make_dialogue(self):
126        """Make the list of dialogue phrases"""
127        return ["Welcome to the " + self.name + "!",
128                "Would you like a room to restore your health?"]
129
130
131    def make_accept_dialogue(self):
132        """Make the dialogue for when the player buys an item"""
133        return ['Your health has been replenished!']
134
135
136    def make_purchasable_items(self):
137        """Make list of items to be chosen"""
138        dialogue = 'Rent a room (30 gold)'
139
140        item = {'type': 'room',
141                'price': 30,
142                'quantity': 0,
143                'power': None,
144                'dialogue': dialogue}
145
146        return [item]
147
148
149
150class WeaponShop(Shop):
151    """A place to buy weapons"""
152    def __init__(self):
153        super(WeaponShop, self).__init__()
154        self.name = c.WEAPON_SHOP
155        self.key = 'weaponman'
156        self.sell_items = ['Long Sword', 'Rapier']
157
158
159    def make_dialogue(self):
160        """Make the list of dialogue phrases"""
161        return ["Welcome to the " + self.name + "!",
162                "What weapon would you like to buy?"]
163
164
165    def make_purchasable_items(self):
166        """Make list of items to be chosen"""
167        longsword_dialogue = 'Long Sword (100 gold)'
168        rapier_dialogue = 'Rapier (50 gold)'
169
170        item2 = {'type': 'Long Sword',
171                'price': 100,
172                'quantity': 1,
173                'power': 10,
174                'dialogue': longsword_dialogue}
175
176        item1 = {'type': 'Rapier',
177                 'price': 50,
178                 'quantity': 1,
179                 'power': 5,
180                 'dialogue': rapier_dialogue}
181
182        return [item1, item2]
183
184
185class ArmorShop(Shop):
186    """A place to buy armor"""
187    def __init__(self):
188        super(ArmorShop, self).__init__()
189        self.name = c.ARMOR_SHOP
190        self.key = 'armorman'
191        self.sell_items = ['Chain Mail', 'Wooden Shield']
192
193
194    def make_dialogue(self):
195        """Make the list of dialogue phrases"""
196        return ["Welcome to the " + self.name + "!",
197                "Would piece of armor would you like to buy?"]
198
199
200    def make_purchasable_items(self):
201        """Make list of items to be chosen"""
202        chainmail_dialogue = 'Chain Mail (50 gold)'
203        shield_dialogue = 'Wooden Shield (75 gold)'
204
205        item = {'type': 'Chain Mail',
206                'price': 50,
207                'quantity': 1,
208                'power': 1,
209                'dialogue': chainmail_dialogue}
210
211        item2 = {'type': 'Wooden Shield',
212                 'price': 75,
213                 'quantity': 1,
214                 'power': 1,
215                 'dialogue': shield_dialogue}
216
217        return [item, item2]
218
219
220class MagicShop(Shop):
221    """A place to buy magic"""
222    def __init__(self):
223        super(MagicShop, self).__init__()
224        self.name = c.MAGIC_SHOP
225        self.key = 'magiclady'
226
227
228    def make_dialogue(self):
229        """Make the list of dialogue phrases"""
230        return ["Welcome to the " + self.name + "!",
231                "Would magic spell would you like to buy?"]
232
233
234    def make_purchasable_items(self):
235        """Make list of items to be chosen"""
236        fire_dialogue = 'Fire Blast (150 gold)'
237        cure_dialogue = 'Cure (150 gold)'
238
239        item1 = {'type': 'Cure',
240                 'price': 150,
241                 'quantity': 1,
242                 'magic points': 25,
243                 'power': 50,
244                 'dialogue': cure_dialogue}
245
246        item2 = {'type': 'Fire Blast',
247                'price': 150,
248                'quantity': 1,
249                'magic points': 75,
250                'power': 10,
251                'dialogue': fire_dialogue}
252
253        return [item1, item2]
254
255
256class PotionShop(Shop):
257    """A place to buy potions"""
258    def __init__(self):
259        super(PotionShop, self).__init__()
260        self.name = c.POTION_SHOP
261        self.key = 'potionlady'
262        self.sell_items = 'Healing Potion'
263
264
265    def make_dialogue(self):
266        """Make the list of dialogue phrases"""
267        return ["Welcome to the " + self.name + "!",
268                "What potion would you like to buy?"]
269
270
271    def make_purchasable_items(self):
272        """Make list of items to be chosen"""
273        healing_dialogue = 'Healing Potion (15 gold)'
274        ether_dialogue = 'Ether Potion (15 gold)'
275
276
277        item = {'type': 'Healing Potion',
278                'price': 15,
279                'quantity': 1,
280                'dialogue': healing_dialogue}
281
282        item2 = {'type': 'Ether Potion',
283                 'price': 15,
284                 'quantity': 1,
285                 'dialogue': ether_dialogue}
286
287        return [item, item2]
288