-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.py
411 lines (393 loc) · 17.1 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# sprite classes for game
# i used some ideas from CodePylet https://www.youtube.com/watch?v=osDofIdja6s&t=1038s
# i also borrowed pretty much all of this from kids can code - thanks!
# on acceleration https://www.khanacademy.org/science/physics/one-dimensional-motion/kinematic-formulas/v/average-velocity-for-constant-acceleration
# on vectors: https://www.youtube.com/watch?v=ml4NSzCQobk
import pygame as pg
from pygame.sprite import Sprite
import random
from random import randint, randrange, choice
from settings import *
vec = pg.math.Vector2
class Spritesheet:
# class for loading and parsing sprite sheets
def __init__(self, filename):
self.spritesheet = pg.image.load(filename).convert()
def get_image(self, x, y, width, height):
image = pg.Surface((width, height))
image.blit(self.spritesheet, (0,0), (x, y, width, height))
image = pg.transform.scale(image, (width // 2, height // 2))
return image
class Player(Sprite):
def __init__(self, game):
# allows layering in LayeredUpdates sprite group - thanks pygame!
self._layer = PLAYER_LAYER
# add player to game groups when instantiated
self.groups = game.all_sprites
Sprite.__init__(self, self.groups)
self.game = game
self.walking = False
self.jumping = False
self.current_frame = 0
self.last_update = 0
self.load_images()
# self.image = pg.Surface((30,40))
# self.image = self.game.spritesheet.get_image(614,1063,120,191)
self.image = self.standing_frames[0]
self.image.set_colorkey(BLACK)
# self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT /2)
self.pos = vec(WIDTH / 2, HEIGHT / 2)
self.vel = vec(0, 0)
self.acc = vec(0, 0)
print("adding vecs " + str(self.vel + self.acc))
def load_images(self):
self.standing_frames = [self.game.spritesheet.get_image(690, 406, 120, 201),
self.game.spritesheet.get_image(614, 1063, 120, 191)
]
for frame in self.standing_frames:
frame.set_colorkey(BLACK)
self.walk_frames_r = [self.game.spritesheet.get_image(678, 860, 120, 201),
self.game.spritesheet.get_image(692, 1458, 120, 207)
]
self.walk_frames_l = []
for frame in self.walk_frames_r:
frame.set_colorkey(BLACK)
self.walk_frames_l.append(pg.transform.flip(frame, True, False))
self.jump_frame = self.game.spritesheet.get_image(382, 763, 150, 181)
self.jump_frame.set_colorkey(BLACK)
def update(self):
self.animate()
self.acc = vec(0, PLAYER_GRAV)
# print("acc " + str(self.acc))
# print("vel " + str(self.vel))
keys = pg.key.get_pressed()
if keys[pg.K_a]:
self.acc.x = -PLAYER_ACC
if keys[pg.K_d]:
self.acc.x = PLAYER_ACC
# set player friction
self.acc.x += self.vel.x * PLAYER_FRICTION
# equations 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
# jump to other side of screen
# if self.pos.x > WIDTH + self.rect.width / 2:
# self.pos.x = 0 - self.rect.width / 2
# if self.pos.x < 0 - self.rect.width / 2:
# self.pos.x = WIDTH + self.rect.width / 2
self.rect.midbottom = self.pos
# cuts the jump short when the space bar is released
def jump_cut(self):
if self.jumping:
if self.vel.y < -5:
self.vel.y = -5
def jump(self):
print("jump is working")
# check pixel below
self.rect.y += 2
hits = pg.sprite.spritecollide(self, self.game.platforms, False)
# adjust based on checked pixel
self.rect.y -= 2
# only allow jumping if player is on platform
if hits and not self.jumping:
# play sound only when space bar is hit and while not jumping
self.game.jump_sound[choice([0,1])].play()
# tell the program that player is currently jumping
self.jumping = True
self.vel.y = -PLAYER_JUMP
print(self.acc.y)
def animate(self):
# gets time in miliseconds
now = pg.time.get_ticks()
if self.vel.x != 0:
self.walking = True
else:
self.walking = False
if self.walking:
if now - self.last_update > 200:
self.last_update = now
self.current_frame = (self.current_frame + 1) % len(self.walk_frames_l)
bottom = self.rect.bottom
if self.vel.x > 0:
self.image = self.walk_frames_r[self.current_frame]
else:
self.image = self.walk_frames_l[self.current_frame]
self.rect = self.image.get_rect()
self.rect.bottom = bottom
# checks state
if not self.jumping and not self.walking:
# gets current delta time and checks against 200 miliseconds
if now - self.last_update > 200:
self.last_update = now
self.current_frame = (self.current_frame + 1) % len(self.standing_frames)
# reset bottom for each frame of animation
bottom = self.rect.bottom
self.image = self.standing_frames[self.current_frame]
self.rect = self.image.get_rect()
self.rect.bottom = bottom
class Platform(Sprite):
def __init__(self, game, x, y):
# allows layering in LayeredUpdates sprite group
self._layer = PLATFORM_LAYER
# add Platforms to game groups when instantiated
self.groups = game.all_sprites, game.platforms
Sprite.__init__(self, self.groups)
self.game = game
images = [self.game.spritesheet.get_image(0, 288, 380, 94),
self.game.spritesheet.get_image(213, 1662, 201, 100)]
self.image = random.choice(images)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
if random.randrange(100) < GOLD_SPAWN_PCT:
Gold(self.game, self)
if random.randrange(100) < SILVER_SPAWN_PCT:
Silver(self.game, self)
if random.randrange(100) < BRONZE_SPAWN_PCT:
Bronze(self.game, self)
if random.randrange(100) < BRONZE_SPAWN_PCT:
Spike(self.game, self)
if random.randrange(100) < SILVER_SPAWN_PCT:
Spring(self.game, self)
if random.randrange(100) < CARROT_SPAWN_PCT:
Carrot(self.game, self)
class BrokenPlatform(Sprite):
def __init__(self, game, x, y):
# allows layering in LayeredUpdates sprite group
self._layer = PLATFORM_LAYER
# add Platforms to game groups when instantiated
self.groups = game.all_sprites, game.brokenplatforms
Sprite.__init__(self, self.groups)
self.game = game
self.type = random.choice(['brokenplatform'])
images = [self.game.spritesheet.get_image(0, 384, 380, 94),
self.game.spritesheet.get_image(382, 204, 200, 100)]
self.image = random.choice(images)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
if random.randrange(100) < GOLD_SPAWN_PCT:
Gold(self.game, self)
if random.randrange(100) < SILVER_SPAWN_PCT:
Silver(self.game, self)
if random.randrange(100) < BRONZE_SPAWN_PCT:
Bronze(self.game, self)
if random.randrange(100) < BRONZE_SPAWN_PCT:
Spike(self.game, self)
if random.randrange(100) < SILVER_SPAWN_PCT:
Spring(self.game, self)
if random.randrange(100) < CARROT_SPAWN_PCT:
Carrot(self.game, self)
class Gold(Sprite):
def __init__(self, game, plat):
# allows layering in LayeredUpdates sprite group
self._layer = POW_LAYER
# add a groups property where we can pass all instances of this object into game groups
self.groups = game.all_sprites, game.gold
Sprite.__init__(self, self.groups)
self.game = game
self.plat = plat
self.type = random.choice(['gold'])
# self.spinning_frames = [self.game.spritesheet.get_image(698, 1931, 84, 84),
# self.game.spritesheet.get_image(829, 0, 66, 84),
# self.game.spritesheet.get_image(897, 1574, 50, 84),
# self.game.spritesheet.get_image(645, 651, 15, 84),
# self.game.spritesheet.get_image(pg.transform.flip(897, 1574, 50, 84)),
# self.game.spritesheet.get_image(pg.transform.flip(829, 0, 84, 84))
# ]
# self.spinning_frames.set_colorkey(BLACK)
# self.rect = self.spinning_frames.get_rect()
self.image = self.game.spritesheet.get_image(698, 1931, 84, 84)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = self.plat.rect.centerx
self.rect.bottom = self.plat.rect.top - 5
def update(self):
self.rect.bottom = self.plat.rect.top - 5
# checks to see if plat is in the game's platforms group so we can kill the powerup instance
if not self.game.platforms.has(self.plat):
self.kill()
class Silver(Sprite):
def __init__(self, game, plat):
# allows layering in LayeredUpdates sprite group
self._layer = POW_LAYER
# add a groups property where we can pass all instances of this object into game groups
self.groups = game.all_sprites, game.silver
Sprite.__init__(self, self.groups)
self.game = game
self.plat = plat
self.type = random.choice(['silver'])
self.image = self.game.spritesheet.get_image(584, 406, 84, 84)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = self.plat.rect.centerx
self.rect.bottom = self.plat.rect.top - 5
def update(self):
self.rect.bottom = self.plat.rect.top - 5
# checks to see if plat is in the game's platforms group so we can kill the powerup instance
if not self.game.platforms.has(self.plat):
self.kill()
class Bronze(Sprite):
def __init__(self, game, plat):
# allows layering in LayeredUpdates sprite group
self._layer = POW_LAYER
# add a groups property where we can pass all instances of this object into game groups
self.groups = game.all_sprites, game.bronze
Sprite.__init__(self, self.groups)
self.game = game
self.plat = plat
self.type = random.choice(['bronze'])
self.image = self.game.spritesheet.get_image(707, 296, 84, 84)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = self.plat.rect.centerx
self.rect.bottom = self.plat.rect.top - 5
def update(self):
self.rect.bottom = self.plat.rect.top - 5
# checks to see if plat is in the game's platforms group so we can kill the powerup instance
if not self.game.platforms.has(self.plat):
self.kill()
class Carrot(Sprite):
def __init__(self, game, plat):
# allows layering in LayeredUpdates sprite group
self._layer = POW_LAYER
# add a groups property where we can pass all instances of this object into game groups
self.groups = game.all_sprites, game.carrot
Sprite.__init__(self, self.groups)
self.game = game
self.plat = plat
self.type = random.choice(['carrot'])
self.image = self.game.spritesheet.get_image(814, 1661, 78, 70)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = self.plat.rect.centerx
self.rect.bottom = self.plat.rect.top - 5
def update(self):
self.rect.bottom = self.plat.rect.top - 5
# checks to see if plat is in the game's platforms group so we can kill the powerup instance
if not self.game.platforms.has(self.plat):
self.kill()
class FlyMob1(Sprite):
def __init__(self, game):
# allows layering in LayeredUpdates sprite group
self._layer = MOB_LAYER
# add a groups property where we can pass all instances of this object into game groups
self.groups = game.all_sprites, game.flymobs1
Sprite.__init__(self, self.groups)
self.game = game
self.image_up = self.game.spritesheet.get_image(566, 510, 122, 139)
self.image_up.set_colorkey(BLACK)
self.image_down = self.game.spritesheet.get_image(568, 1534, 122, 135)
self.image_down.set_colorkey(BLACK)
self.image = self.image_up
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = choice([-100, WIDTH + 100])
self.rect_top = self.rect.top
self.vx = randrange(1, 4)
if self.rect.centerx > WIDTH:
self.vx *= -1
self.rect.y = randrange(HEIGHT/2)
self.vy = 0
self.dy = 0.5
def update(self):
self.rect.x += self.vx
self.vy += self.dy
self.rect_top = self.rect.top
if self.vy > 3 or self.vy < -3:
self.dy *= -1
center = self.rect.center
if self.dy < 0:
self.image = self.image_up
else:
self.image = self.image_down
self.rect.center = center
self.rect_top = self.rect.top
self.rect.y += self.vy
# if self.rect.left > WIDTH + 100 or self.rect.right < -100:
# self.kill()
class FlyMob2(Sprite):
def __init__(self, game):
# allows layering in LayeredUpdates sprite group
self._layer = MOB_LAYER
# add a groups property where we can pass all instances of this object into game groups
self.groups = game.all_sprites, game.flymobs2
Sprite.__init__(self, self.groups)
self.game = game
self.image_top = self.game.spritesheet.get_image(382, 635, 174, 126)
self.image_top.set_colorkey(BLACK)
self.image_bottom = self.game.spritesheet.get_image(382, 510, 182, 123)
self.image_bottom.set_colorkey(BLACK)
self.image = self.image_top
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = choice([-100, WIDTH + 100])
self.rect_top = self.rect.top
self.vx = randrange(1, 4)
if self.rect.centerx > WIDTH:
self.vx *= -1
self.rect.y = randrange(HEIGHT/2)
self.vy = 0
self.dy = 0.5
def update(self):
self.rect.x += self.vx
self.vy += self.dy
self.rect_top = self.rect.top
if self.vy > 3 or self.vy < -3:
self.dy *= -1
center = self.rect.center
if self.dy < 0:
self.image = self.image_top
elif self.dy > 0:
self.image = self.image_bottom
self.rect.center = center
self.rect_top = self.rect.top
self.rect.y += self.vy
# if self.rect.left > WIDTH + 100 or self.rect.right < -100:
# self.kill()
class Spike(Sprite):
def __init__(self, game, plat):
# allows layering in LayeredUpdates sprite group
self._layer = MOB_LAYER
# add a groups property where we can pass all instances of this object into game groups
self.groups = game.all_sprites, game.spikes
Sprite.__init__(self, self.groups)
self.game = game
self.plat = plat
self.type = random.choice(['spike'])
self.image = self.game.spritesheet.get_image(232, 1390, 95, 53)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = self.plat.rect.centerx
self.rect.bottom = self.plat.rect.top
def update(self):
self.rect.bottom = self.plat.rect.top
# checks to see if plat is in the game's platforms group so we can kill the powerup instance
if not self.game.platforms.has(self.plat):
self.kill()
class Spring(Sprite):
def __init__(self, game, plat):
# allows layering in LayeredUpdates sprite group
self._layer = MOB_LAYER
# add a groups property where we can pass all instances of this object into game groups
self.groups = game.all_sprites, game.springs
Sprite.__init__(self, self.groups)
self.game = game
self.plat = plat
self.type = random.choice(['spring'])
self.image = self.game.spritesheet.get_image(434, 1265, 145, 110)
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = self.plat.rect.centerx
self.rect.bottom = self.plat.rect.top
def update(self):
self.rect.bottom = self.plat.rect.top
# checks to see if plat is in the game's platforms group so we can kill the powerup instance
if not self.game.platforms.has(self.plat):
self.kill()