all repos — Legends-RPG @ 615ae8672bd8ae22eed4bbfc45a4b324bc4eb945

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