all repos — Legends-RPG @ 9ee373a472b1ac434cf268cdcf3875ada4c57b29

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