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