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