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.
6"""
7import copy
8import pygame as pg
9from .. import tools, setup
10from .. import constants as c
11from .. components import textbox, person
12
13
14class Gui(object):
15 """Class that controls the GUI of the shop state"""
16 def __init__(self, name, dialogue, level):
17 self.level = level
18 self.name = name
19 self.state = 'dialogue'
20 self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22)
21 self.index = 0
22 self.dialogue = dialogue
23 self.arrow = textbox.NextArrow()
24 self.selection_arrow = textbox.NextArrow()
25 self.arrow_pos1 = (50, 485)
26 self.arrow_pos2 = (50, 535)
27 self.selection_arrow.rect.topleft = self.arrow_pos1
28 self.dialogue_box = self.make_dialogue_box()
29 self.gold_box = self.make_gold_box()
30 self.selection_box = self.make_selection_box()
31 self.state_dict = self.make_state_dict()
32
33
34 def make_dialogue_box(self):
35 """Make the sprite that controls the dialogue"""
36 image = setup.GFX['dialoguebox']
37 rect = image.get_rect()
38 surface = pg.Surface(rect.size)
39 surface.set_colorkey(c.BLACK)
40 surface.blit(image, rect)
41 dialogue = self.font.render(self.dialogue[self.index],
42 True,
43 c.NEAR_BLACK)
44 dialogue_rect = dialogue.get_rect(left=50, top=50)
45 surface.blit(dialogue, dialogue_rect)
46 sprite = pg.sprite.Sprite()
47 sprite.image = surface
48 sprite.rect = rect
49 self.check_to_draw_arrow(sprite)
50
51 return sprite
52
53
54 def make_selection_box(self):
55 """Make the box for the player to select options"""
56 image = setup.GFX['shopbox']
57 rect = image.get_rect(bottom=608)
58
59 surface = pg.Surface(rect.size)
60 surface.set_colorkey(c.BLACK)
61 surface.blit(image, (0, 0))
62 choices = ['Rent a room. (30 Gold)',
63 'Leave.']
64 choice1 = self.font.render(choices[0], True, c.NEAR_BLACK)
65 choice1_rect = choice1.get_rect(x=200, y=25)
66 choice2 = self.font.render(choices[1], True, c.NEAR_BLACK)
67 choice2_rect = choice2.get_rect(x=200, y=75)
68 surface.blit(choice1, choice1_rect)
69 surface.blit(choice2, choice2_rect)
70 sprite = pg.sprite.Sprite()
71 sprite.image = surface
72 sprite.rect = rect
73
74 return sprite
75
76
77 def check_to_draw_arrow(self, sprite):
78 """Blink arrow if more text needs to be read"""
79 if self.index < len(self.dialogue) - 1:
80 sprite.image.blit(self.arrow.image, self.arrow.rect)
81
82
83 def make_gold_box(self):
84 """Make the box to display total gold"""
85 image = setup.GFX['goldbox']
86 rect = image.get_rect(bottom=608, right=800)
87
88 surface = pg.Surface(rect.size)
89 surface.set_colorkey(c.BLACK)
90 surface.blit(image, (0, 0))
91 gold = self.level.game_data['player items']['gold']
92 text = 'Gold: ' + str(gold)
93 text_render = self.font.render(text, True, c.NEAR_BLACK)
94 text_rect = text_render.get_rect(x=80, y=60)
95
96 surface.blit(text_render, text_rect)
97
98 sprite = pg.sprite.Sprite()
99 sprite.image = surface
100 sprite.rect = rect
101
102 return sprite
103
104
105
106 def make_state_dict(self):
107 """Make the state dictionary for the GUI behavior"""
108 state_dict = {'dialogue': self.control_dialogue,
109 'select': self.make_selection}
110
111 return state_dict
112
113
114 def control_dialogue(self, keys, current_time):
115 """Control the dialogue boxes"""
116 self.dialogue_box = self.make_dialogue_box()
117
118 if self.index < (len(self.dialogue) - 1):
119 if keys[pg.K_SPACE]:
120 self.index += 1
121
122 elif self.index == (len(self.dialogue) - 1):
123 self.state = 'select'
124
125
126 def make_selection(self, keys, current_time):
127 """Control the selection"""
128 self.selection_box = self.make_selection_box()
129 self.gold_box = self.make_gold_box()
130
131 if keys[pg.K_DOWN]:
132 self.selection_arrow.rect.topleft = self.arrow_pos2
133 elif keys[pg.K_UP]:
134 self.selection_arrow.rect.topleft = self.arrow_pos1
135 elif keys[pg.K_SPACE]:
136 if self.selection_arrow.rect.topleft == self.arrow_pos2:
137 self.level.done = True
138 self.level.game_data['last direction'] = 'down'
139 elif self.selection_arrow.rect.topleft == self.arrow_pos1:
140 self.level.game_data['player items']['gold'] -= 30
141
142
143
144
145
146 def update(self, keys, current_time):
147 """Updates the shop GUI"""
148 state_function = self.state_dict[self.state]
149 state_function(keys, current_time)
150
151
152 def draw(self, surface):
153 """Draw GUI to level surface"""
154 if self.state == 'dialogue':
155 surface.blit(self.dialogue_box.image, self.dialogue_box.rect)
156 surface.blit(self.gold_box.image, self.gold_box.rect)
157 elif self.state == 'select':
158 surface.blit(self.dialogue_box.image, self.dialogue_box.rect)
159 surface.blit(self.selection_box.image, self.selection_box.rect)
160 surface.blit(self.selection_arrow.image, self.selection_arrow.rect)
161 surface.blit(self.gold_box.image, self.gold_box.rect)
162
163
164
165class Shop(tools._State):
166 """Basic shop state"""
167 def __init__(self, name):
168 super(Shop, self).__init__(name)
169 self.map_width = 13
170 self.map_height = 10
171
172 def startup(self, current_time, game_data):
173 """Startup state"""
174 self.game_data = game_data
175 self.current_time = current_time
176 self.state = 'normal'
177 self.get_image = tools.get_image
178 self.dialogue = self.make_dialogue()
179 self.background = self.make_background()
180 self.gui = Gui('Inn', self.dialogue, self)
181
182
183 def make_dialogue(self):
184 """Make the list of dialogue phrases"""
185 dialogue = ["Welcome to the " + self.name + "!",
186 "Would you like to rent a room to restore your health?"]
187
188 return dialogue
189
190
191
192 def make_background(self):
193 """Make the level surface"""
194 background = pg.sprite.Sprite()
195 surface = pg.Surface(c.SCREEN_SIZE).convert()
196 surface.fill(c.BLACK_BLUE)
197 background.image = surface
198 background.rect = background.image.get_rect()
199
200 player = self.make_sprite('player', 96, 32, 150)
201 shop_owner = self.make_sprite('man1', 32, 32, 600)
202 counter = self.make_counter()
203
204 background.image.blit(player.image, player.rect)
205 background.image.blit(shop_owner.image, shop_owner.rect)
206 background.image.blit(counter.image, counter.rect)
207
208 return background
209
210
211 def make_sprite(self, key, coordx, coordy, x, y=304):
212 """Get the image for the player"""
213 spritesheet = setup.GFX[key]
214 surface = pg.Surface((32, 32))
215 surface.set_colorkey(c.BLACK)
216 image = self.get_image(coordx, coordy, 32, 32, spritesheet)
217 rect = image.get_rect()
218 surface.blit(image, rect)
219
220 surface = pg.transform.scale(surface, (96, 96))
221 rect = surface.get_rect(left=x, centery=y)
222 sprite = pg.sprite.Sprite()
223 sprite.image = surface
224 sprite.rect = rect
225
226 return sprite
227
228
229 def make_counter(self):
230 """Make the counter to conduct business"""
231 sprite_sheet = copy.copy(setup.GFX['house'])
232 sprite = pg.sprite.Sprite()
233 sprite.image = self.get_image(102, 64, 26, 82, sprite_sheet)
234 sprite.image = pg.transform.scale2x(sprite.image)
235 sprite.rect = sprite.image.get_rect(left=550, top=225)
236
237 return sprite
238
239
240
241
242 def update(self, surface, keys, current_time):
243 """Update level state"""
244 self.gui.update(keys, current_time)
245 self.draw_level(surface)
246 if self.done:
247 self.next = c.TOWN
248
249
250 def draw_level(self, surface):
251 """Blit graphics to game surface"""
252 surface.blit(self.background.image, self.background.rect)
253 self.gui.draw(surface)