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