all repos — Legends-RPG @ 4818df1862ee7d8c27d7775f4b425616bd61f2ee

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
118
119
120
121class Inn(Shop):
122    """Where our hero gets rest"""
123    def __init__(self):
124        super(Inn, self).__init__()
125        self.name = 'Inn'
126        self.key = 'innman'
127
128    def make_dialogue(self):
129        """Make the list of dialogue phrases"""
130        return ["Welcome to the " + self.name + "!",
131                "Would you like to rent a room to restore your health?"]
132
133
134    def make_accept_dialogue(self):
135        """Make the dialogue for when the player buys an item"""
136        return ['Your health has been replenished!']
137
138
139    def make_purchasable_items(self):
140        """Make list of items to be chosen"""
141        dialogue = ['Rent a room (30 gold)',
142                    'Leave']
143
144        item = {'type': 'room',
145                'price': 30,
146                'quantity': 0,
147                'dialogue': dialogue}
148
149        return item
150
151
152
153class WeaponShop(Shop):
154    """A place to buy weapons"""
155    def __init__(self):
156        super(WeaponShop, self).__init__()
157        self.name = 'Weapon Shop'
158        self.key = 'weaponman'
159        self.sell_items = ['Long Sword', 'Rapier']
160
161
162    def make_dialogue(self):
163        """Make the list of dialogue phrases"""
164        return ["Welcome to the " + self.name + "!",
165                "What weapon would you like to buy?"]
166
167
168    def make_purchasable_items(self):
169        """Make list of items to be chosen"""
170        longsword_dialogue = 'Long Sword (100 gold)'
171        rapier_dialogue = 'Rapier (50 gold)'
172
173        item = {'type': 'Long Sword',
174                'price': 100,
175                'quantity': 1,
176                'dialogue': longsword_dialogue}
177
178        item2 = {'type': 'Rapier',
179                 'price': 50,
180                 'quantity': 1,
181                 'dialogue': rapier_dialogue}
182
183        return [item, item2]
184
185
186class ArmorShop(Shop):
187    """A place to buy armor"""
188    def __init__(self):
189        super(ArmorShop, self).__init__()
190        self.name = '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                'dialogue': chainmail_dialogue}
210
211        item2 = {'type': 'Wooden Shield',
212                 'price': 75,
213                 'quantity': 1,
214                 'dialogue': shield_dialogue}
215
216        return [item, item2]
217
218
219class MagicShop(Shop):
220    """A place to buy magic"""
221    def __init__(self):
222        super(MagicShop, self).__init__()
223        self.name = 'Magic Shop'
224        self.key = 'magiclady'
225
226
227    def make_dialogue(self):
228        """Make the list of dialogue phrases"""
229        return ["Welcome to the " + self.name + "!",
230                "Would magic spell would you like to buy?"]
231
232
233    def make_purchasable_items(self):
234        """Make list of items to be chosen"""
235        dialogue = ['Fire Spell (150 gold)',
236                    'Leave']
237
238        item = {'type': 'Fire Spell',
239                'price': 150,
240                'quantity': 1,
241                'dialogue': dialogue}
242
243        return item
244
245
246class PotionShop(Shop):
247    """A place to buy potions"""
248    def __init__(self):
249        super(PotionShop, self).__init__()
250        self.name = 'Potion Shop'
251        self.key = 'potionlady'
252        self.sell_items = 'Healing Potion'
253
254
255    def make_dialogue(self):
256        """Make the list of dialogue phrases"""
257        return ["Welcome to the " + self.name + "!",
258                "What potion would you like to buy?"]
259
260
261    def make_purchasable_items(self):
262        """Make list of items to be chosen"""
263        dialogue = ['Healing Potion (15 gold)',
264                    'Cancel']
265
266        item = {'type': 'Healing Potion',
267                'price': 15,
268                'quantity': 1,
269                'dialogue': dialogue}
270
271        return item
272