-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsprites.py
323 lines (255 loc) · 11.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
import pygame
from defaults import *
from random import choice, randrange
class Spritesheet:
# load and parse assets from spritesheets
def __init__(self, filename):
self.spritesheet = pygame.image.load(filename).convert()
def get_image(self, x, y, width, height):
image = pygame.Surface((width, height))
image.blit(self.spritesheet, (0, 0), (x, y, width, height))
return image
class Platformsheet:
# load and parse assets from spritesheets
def __init__(self, filename):
self.platformsheet = pygame.image.load(filename).convert()
def get_image(self, x, y, width, height):
image = pygame.Surface((width, height))
image.blit(self.platformsheet, (0, 0), (x, y, width, height))
return image
class Player(pygame.sprite.Sprite):
def __init__(self, game, avi):
self.__layer__ = PLAYER_LAYER
self.groups = game.sprites
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.walking = False
self.isjumping = False
self.falling = False
self.current_frame = 0 # current frame
self.last_update = 0
self.avi = avi
self.loadCharacters(self.avi)
self.image = self.standing[0]
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.center = (WIDTH / 2, HEIGHT / 2)
self.position = pygame.math.Vector2(WIDTH / 2, HEIGHT / 2)
self.velocity = pygame.math.Vector2(0, 0)
self.accleration = pygame.math.Vector2(0, 0)
def loadCharacters(self, avi):
if(avi == "green"):
self.standing = [
self.game.spritesheet.get_image(890, 0, 38, 50),
self.game.spritesheet.get_image(890, 51, 38, 50)
]
self.walking_r = [
self.game.spritesheet.get_image(890, 366, 38, 50),
self.game.spritesheet.get_image(889, 963, 38, 48),
self.game.spritesheet.get_image(889, 877, 38, 48)
]
self.flying = [
self.game.spritesheet.get_image(890, 51, 38, 50),
self.game.spritesheet.get_image(849, 877, 38, 43),
self.game.spritesheet.get_image(849, 429, 40, 39)
]
if(avi == "red"):
self.standing = [
self.game.spritesheet.get_image(850, 518, 39, 48),
self.game.spritesheet.get_image(850, 469, 39, 48)
]
self.walking_r = [
self.game.spritesheet.get_image(850, 47, 39, 48),
self.game.spritesheet.get_image(849, 96, 39, 45),
self.game.spritesheet.get_image(710, 384, 49, 45)
]
self.flying = [
self.game.spritesheet.get_image(850, 469, 39, 48),
self.game.spritesheet.get_image(850, 0, 39, 46),
self.game.spritesheet.get_image(650, 685, 56, 38)
]
if(avi == "blue"):
self.standing = [
self.game.spritesheet.get_image(762, 203, 45, 54),
self.game.spritesheet.get_image(760, 435, 45, 54)
]
self.walking_r = [
self.game.spritesheet.get_image(759, 812, 45, 54),
self.game.spritesheet.get_image(760, 380, 45, 54),
self.game.spritesheet.get_image(759, 503, 45, 52)
]
self.flying = [
self.game.spritesheet.get_image(760, 435, 45, 54),
self.game.spritesheet.get_image(759, 556, 45, 50),
self.game.spritesheet.get_image(758, 771, 45, 40)
]
if(avi == "grey"):
self.standing = [
self.game.spritesheet.get_image(890, 799, 36, 45),
self.game.spritesheet.get_image(927, 578, 36, 45)
]
self.walking_r = [
self.game.spritesheet.get_image(927, 791, 36, 45),
self.game.spritesheet.get_image(890, 417, 37, 43),
self.game.spritesheet.get_image(890, 494, 37, 42)
]
self.flying = [
self.game.spritesheet.get_image(927, 578, 36, 45),
self.game.spritesheet.get_image(890, 537, 37, 40),
self.game.spritesheet.get_image(764, 55, 44, 36)
]
self.walking_l = []
for frame in self.walking_r:
frame.set_colorkey(BLACK)
self.walking_l.append(pygame.transform.flip(frame, True, False))
for frame in self.standing:
frame.set_colorkey(BLACK)
def jump(self):
# jump only when standing on a platform
self.rect.x += 1
hits = pygame.sprite.pygame.sprite.spritecollide(self, self.game.platforms, False)
self.rect.x -= 1
if hits and not self.isjumping:
self.game.jumpSound.play()
self.isjumping = True
self.velocity.y = PLAYER_STRENGTH
now = pygame.time.get_ticks()
if now - self.last_update > 350:
self.last_update = now
self.current_frame = (self.current_frame + 1) % len(self.flying)
bottom = self.rect.bottom
self.image = self.flying[self.current_frame]
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.bottom = bottom
def jumpCut(self):
if self.isjumping:
if self.velocity.y < -3:
self.velocity.y = -3
def update(self):
self.animate()
self.accleration = pygame.math.Vector2(0, PLAYER_GRAVITY)
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.accleration.x = -PLAYER_ACCELERATION
if keys[pygame.K_RIGHT]:
self.accleration.x = PLAYER_ACCELERATION
# motion algo
self.accleration.x += self.velocity.x * PLAYER_FRICTION
self.velocity += self.accleration
if abs(self.velocity.x) < 0.1:
self.velocity.x = 0
self.position += self.velocity + 0.5 * self.accleration
# wraparound player
if self.position.x > WIDTH + self.rect.width / 2:
self.position.x = 0 - self.rect.width / 2
if self.position.x < 0 - self.rect.width / 2:
self.position.x = WIDTH + self.rect.width / 2
self.rect.midbottom = self.position
def animate(self):
now = pygame.time.get_ticks()
if self.velocity.x != 0:
self.walking = True
else:
self.walking = False
# show walk animation
if self.walking:
if now - self.last_update > 180:
self.last_update = now
self.current_frame = (self.current_frame + 1) % len(self.walking_l)
bottom = self.rect.bottom
if self.velocity.x > 0:
self.image = self.walking_r[self.current_frame]
else:
self.image = self.walking_l[self.current_frame]
self.rect = self.image.get_rect()
self.rect.bottom = bottom
# show idle animation
if not self.isjumping and not self.walking:
if now - self.last_update > 350:
self.last_update = now
self.current_frame = (self.current_frame + 1) % len(self.standing)
bottom = self.rect.bottom
self.image = self.standing[self.current_frame]
self.rect = self.image.get_rect()
self.rect.bottom = bottom
self.mask = pygame.mask.from_surface(self.image)
class Platform(pygame.sprite.Sprite):
def __init__(self, game, x, y):
self.__layer__ = PLATFORM_LAYER
self.groups = game.sprites, game.platforms
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
images = [
self.game.platformsheet.get_image(64, 0, 192, 64),
self.game.platformsheet.get_image(64, 192, 192, 64),
self.game.platformsheet.get_image(64, 384, 192, 64),
self.game.platformsheet.get_image(448, 62, 130, 64),
self.game.platformsheet.get_image(448, 254, 130, 64),
self.game.platformsheet.get_image(448, 448, 130, 64)
]
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_SPAWN_HZ:
Power(self.game, self)
class Power(pygame.sprite.Sprite):
def __init__(self, game, plat):
self.__layer__ = POWER_LAYER
self.groups = game.sprites, game.powerups
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.plat = plat
self.type = choice(['boost', 'life'])
if(self.type == "boost"):
self.image = self.game.spritesheet.get_image(929, 317, 32, 30)
else:
self.image = self.game.spritesheet.get_image(928, 138, 34, 39)
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
if not self.game.platforms.has(self.plat):
self.kill()
class Enemy(pygame.sprite.Sprite):
def __init__(self, game):
self.__layer__ = ENEMY_LAYER
self.groups = game.sprites, game.enemies
pygame.sprite.Sprite.__init__(self, self.groups)
self.game = game
self.flying = self.game.spritesheet.get_image(455, 390, 64, 38)
self.flyingDown = self.game.spritesheet.get_image(455, 533, 64, 43)
self.image = self.flying
self.rect = self.image.get_rect()
self.rect.centerx = choice([-100, WIDTH + 100])
self.vx = randrange(1, 5)
if(self.rect.centerx > WIDTH):
self.vx *= -1
self.rect.y = randrange(HEIGHT / 2)
self.vy = 0
self.dc = 0.5
def update(self):
self.rect.x += self.vx
self.vy += self.dc
if(self.vy > 3 or self.vy < -3):
self.dc *= -1
center = self.rect.center
if self.dc < 0:
self.image = self.flying
else:
self.image = self.flyingDown
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
self.rect.center = center
self.rect.y += self.vy
if self.rect.left > WIDTH + 100 or self.rect.right < -100:
self.kill()
# <SubTexture name="enemyFlying_1.png" x="455" y="390" width="64" height="38" frameX="-0" frameY="-0" frameWidth="64" frameHeight="38"/>
# <SubTexture name="enemyFlying_2.png" x="455" y="429" width="64" height="38" frameX="-0" frameY="-0" frameWidth="64" frameHeight="38"/>
# <SubTexture name="enemyFlying_3.png" x="455" y="533" width="64" height="43" frameX="-0" frameY="-0" frameWidth="64" frameHeight="43"/>
# <SubTexture name="enemyFlying_4.png" x="584" y="996" width="60" height="25" frameX="-0" frameY="-0" frameWidth="60" frameHeight="25"/>