all repos — Legends-RPG @ 39646be108cc46f9389d53efbb27244073e8e992

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.item = 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                    'Back.']
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']
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        dialogue = ['Long Sword. (100 gold)',
171                    'Back.']
172
173        item = {'type': 'Long Sword',
174                'price': 100,
175                'quantity': 1,
176                'dialogue': dialogue}
177
178        return item
179
180
181class ArmorShop(Shop):
182    """A place to buy armor"""
183    def __init__(self):
184        super(ArmorShop, self).__init__()
185        self.name = 'Armor Shop'
186        self.key = 'armorman'
187        self.sell_items = ['Chain Mail']
188
189
190    def make_dialogue(self):
191        """Make the list of dialogue phrases"""
192        return ["Welcome to the " + self.name + "!",
193                "Would piece of armor would you like to buy?"]
194
195
196    def make_purchasable_items(self):
197        """Make list of items to be chosen"""
198        dialogue = ['Chain Mail. (50 gold)',
199                    'Back.']
200
201        item = {'type': 'Chain Mail',
202                'price': 50,
203                'quantity': 1,
204                'dialogue': dialogue}
205
206        return item
207
208
209class MagicShop(Shop):
210    """A place to buy magic"""
211    def __init__(self):
212        super(MagicShop, self).__init__()
213        self.name = 'Magic Shop'
214        self.key = 'magiclady'
215
216
217    def make_dialogue(self):
218        """Make the list of dialogue phrases"""
219        return ["Welcome to the " + self.name + "!",
220                "Would magic spell would you like to buy?"]
221
222
223    def make_purchasable_items(self):
224        """Make list of items to be chosen"""
225        dialogue = ['Fire Spell. (150 gold)',
226                    'Back.']
227
228        item = {'type': 'Fire Spell',
229                'price': 150,
230                'quantity': 1,
231                'dialogue': dialogue}
232
233        return item
234
235
236class PotionShop(Shop):
237    """A place to buy potions"""
238    def __init__(self):
239        super(PotionShop, self).__init__()
240        self.name = 'Potion Shop'
241        self.key = 'potionlady'
242        self.sell_items = 'Healing Potion'
243
244
245    def make_dialogue(self):
246        """Make the list of dialogue phrases"""
247        return ["Welcome to the " + self.name + "!",
248                "What potion would you like to buy?"]
249
250
251    def make_purchasable_items(self):
252        """Make list of items to be chosen"""
253        dialogue = ['Healing Potion. (15 gold)',
254                    'Back.']
255
256        item = {'type': 'Healing Potion',
257                'price': 15,
258                'quantity': 1,
259                'dialogue': dialogue}
260
261        return item
262