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
12
13
14class Gui(object):
15 """Class that controls the GUI of the shop state"""
16 def __init__(self, name, dialogue, level):
17 self.name = name
18 self.state = 'dialogue'
19 self.font = pg.font.Font(setup.FONTS['Fixedsys500c'], 22)
20 self.index = 0
21 self.dialogue = dialogue
22 self.arrow = textbox.NextArrow()
23 self.selection_arrow = textbox.NextArrow()
24 self.arrow_pos1 = (50, 485)
25 self.arrow_pos2 = (50, 535)
26 self.selection_arrow.rect.topleft = self.arrow_pos1
27 self.dialogue_box = self.make_dialogue_box()
28 self.gold = self.make_gold_box()
29 self.selection_box = self.make_selection_box()
30 self.state_dict = self.make_state_dict()
31 self.level = level
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 return None
86
87
88 def make_state_dict(self):
89 """Make the state dictionary for the GUI behavior"""
90 state_dict = {'dialogue': self.control_dialogue,
91 'select': self.make_selection}
92
93 return state_dict
94
95
96 def control_dialogue(self, keys, current_time):
97 """Control the dialogue boxes"""
98 self.dialogue_box = self.make_dialogue_box()
99
100 if self.index < (len(self.dialogue) - 1):
101 if keys[pg.K_SPACE]:
102 self.index += 1
103
104 elif self.index == (len(self.dialogue) - 1):
105 self.state = 'select'
106
107
108 def make_selection(self, keys, current_time):
109 """Control the selection"""
110 self.selection_box = self.make_selection_box()
111
112 if keys[pg.K_DOWN]:
113 self.selection_arrow.rect.topleft = self.arrow_pos2
114 elif keys[pg.K_UP]:
115 self.selection_arrow.rect.topleft = self.arrow_pos1
116 elif keys[pg.K_SPACE]:
117 if self.selection_arrow.rect.topleft == self.arrow_pos2:
118 self.level.done = True
119
120
121
122
123 def update(self, keys, current_time):
124 """Updates the shop GUI"""
125 state_function = self.state_dict[self.state]
126 state_function(keys, current_time)
127
128
129 def draw(self, surface):
130 """Draw GUI to level surface"""
131 if self.state == 'dialogue':
132 surface.blit(self.dialogue_box.image, self.dialogue_box.rect)
133 elif self.state == 'select':
134 surface.blit(self.dialogue_box.image, self.dialogue_box.rect)
135 surface.blit(self.selection_box.image, self.selection_box.rect)
136 surface.blit(self.selection_arrow.image, self.selection_arrow.rect)
137
138
139
140class Shop(tools._State):
141 """Basic shop state"""
142 def __init__(self, name):
143 super(Shop, self).__init__(name)
144 self.map_width = 13
145 self.map_height = 10
146
147 def startup(self, current_time, persist):
148 """Startup state"""
149 self.persist = persist
150 self.current_time = current_time
151 self.state = 'normal'
152 self.get_image = tools.get_image
153 self.dialogue = self.make_dialogue()
154 self.background = self.make_background()
155 self.player = None
156 self.sprites = None
157 self.gui = Gui('Inn', self.dialogue, self)
158
159
160
161
162 def make_dialogue(self):
163 """Make the list of dialogue phrases"""
164 dialogue = ["Welcome to the " + self.name + "!",
165 "Would you like to rent a room to restore your health?"]
166
167 return dialogue
168
169
170
171 def make_background(self):
172 """Make the level surface"""
173 background = pg.sprite.Sprite()
174 surface = pg.Surface(c.SCREEN_SIZE).convert()
175 surface.fill(c.BLACK_BLUE)
176 background.image = surface
177 background.rect = background.image.get_rect()
178
179 player = self.make_sprite('player', 96, 32, 150)
180 shop_owner = self.make_sprite('man1', 32, 32, 600)
181 counter = self.make_counter()
182
183 background.image.blit(player.image, player.rect)
184 background.image.blit(shop_owner.image, shop_owner.rect)
185 background.image.blit(counter.image, counter.rect)
186
187 return background
188
189
190 def make_sprite(self, key, coordx, coordy, x, y=304):
191 """Get the image for the player"""
192 spritesheet = setup.GFX[key]
193 surface = pg.Surface((32, 32))
194 surface.set_colorkey(c.BLACK)
195 image = self.get_image(coordx, coordy, 32, 32, spritesheet)
196 rect = image.get_rect()
197 surface.blit(image, rect)
198
199 surface = pg.transform.scale(surface, (96, 96))
200 rect = surface.get_rect(left=x, centery=y)
201 sprite = pg.sprite.Sprite()
202 sprite.image = surface
203 sprite.rect = rect
204
205 return sprite
206
207
208 def make_counter(self):
209 """Make the counter to conduct business"""
210 sprite_sheet = copy.copy(setup.GFX['house'])
211 sprite = pg.sprite.Sprite()
212 sprite.image = self.get_image(102, 64, 26, 82, sprite_sheet)
213 sprite.image = pg.transform.scale2x(sprite.image)
214 sprite.rect = sprite.image.get_rect(left=550, top=225)
215
216 return sprite
217
218
219 def update(self, surface, keys, current_time):
220 """Update level state"""
221 self.gui.update(keys, current_time)
222 self.draw_level(surface)
223 if self.done:
224 self.next = c.TOWN
225
226
227 def draw_level(self, surface):
228 """Blit graphics to game surface"""
229 surface.blit(self.background.image, self.background.rect)
230 self.gui.draw(surface)