This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
item.py
279 lines (251 loc) · 11.6 KB
/
item.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
from animate import Animator
from pygame.sprite import Sprite, Group, collide_rect
from pygame import image as pygimg
from pygame import time, transform
# TODO: Additional item types
class Item(Sprite):
"""Represents a generic item object in the mario game"""
MUSHROOM = 'mushroom'
ONE_UP = '1-up'
FIRE_FLOWER = 'fire-flower'
STARMAN = 'starman'
def __init__(self, x, y, image, speed, obstacles, floor, item_type, rise_from=None, animated=False):
super(Item, self).__init__()
if animated:
self.animator = Animator(image)
self.image = self.animator.get_image()
else:
self.animator = None
self.image = image
self.item_type = item_type
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = x, y
self.speed = speed
self.jump_speed = 0
self.obstacles = obstacles # objects that the item may collide with
self.floor = floor # rects for the floor
self.rise_from = rise_from
def rise(self):
"""Have the item rise up from another object"""
if not self.rise_from:
raise ValueError('Cannot rise from an object when that object is None')
if self.rect.bottom <= self.rise_from.rect.top:
self.rise_from = None
else:
self.rect.bottom -= 2
def jump(self):
"""Have the item jump into the air"""
if self.speed >= 0:
self.jump_speed = -(self.speed * 5)
else:
self.jump_speed = (self.speed * 5)
def flip_direction(self):
"""Flip the direction the item is moving on the x-axis"""
self.speed = -self.speed
self.rect.left += self.speed
def bounce_off_obstacles(self):
"""Check if the item has hit any obstacles which cause it to bounce the other direction"""
for obs in self.obstacles:
pts = [obs.rect.bottomleft, obs.rect.midleft,
obs.rect.bottomright, obs.rect.midright]
for pt in pts:
if self.rect.collidepoint(pt):
self.flip_direction()
return
for rect in self.floor:
pts = [rect.midleft, rect.midright, rect.bottomleft, rect.bottomright]
y_cap = rect.top
for pt in pts:
if self.rect.collidepoint(pt) or \
((self.rect.left == rect.right or self.rect.right == rect.left) and self.rect.top > y_cap):
self.flip_direction()
return
def fall(self):
"""If the item is not supported by any floor rects, then fall down"""
falling = True
for rect in self.floor:
# check if bottom is at the top of the floor rect and that the x pos is within floor area
if self.rect.bottom == rect.top and (rect.left < self.rect.center[0] < rect.right):
self.rect.bottom = rect.top
falling = False
break
if falling:
for obj in self.obstacles:
pts = [obj.rect.topleft, obj.rect.midtop, obj.rect.topright]
for pt in pts:
if self.rect.collidepoint(pt):
falling = False
break
if not falling:
break
if falling:
self.rect.bottom += abs(self.speed)
def update(self):
"""Update the item position based on its speed variables"""
if self.animator:
self.image = self.animator.get_image()
if not self.rise_from:
if abs(self.jump_speed) > 0:
self.rect.top += self.jump_speed
self.jump_speed += 1 # simulate gravity reducing speed
self.rect.left += self.speed
self.bounce_off_obstacles()
self.fall()
else:
self.rise()
class Mushroom(Item):
"""A mushroom power-up which can be picked up by Mario, causing size increase"""
def __init__(self, x, y, obstacles, floor, rise_from=None):
image = pygimg.load('map/mushroom.png')
speed = 2
super(Mushroom, self).__init__(x, y, image, speed, obstacles, floor,
Item.MUSHROOM, rise_from, animated=False)
class OneUp(Item):
"""A special mushroom which is meant to add an extra life on pickup"""
def __init__(self, x, y, obstacles, floor, rise_from=None):
image = pygimg.load('map/mushroom-1-up.png')
speed = 2
super(OneUp, self).__init__(x, y, image, speed, obstacles, floor,
Item.ONE_UP, rise_from, animated=False)
class FireFlower(Item):
"""A fire flower item which can be picked up by Mario, giving the ability to throw fire balls"""
def __init__(self, x, y, obstacles, floor, rise_from=None):
images = [pygimg.load('map/fire-flower-1.png'), pygimg.load('map/fire-flower-2.png'),
pygimg.load('map/fire-flower-3.png'), pygimg.load('map/fire-flower-4.png')]
speed = 0
super(FireFlower, self).__init__(x, y, images, speed, obstacles,
floor, Item.FIRE_FLOWER, rise_from, True)
class StarMan(Item):
"""A 'star-man' item which can be picked up by Mario, causing invincibility"""
def __init__(self, x, y, obstacles, floor, rise_from=None):
images = [pygimg.load('map/starman-1.png'), pygimg.load('map/starman-2.png'),
pygimg.load('map/starman-3.png'), pygimg.load('map/starman-4.png')]
speed = 2
self.last_jump = time.get_ticks()
self.jump_interval = 1000 # jump around every second
super(StarMan, self).__init__(x, y, images, speed, obstacles, floor, Item.STARMAN, rise_from, True)
def update(self):
touch_floor = False
for rect in self.floor:
if self.rect.bottom >= rect.top:
self.rect.bottom = rect.top
touch_floor = True
break
if abs(self.last_jump - time.get_ticks()) > self.jump_interval and touch_floor:
self.jump()
self.last_jump = time.get_ticks()
super(StarMan, self).update()
class FireBall(Sprite):
"""A fireball which can be thrown from Mario when he is in his fire flower state"""
def __init__(self, x, y, norm_images, explode_images, obstacles, floor, goomba, koopa, speed=5):
self.norm_animator = Animator(norm_images)
self.explode_animator = Animator(explode_images, repeat=False)
self.image = self.norm_animator.get_image()
self.rect = self.image.get_rect()
self.rect.x, self.rect.y = x, y
self.obstacles = obstacles
self.floor = floor
self.goomba, self.koopa = goomba, koopa
self.speed_x = speed
self.speed_y = speed
self.active = True
super(FireBall, self).__init__()
def check_hit_wall(self):
"""Check if the fireball has hit any walls"""
for obs in self.obstacles:
pts = [obs.rect.midleft, obs.rect.midright, obs.rect.bottomleft, obs.rect.bottomright]
for pt in pts:
if self.rect.collidepoint(pt):
self.active = False
return
for flr_rect in self.floor:
pts = [flr_rect.midleft, flr_rect.midright, flr_rect.bottomleft, flr_rect.bottomright]
for pt in pts:
if self.rect.collidepoint(pt):
self.active = False
return
def check_hit_enemies(self):
"""Check if the fireball has hit any enemies"""
for g_enemy in self.goomba:
if collide_rect(self, g_enemy): # FIXME: change kill() when animation works
g_enemy.kill()
self.active = False
return
for k_enemy in self.koopa:
if collide_rect(self, k_enemy):
k_enemy.kill()
self.active = False
return
def apply_gravity(self):
"""Apply gravity to the fireball, bounce off of horizontal side of surfaces"""
bounce = False
for obs in self.obstacles:
pts = [obs.rect.topleft, obs.rect.midtop, obs.rect.topright]
for pt in pts:
if self.rect.collidepoint(pt):
bounce = True
break
if bounce:
break
if not bounce:
for flr_rect in self.floor:
# check if bottom is at the top of the floor rect and that the x pos is within floor area
if self.rect.bottom >= flr_rect.top and (flr_rect.left < self.rect.center[0] < flr_rect.right):
bounce = True
break
if bounce:
self.speed_y = -abs(self.speed_y) # ensure speed in y-direction is negative
else:
self.speed_y += 2 # apply gravity
self.rect.y += self.speed_y
def update(self):
"""Update the position of the fireball"""
if self.active:
self.rect.x += self.speed_x
self.apply_gravity()
self.image = self.norm_animator.get_image()
self.check_hit_wall()
self.check_hit_enemies()
elif self.explode_animator.is_animation_done():
self.kill()
else:
self.image = self.explode_animator.get_image()
class FireBallController:
"""Manages fireballs and provides an interface for using them"""
def __init__(self, screen, map_group, obstacles, floor, origin, goomba, koopa):
self.screen = screen
self.origin = origin
self.map_group = map_group
self.obstacles = obstacles
self.floor = floor
self.goomba, self.koopa = goomba, koopa
self.fireballs = Group()
self.fb_images = [pygimg.load('map/super_mario_fireball_1.png'), pygimg.load('map/super_mario_fireball_2.png'),
pygimg.load('map/super_mario_fireball_3.png'), pygimg.load('map/super_mario_fireball_4.png')]
self.exp_images = [pygimg.load('map/super_mario_fireball_explode_1.png'),
pygimg.load('map/super_mario_fireball_explode_2.png'),
pygimg.load('map/super_mario_fireball_explode_3.png')]
self.fb_images = [transform.scale(img, (16, 16)) for img in self.fb_images]
self.exp_images = [transform.scale(img, (16, 16)) for img in self.exp_images]
def throw_fireball(self):
"""If there are not two fireballs already active, then throw a new fireball"""
if len(self.fireballs) < 2:
if self.origin.state_info['facing_right']:
n_fireball = FireBall(self.origin.rect.topright[0], self.origin.rect.topright[1], self.fb_images,
self.exp_images, self.obstacles, self.floor, self.goomba, self.koopa)
else:
n_fireball = FireBall(self.origin.rect.topleft[0], self.origin.rect.topleft[1], self.fb_images,
self.exp_images, self.obstacles, self.floor, self.goomba, self.koopa, speed=-5)
self.fireballs.add(n_fireball)
self.map_group.add(n_fireball)
return True
return False
def update_fireballs(self):
"""Update all fireballs currently in play"""
self.fireballs.update()
for fb in self.fireballs:
if fb.rect.x < (self.origin.rect.x - self.screen.get_width()) or \
fb.rect.x > (self.origin.rect.x + self.screen.get_width()) or \
(fb.rect.y < (self.origin.rect.y - self.screen.get_height()) or
fb.rect.y > (self.origin.rect.y + self.screen.get_height())):
fb.kill()