Repository navigation
Expand file tree
/
Copy pathendless_runner.py
More file actions
356 lines (273 loc) · 12.5 KB
/
Copy pathendless_runner.py
File metadata and controls
356 lines (273 loc) · 12.5 KB
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
import pygame
from pygame.locals import *
import random
import math
pygame.init()
# create the game window
game_width = 800
game_height = 400
size = (game_width, game_height)
game = pygame.display.set_mode(size)
pygame.display.set_caption('Endless Runner')
# game variables
score = 0
speed = 3
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.height = 150
self.x = 25
self.y = game_height - self.height
self.action = 'running'
self.health = 3
# load the running sprites
self.running_sprites = []
self.running_sprite_index = 0
for i in range(10):
running_sprite = pygame.image.load(f'images/running/run{i}.png').convert_alpha()
scale = self.height / running_sprite.get_height()
new_width = running_sprite.get_width() * scale
new_height = running_sprite.get_height() * scale
running_sprite = pygame.transform.scale(running_sprite, (new_width, new_height))
self.running_sprites.append(running_sprite)
# load the jumping sprites
self.jumping_sprites = []
self.jumping_sprite_index = 0
for i in range(10):
jumping_sprite = pygame.image.load(f'images/jumping/jump{i}.png').convert_alpha()
scale = self.height / jumping_sprite.get_height()
new_width = jumping_sprite.get_width() * scale
new_height = jumping_sprite.get_height() * scale
jumping_sprite = pygame.transform.scale(jumping_sprite, (new_width, new_height))
self.jumping_sprites.append(jumping_sprite)
# set the initial sprite rect
self.rect = self.running_sprites[self.running_sprite_index].get_rect()
self.rect.x = self.x
self.rect.y = self.y
# number of frames player is invincible after getting hurt
self.invincibility_frame = 0
def draw(self):
''' draw the sprite based on the character action and index '''
if self.action == 'running':
running_sprite = self.running_sprites[int(self.running_sprite_index)]
# add invincibility effect when hurt
if self.invincibility_frame > 0:
self.invincibility_frame -= 1
if self.invincibility_frame % 10 == 0:
game.blit(running_sprite, (self.x, self.y))
elif self.action == 'jumping' or self.action == 'landing':
jumping_sprite = self.jumping_sprites[int(self.jumping_sprite_index)]
# add invincibility effect when hurt
if self.invincibility_frame > 0:
self.invincibility_frame -= 1
if self.invincibility_frame % 10 == 0:
game.blit(jumping_sprite, (self.x, self.y))
def update(self):
''' update the sprite index so the next sprite image is drawn '''
''' also update the y position when jumping or landing '''
if self.action == 'running':
# increment the sprite index by 0.2
# so it takes 5 frames to get to the next index
self.running_sprite_index += 0.2
# go back to index 0 after the last sprite image is drawn
if self.running_sprite_index >= len(self.running_sprites):
self.running_sprite_index = 0
# update the rect
self.rect = self.running_sprites[int(self.running_sprite_index)].get_rect()
self.rect.x = self.x
self.rect.y = self.y
# update the mask for collision detection
self.mask = pygame.mask.from_surface(self.running_sprites[int(self.running_sprite_index)])
elif self.action == 'jumping' or self.action == 'landing':
# increment the sprite index by 0.2
# so it takes 5 frames to get to the next index
self.jumping_sprite_index += 0.2
# go back to index 0 after the last sprite image is drawn
if self.jumping_sprite_index >= len(self.jumping_sprites):
self.jumping_sprite_index = 0
# move position up if jumping or down if landing
if self.action == 'jumping':
self.y -= 2
# change to landing when peak of jump is reached
if self.y <= game_height - self.height * 1.5:
self.action = 'landing'
elif self.action == 'landing':
self.y += 2
# change to running when character touches the ground
if self.y == game_height - self.height:
self.action = 'running'
# update the rect
self.rect = self.jumping_sprites[int(self.jumping_sprite_index)].get_rect()
self.rect.x = self.x
self.rect.y = self.y
# update the mask for collision detection
self.mask = pygame.mask.from_surface(self.jumping_sprites[int(self.jumping_sprite_index)])
def jump(self):
''' make the player go to jumping action when not already jumping or landing '''
if self.action not in ['jumping', 'landing']:
self.action = 'jumping'
class Obstacle(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# load images used for the obstacles
self.obstacle_images = []
for image_name in ['rock1', 'rock2', 'rock3', 'spikes']:
image = pygame.image.load(f'images/obstacles/{image_name}.png').convert_alpha()
scale = 50 / image.get_width()
new_width = image.get_width() * scale
new_height = image.get_height() * scale
image = pygame.transform.scale(image, (new_width, new_height))
self.obstacle_images.append(image)
# assign a random image
self.image = random.choice(self.obstacle_images)
# position the obstacle just off the right side of the screen
self.x = game_width
self.y = game_height - self.image.get_height()
# set the initial rect
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
def draw(self):
game.blit(self.image, (self.x, self.y))
def update(self):
''' move obstacle to the left '''
# move left
self.x -= speed
# update the rect
self.rect = self.image.get_rect()
self.rect.x = self.x
self.rect.y = self.y
# update the mask for collision detection
self.mask = pygame.mask.from_surface(self.image)
def reset(self):
''' assign a new image and reset back to original position '''
self.image = random.choice(self.obstacle_images)
self.x = game_width
self.y = game_height - self.image.get_height()
# set the image for the sky
sky = pygame.image.load('images/bg/sky.png').convert_alpha()
num_bg_tiles = math.ceil(game_width / sky.get_width()) + 1
# set the images for the parallax background
bgs = []
bgs.append(pygame.image.load('images/bg/mountains.png').convert_alpha())
bgs.append(pygame.image.load('images/bg/trees.png').convert_alpha())
bgs.append(pygame.image.load('images/bg/bushes.png').convert_alpha())
# for the parallax effect, determine how much each layer will scroll
parallax = []
for x in range(len(bgs)):
parallax.append(0)
# create the player
player = Player()
# create the obstacle
obstacles_group = pygame.sprite.Group()
obstacle = Obstacle()
obstacles_group.add(obstacle)
# load the heart images for representing health
heart_sprites = []
heart_sprite_index = 0
for i in range(8):
heart_sprite = pygame.image.load(f'images/heart/heart{i}.png').convert_alpha()
scale = 30 / heart_sprite.get_height()
new_width = heart_sprite.get_width() * scale
new_height = heart_sprite.get_height() * scale
heart_sprite = pygame.transform.scale(heart_sprite, (new_width, new_height))
heart_sprites.append(heart_sprite)
# game loop
clock = pygame.time.Clock()
fps = 90
quit = False
while not quit:
clock.tick(fps)
for event in pygame.event.get():
if event.type == QUIT:
quit = True
# press SPACE to jump
if event.type == KEYDOWN and event.key == K_SPACE:
player.jump()
# draw the sky
for i in range(num_bg_tiles):
game.blit(sky, (i * sky.get_width(), 0))
# draw each background layer
for i in range(len(bgs)):
bg = bgs[i]
for j in range(num_bg_tiles):
game.blit(bg, (j * bg.get_width() + parallax[i], 0))
# update how much each layer will scroll
for i in range(len(parallax)):
# top layer should scroll faster
parallax[i] -= i + 1
if abs(parallax[i]) > bgs[i].get_width():
parallax[i] = 0
# draw the player
player.draw()
# update the sprite and position of the player
player.update()
# draw the obstacle
obstacle.draw()
# update the position of the obstacle
obstacle.update()
# add to score and reset the obstacle when it goes off screen
if obstacle.x < obstacle.image.get_width() * -1:
score += 1
obstacle.reset()
# increase the speed after clearing 2 obstacles
# but the max it can go up to is 10
if score % 2 == 0 and speed < 10:
speed += 1
# check if player collides with the obstacle
if pygame.sprite.spritecollide(player, obstacles_group, True, pygame.sprite.collide_mask):
player.health -= 1
player.invincibility_frame = 30
# remove obstacle and replace with a new one
obstacles_group.remove(obstacle)
obstacle = Obstacle()
obstacles_group.add(obstacle)
# display a heart per remaining health
for life in range(player.health):
heart_sprite = heart_sprites[int(heart_sprite_index)]
x_pos = 10 + life * (heart_sprite.get_width() + 10)
y_pos = 10
game.blit(heart_sprite, (x_pos, y_pos))
# increment the index for the next heart sprite
# use 0.1 to make the sprite change after 10 frames
heart_sprite_index += 0.1
# set index back to 0 after the last heart sprite is drawn
if heart_sprite_index >= len(heart_sprites):
heart_sprite_index = 0
# display the score
black = (0, 0, 0)
font = pygame.font.Font(pygame.font.get_default_font(), 16)
text = font.render(f'Score: {score}', True, black)
text_rect = text.get_rect()
text_rect.center = (game_width - 50, 20)
game.blit(text, text_rect)
pygame.display.update()
# gameover
gameover = player.health == 0
while gameover and not quit:
# display game over message
red = (255, 0, 0)
pygame.draw.rect(game, red, (0, 50, game_width, 100))
font = pygame.font.Font(pygame.font.get_default_font(), 16)
text = font.render('Game over. Play again? (Enter Y or N)', True, black)
text_rect = text.get_rect()
text_rect.center = (game_width / 2, 100)
game.blit(text, text_rect)
for event in pygame.event.get():
if event.type == QUIT:
quit = True
# get the player's input (Y or N)
if event.type == KEYDOWN:
if event.key == K_y:
# reset the game
gameover = False
speed = 3
score = 0
player = Player()
obstacle = Obstacle()
obstacles_group.empty()
obstacles_group.add(obstacle)
elif event.key == K_n:
quit = True
pygame.display.update()
pygame.quit()