all repos — Legends-RPG @ d42cfd5a960d68a90490ac950b309d27b477ce30

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                'dialogue': dialogue}
144
145        return [item]
146
147
148
149class WeaponShop(Shop):
150    """A place to buy weapons"""
151    def __init__(self):
152        super(WeaponShop, self).__init__()
153        self.name = c.WEAPON_SHOP
154        self.key = 'weaponman'
155        self.sell_items = ['Long Sword', 'Rapier']
156
157
158    def make_dialogue(self):
159        """Make the list of dialogue phrases"""
160        return ["Welcome to the " + self.name + "!",
161                "What weapon would you like to buy?"]
162
163
164    def make_purchasable_items(self):
165        """Make list of items to be chosen"""
166        longsword_dialogue = 'Long Sword (100 gold)'
167        rapier_dialogue = 'Rapier (50 gold)'
168
169        item2 = {'type': 'Long Sword',
170                'price': 100,
171                'quantity': 1,
172                'dialogue': longsword_dialogue}
173
174        item1 = {'type': 'Rapier',
175                 'price': 50,
176                 'quantity': 1,
177                 'dialogue': rapier_dialogue}
178
179        return [item1, item2]
180
181
182class ArmorShop(Shop):
183    """A place to buy armor"""
184    def __init__(self):
185        super(ArmorShop, self).__init__()
186        self.name = c.ARMOR_SHOP
187        self.key = 'armorman'
188        self.sell_items = ['Chain Mail', 'Wooden Shield']
189
190
191    def make_dialogue(self):
192        """Make the list of dialogue phrases"""
193        return ["Welcome to the " + self.name + "!",
194                "Would piece of armor would you like to buy?"]
195
196
197    def make_purchasable_items(self):
198        """Make list of items to be chosen"""
199        chainmail_dialogue = 'Chain Mail (50 gold)'
200        shield_dialogue = 'Wooden Shield (75 gold)'
201
202        item = {'type': 'Chain Mail',
203                'price': 50,
204                'quantity': 1,
205                'dialogue': chainmail_dialogue}
206
207        item2 = {'type': 'Wooden Shield',
208                 'price': 75,
209                 'quantity': 1,
210                 'dialogue': shield_dialogue}
211
212        return [item, item2]
213
214
215class MagicShop(Shop):
216    """A place to buy magic"""
217    def __init__(self):
218        super(MagicShop, self).__init__()
219        self.name = c.MAGIC_SHOP
220        self.key = 'magiclady'
221
222
223    def make_dialogue(self):
224        """Make the list of dialogue phrases"""
225        return ["Welcome to the " + self.name + "!",
226                "Would magic spell would you like to buy?"]
227
228
229    def make_purchasable_items(self):
230        """Make list of items to be chosen"""
231        fire_dialogue = 'Fire Blast (150 gold)'
232        cure_dialogue = 'Cure (150 gold)'
233
234        item1 = {'type': 'Cure',
235                 'price': 150,
236                 'quantity': 1,
237                 'dialogue': cure_dialogue}
238
239        item2 = {'type': 'Fire Blast',
240                'price': 150,
241                'quantity': 1,
242                'dialogue': fire_dialogue}
243
244        return [item1, item2]
245
246
247class PotionShop(Shop):
248    """A place to buy potions"""
249    def __init__(self):
250        super(PotionShop, self).__init__()
251        self.name = c.POTION_SHOP
252        self.key = 'potionlady'
253        self.sell_items = 'Healing Potion'
254
255
256    def make_dialogue(self):
257        """Make the list of dialogue phrases"""
258        return ["Welcome to the " + self.name + "!",
259                "What potion would you like to buy?"]
260
261
262    def make_purchasable_items(self):
263        """Make list of items to be chosen"""
264        healing_dialogue = 'Healing Potion (15 gold)'
265
266
267        item = {'type': 'Healing Potion',
268                'price': 15,
269                'quantity': 1,
270                'dialogue': healing_dialogue}
271
272        return [item]
273