-
Notifications
You must be signed in to change notification settings - Fork 1
/
powerups.py
324 lines (263 loc) · 10.2 KB
/
powerups.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import pygame as pg
from .. import constants as c
from .. import setup
class Powerup(pg.sprite.Sprite):
"""Base class for all powerup_group"""
def __init__(self, x, y):
super(Powerup, self).__init__()
def setup_powerup(self, x, y, name, setup_frames):
"""This separate setup function allows me to pass a different
setup_frames method depending on what the powerup is"""
self.sprite_sheet = setup.GFX['item_objects']
self.frames = []
self.frame_index = 0
setup_frames()
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.y = y
self.state = c.REVEAL
self.y_vel = -1
self.x_vel = 0
self.direction = c.RIGHT
self.box_height = y
self.gravity = 1
self.max_y_vel = 8
self.animate_timer = 0
self.name = name
def get_image(self, x, y, width, height):
"""Get the image frames from the sprite sheet"""
image = pg.Surface([width, height]).convert()
rect = image.get_rect()
image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
image.set_colorkey(c.BLACK)
image = pg.transform.scale(image,
(int(rect.width * c.SIZE_MULTIPLIER),
int(rect.height * c.SIZE_MULTIPLIER)))
return image
def update(self, game_info, *args):
"""Updates powerup behavior"""
self.current_time = game_info[c.CURRENT_TIME]
self.handle_state()
def handle_state(self):
pass
def revealing(self, *args):
"""Action when powerup leaves the coin box or brick"""
self.rect.y += self.y_vel
if self.rect.bottom <= self.box_height:
self.rect.bottom = self.box_height
self.y_vel = 0
self.state = c.SLIDE
def sliding(self):
"""Action for when powerup slides along the ground"""
if self.direction == c.RIGHT:
self.x_vel = 3
else:
self.x_vel = -3
def falling(self):
"""When powerups fall of a ledge"""
if self.y_vel < self.max_y_vel:
self.y_vel += self.gravity
class Mushroom(Powerup):
"""Powerup that makes Mario become bigger"""
def __init__(self, x, y, name='mushroom'):
super(Mushroom, self).__init__(x, y)
self.setup_powerup(x, y, name, self.setup_frames)
def setup_frames(self):
"""Sets up frame list"""
self.frames.append(self.get_image(0, 0, 16, 16))
def handle_state(self):
"""Handles behavior based on state"""
if self.state == c.REVEAL:
self.revealing()
elif self.state == c.SLIDE:
self.sliding()
elif self.state == c.FALL:
self.falling()
class LifeMushroom(Mushroom):
"""1up mushroom"""
def __init__(self, x, y, name='1up_mushroom'):
super(LifeMushroom, self).__init__(x, y)
self.setup_powerup(x, y, name, self.setup_frames)
def setup_frames(self):
self.frames.append(self.get_image(16, 0, 16, 16))
class FireFlower(Powerup):
"""Powerup that allows Mario to throw fireballs"""
def __init__(self, x, y, name=c.FIREFLOWER):
super(FireFlower, self).__init__(x, y)
self.setup_powerup(x, y, name, self.setup_frames)
def setup_frames(self):
"""Sets up frame list"""
self.frames.append(
self.get_image(0, 32, 16, 16))
self.frames.append(
self.get_image(16, 32, 16, 16))
self.frames.append(
self.get_image(32, 32, 16, 16))
self.frames.append(
self.get_image(48, 32, 16, 16))
def handle_state(self):
"""Handle behavior based on state"""
if self.state == c.REVEAL:
self.revealing()
elif self.state == c.RESTING:
self.resting()
def revealing(self):
"""Animation of flower coming out of box"""
self.rect.y += self.y_vel
if self.rect.bottom <= self.box_height:
self.rect.bottom = self.box_height
self.state = c.RESTING
self.animation()
def resting(self):
"""Fire Flower staying still on opened box"""
self.animation()
def animation(self):
"""Method to make the Fire Flower blink"""
if (self.current_time - self.animate_timer) > 30:
if self.frame_index < 3:
self.frame_index += 1
else:
self.frame_index = 0
self.image = self.frames[self.frame_index]
self.animate_timer = self.current_time
class Star(Powerup):
"""A powerup that gives mario invincibility"""
def __init__(self, x, y, name='star'):
super(Star, self).__init__(x, y)
self.setup_powerup(x, y, name, self.setup_frames)
self.animate_timer = 0
self.rect.y += 1 # looks more centered offset one pixel
self.gravity = .4
def setup_frames(self):
"""Creating the self.frames list where the images for the animation
are stored"""
self.frames.append(self.get_image(1, 48, 15, 16))
self.frames.append(self.get_image(17, 48, 15, 16))
self.frames.append(self.get_image(33, 48, 15, 16))
self.frames.append(self.get_image(49, 48, 15, 16))
def handle_state(self):
"""Handles behavior based on state"""
if self.state == c.REVEAL:
self.revealing()
elif self.state == c.BOUNCE:
self.bouncing()
def revealing(self):
"""When the star comes out of the box"""
self.rect.y += self.y_vel
if self.rect.bottom <= self.box_height:
self.rect.bottom = self.box_height
self.start_bounce(-2)
self.state = c.BOUNCE
self.animation()
def animation(self):
"""sets image for animation"""
if (self.current_time - self.animate_timer) > 30:
if self.frame_index < 3:
self.frame_index += 1
else:
self.frame_index = 0
self.animate_timer = self.current_time
self.image = self.frames[self.frame_index]
def start_bounce(self, vel):
"""Transitions into bouncing state"""
self.y_vel = vel
def bouncing(self):
"""Action when the star is bouncing around"""
self.animation()
if self.direction == c.LEFT:
self.x_vel = -5
else:
self.x_vel = 5
class FireBall(pg.sprite.Sprite):
"""Shot from Fire Mario"""
def __init__(self, x, y, facing_right, name=c.FIREBALL):
super(FireBall, self).__init__()
self.sprite_sheet = setup.GFX['item_objects']
self.setup_frames()
if facing_right:
self.direction = c.RIGHT
self.x_vel = 12
else:
self.direction = c.LEFT
self.x_vel = -12
self.y_vel = 10
self.gravity = .9
self.frame_index = 0
self.animation_timer = 0
self.state = c.FLYING
self.image = self.frames[self.frame_index]
self.rect = self.image.get_rect()
self.rect.right = x
self.rect.y = y
self.name = name
def setup_frames(self):
"""Sets up animation frames"""
self.frames = []
self.frames.append(
self.get_image(96, 144, 8, 8)) # Frame 1 of flying
self.frames.append(
self.get_image(104, 144, 8, 8)) # Frame 2 of Flying
self.frames.append(
self.get_image(96, 152, 8, 8)) # Frame 3 of Flying
self.frames.append(
self.get_image(104, 152, 8, 8)) # Frame 4 of flying
self.frames.append(
self.get_image(112, 144, 16, 16)) # frame 1 of exploding
self.frames.append(
self.get_image(112, 160, 16, 16)) # frame 2 of exploding
self.frames.append(
self.get_image(112, 176, 16, 16)) # frame 3 of exploding
def get_image(self, x, y, width, height):
"""Get the image frames from the sprite sheet"""
image = pg.Surface([width, height]).convert()
rect = image.get_rect()
image.blit(self.sprite_sheet, (0, 0), (x, y, width, height))
image.set_colorkey(c.BLACK)
image = pg.transform.scale(image,
(int(rect.width * c.SIZE_MULTIPLIER),
int(rect.height * c.SIZE_MULTIPLIER)))
return image
def update(self, game_info, viewport):
"""Updates fireball behavior"""
self.current_time = game_info[c.CURRENT_TIME]
self.handle_state()
self.check_if_off_screen(viewport)
def handle_state(self):
"""Handles behavior based on state"""
if self.state == c.FLYING:
self.animation()
elif self.state == c.BOUNCING:
self.animation()
elif self.state == c.EXPLODING:
self.animation()
def animation(self):
"""adjusts frame for animation"""
if self.state == c.FLYING or self.state == c.BOUNCING:
if (self.current_time - self.animation_timer) > 200:
if self.frame_index < 3:
self.frame_index += 1
else:
self.frame_index = 0
self.animation_timer = self.current_time
self.image = self.frames[self.frame_index]
elif self.state == c.EXPLODING:
if (self.current_time - self.animation_timer) > 50:
if self.frame_index < 6:
self.frame_index += 1
self.image = self.frames[self.frame_index]
self.animation_timer = self.current_time
else:
self.kill()
def explode_transition(self):
"""Transitions fireball to EXPLODING state"""
self.frame_index = 4
centerx = self.rect.centerx
self.image = self.frames[self.frame_index]
self.rect.centerx = centerx
self.state = c.EXPLODING
def check_if_off_screen(self, viewport):
"""Removes from sprite group if off-screen"""
if (self.rect.x > viewport.right) or (self.rect.y > viewport.bottom) \
or (self.rect.right < viewport.x):
self.kill()