-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.py
303 lines (267 loc) · 11.7 KB
/
sprites.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
# sprite classes for jumpy game
import xml.etree.ElementTree
from random import choice, randrange
import pygame as pg
from settings import *
vec = pg.math.Vector2
class Spritesheet:
"""
Utility class for loading and parsing spritesheets
"""
def __init__(self, filename):
self.sheet = pg.image.load(filename).convert()
self.elements = xml.etree.ElementTree.parse(filename.replace('.png', '.xml')).getroot().findall('SubTexture')
def get_image(self, name):
"""
grab an image out of the spritesheet by its name in the xml file
:param name: name of the image i.e: 'bunny1_jump.png'
:return: pygame.Surface
"""
for elt in self.elements:
if elt.get('name') == name:
return self._get_image(int(elt.get('x')), int(elt.get('y')), int(elt.get('width')), int(elt.get('height')))
def _get_image(self, x, y, width, height):
"""
grab an image out of the spritesheet by its coordonates
:param x:
:param y:
:param width:
:param height:
:return: pygame.Surface
"""
image = pg.Surface((width, height))
image.blit(self.sheet, (0, 0), (x, y, width, height))
image = pg.transform.scale(image, (width // 2, height // 2))
image.set_colorkey(BLACK)
return image
class Player(pg.sprite.Sprite):
def __init__(self, game):
self._layer = PLAYER_LAYER
self.groups = game.all_sprites
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.walking = False
self.jumping = False
self.hurted = False
self.current_frame = 0
self.last_update = 0
self.load_images()
self.image = self.game.spritesheet.get_image('bunny2_ready.png')
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.pos = vec(30, HEIGHT - 100)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
def load_images(self):
"""
Load all images from the spritesheet
:return:
"""
self.standing_frames = [self.game.spritesheet.get_image('bunny1_stand.png'),
self.game.spritesheet.get_image('bunny1_ready.png'), ]
self.walking_frames_r = [self.game.spritesheet.get_image('bunny1_walk1.png'),
self.game.spritesheet.get_image('bunny1_walk2.png'), ]
self.walking_frames_l = [pg.transform.flip(frame, True, False) for frame in self.walking_frames_r]
self.jump_frame = self.game.spritesheet.get_image('bunny1_jump.png')
self.hurted_frame = self.game.spritesheet.get_image('bunny1_hurt.png')
def jump(self):
"""
Make the player jump only if standing on a platform
TODO: enable double jumping
:return:
"""
self.rect.x += 1
hits = pg.sprite.spritecollide(self, self.game.platforms, False)
self.rect.x -= 1
if hits and not self.jumping:
self.game.sounds['jump'].play()
self.jumping = True
self.vel.y = -PLAYER_JUMP
def jump_cut(self):
"""
Cut the jump in case of a quick space key press
:return:
"""
if self.jumping and self.vel.y < -3:
self.vel.y = -3
def animate(self):
"""
Switch frames for character animation while jumping, walking and idle
:return:
"""
now = pg.time.get_ticks()
if self.vel.x != 0:
self.walking = True
else:
self.walking = False
# show hurted frame
if self.hurted:
self.image = self.hurted_frame
self.current_frame += 1
# show jump animation
if self.jumping and not self.hurted:
self.current_frame = 0
self.image = self.jump_frame
# show walk animation
if self.walking and not self.jumping and not self.hurted and now - self.last_update > 100:
self.last_update = now
self.current_frame = (self.current_frame + 1) % len(self.walking_frames_l)
bottom = self.rect.bottom
if self.vel.x > 0:
self.image = self.walking_frames_r[self.current_frame]
else:
self.image = self.walking_frames_l[self.current_frame]
self.rect = self.image.get_rect()
self.rect.bottom = bottom
# show idle animation
if not self.jumping and not self.walking and not self.hurted and now - self.last_update > 350:
self.last_update = now
self.current_frame = (self.current_frame + 1) % len(self.standing_frames)
bottom = self.rect.bottom
self.image = self.standing_frames[self.current_frame]
self.rect = self.image.get_rect()
self.rect.bottom = bottom
self.mask = pg.mask.from_surface(self.image)
def update(self):
"""
motion management with gravity and friction
:return:
"""
self.animate()
self.acc = vec(0, PLAYER_GRAVITY)
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
self.acc.x = -PLAYER_ACC
if keys[pg.K_RIGHT]:
self.acc.x = PLAYER_ACC
# apply friction
self.acc.x += self.vel.x * PLAYER_FRICTION
# equation of motion
self.vel += self.acc
if abs(self.vel.x) < 0.1:
self.vel.x = 0
self.pos += self.vel + 0.5 * self.acc
# wrap around the sides of the screen
if self.pos.x > WIDTH + self.rect.width / 2:
self.pos.x = 0 - self.rect.width / 2
elif self.pos.x < 0 - self.rect.width / 2:
self.pos.x = WIDTH + self.rect.width / 2
self.rect.midbottom = self.pos
class Platform(pg.sprite.Sprite):
def __init__(self, game, x, y):
self._layer = PLATFORM_LAYER
self.groups = game.all_sprites, game.platforms
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
images = [self.game.spritesheet.get_image('ground_grass.png'),
self.game.spritesheet.get_image('ground_grass_broken.png'),
self.game.spritesheet.get_image('ground_grass_small.png'),
# self.game.spritesheet.get_image('ground_grass_small_broken.png'),
]
self.image = choice(images)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
if randrange(100) < POW_SPWAN_PCT:
Pow(self.game, self)
class Pow(pg.sprite.Sprite):
pow_span_pct = POW_SPWAN_PCT
def __init__(self, game, platform):
self._layer = POW_LAYER
self.groups = game.all_sprites, game.powerups
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.platform = platform
self.available_types = ['spring', 'coin']
self.type = choice(self.available_types)
self.frames = {type_name: {} for type_name in self.available_types}
self.load_images()
self.image = self.frames[self.type]['default_frame']
self.current_frame = 0
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = self.platform.rect.centerx
self.rect.bottom = self.platform.rect.top
self.engaged = False
self.last_update = 0
def load_images(self):
self.frames['coin']['default_frame'] = self.game.spritesheet.get_image('gold_1.png')
self.frames['coin']['animation'] = [self.game.spritesheet.get_image('gold_1.png'),
self.game.spritesheet.get_image('gold_2.png'),
self.game.spritesheet.get_image('gold_3.png'),
self.game.spritesheet.get_image('gold_4.png'),
pg.transform.flip(self.game.spritesheet.get_image('gold_3.png'), True, False),
pg.transform.flip(self.game.spritesheet.get_image('gold_2.png'), True, False), ]
self.frames['spring']['default_frame'] = self.game.spritesheet.get_image('spring.png')
self.frames['spring']['engaged_frames'] = [self.game.spritesheet.get_image('spring_in.png'),
self.game.spritesheet.get_image('spring.png'),
self.game.spritesheet.get_image('spring_out.png'),
self.game.spritesheet.get_image('spring_out.png'),
self.game.spritesheet.get_image('spring_out.png'), ]
def update(self, *args):
if self.game.platforms.has(self.platform):
self.animate()
else:
self.kill()
def animate(self):
now = pg.time.get_ticks()
if self.type == 'spring':
if self.engaged and now - self.last_update > 1500 and \
self.current_frame < len(self.frames[self.type]['engaged_frames']):
self.image = self.frames[self.type]['engaged_frames'][self.current_frame]
self.current_frame += 1
self.rect = self.image.get_rect()
self.rect.centerx = self.platform.rect.centerx
self.rect.bottom = self.platform.rect.top
elif not self.engaged or self.current_frame >= len(self.frames[self.type]['engaged_frames']):
self.image = self.frames[self.type]['default_frame']
self.current_frame = 0
self.engaged = False
self.rect = self.image.get_rect()
self.rect.centerx = self.platform.rect.centerx
self.rect.bottom = self.platform.rect.top
if self.type == 'coin' and \
now - self.last_update > 150 and \
self.current_frame < len(self.frames[self.type]['animation']):
self.last_update = now
self.image = self.frames[self.type]['animation'][self.current_frame]
self.current_frame = (self.current_frame + 1) % len(self.frames[self.type]['animation'])
self.rect = self.image.get_rect()
self.rect.centerx = self.platform.rect.centerx
self.rect.bottom = self.platform.rect.top
class Mob(pg.sprite.Sprite):
def __init__(self, game):
self._layer = MOB_LAYER
self.groups = game.all_sprites, game.mobs
pg.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.image_up = self.game.spritesheet.get_image('flyMan_fly.png')
self.image_up.set_colorkey(BLACK)
self.image_down = self.game.spritesheet.get_image('flyMan_jump.png')
self.image_down.set_colorkey(BLACK)
self.image = self.image_up
self.rect = self.image.get_rect()
self.rect.centerx = choice([-self.rect.width, WIDTH + self.rect.width])
self.vx = randrange(MOB_MIN_SPEED, MOB_MAX_SPEED - 1)
if self.rect.centerx > WIDTH:
self.vx = -self.vx
self.rect.y = HEIGHT / 3 # randrange(HEIGHT / 2) # the mob spawns in the high middle of the screen
self.vy = 0
self.dy = 0.5
def update(self, *args):
self.rect.x += self.vx
self.vy += self.dy
if self.vy > 3 or self.vy < -3:
self.dy = -self.dy
center = self.rect.center
if self.dy < 0:
self.image = self.image_up
else:
self.image = self.image_down
self.rect = self.image.get_rect()
self.mask = pg.mask.from_surface(self.image)
self.rect.center = center
self.rect.y += self.vy
if self.rect.left > WIDTH + 50 or self.rect.right < -50:
self.kill()