-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
621 lines (534 loc) · 18.5 KB
/
main.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
import pygame, os, random, math
pygame.init()
WINDOW = pygame.display.set_mode((900, 500))
pygame.display.set_caption("Escape Nickel")
FPS = 60
RED_GRAY = (112, 92, 89)
LIGHT_RED_GRAY = (156, 134, 131)
BLACK = (0, 0, 0)
LIGHT_BLUE = (80, 196, 222)
GREEN = (10, 171, 66)
DESCRIPTION_FONT = pygame.font.SysFont("couriernew", 20, True)
CONTROLS_FONT = pygame.font.SysFont("couriernew", 16, True)
ASSETSPATH = os.path.abspath(os.getcwd()) + "/Assets/"
class Ground:
speed = 7
def __init__(self, x):
self.picture = pygame.image.load(ASSETSPATH + "ground.png")
self.x = x
self.y = WINDOW.get_height() - self.picture.get_height()
self.width = self.picture.get_width()
self.height = self.picture.get_height()
def move(self):
self.x -= self.speed
def draw(self):
WINDOW.blit(self.picture, (self.x, self.y))
def behave(self):
self.move()
self.draw()
class Obstacle:
scale = 0.07
def __init__(self, x):
self.rawPicture = pygame.image.load(ASSETSPATH + "obstacle.png")
if random.randint(1, 2) == 1:
self.picture = pygame.transform.scale(
self.rawPicture,
(
int(self.rawPicture.get_width() * self.scale),
int(self.rawPicture.get_height() * (self.scale)),
),
)
else:
self.picture = pygame.transform.scale(
self.rawPicture,
(
int(self.rawPicture.get_width() * self.scale),
int(self.rawPicture.get_height() * (self.scale + 0.05)),
),
)
self.x = x
self.y = WINDOW.get_height() - 160 - self.picture.get_height()
self.width = self.picture.get_width()
self.height = self.picture.get_height()
self.speed = Ground.speed
def move(self):
self.x -= self.speed
def draw(self):
WINDOW.blit(self.picture, (self.x, self.y))
def behave(self):
self.move()
self.draw()
class Bullet:
vx = 4
scale = 0.04
def __init__(self, x, y):
self.rawPicture = pygame.image.load(ASSETSPATH + "bullet.png")
self.scaledPicture = pygame.transform.scale(
self.rawPicture,
(
int(self.rawPicture.get_width() * self.scale),
int(self.rawPicture.get_height() * self.scale),
),
)
self.x = x
self.y = y
self.width = self.scaledPicture.get_width()
self.height = self.scaledPicture.get_height()
self.vy = random.randint(-3, 3)
if self.vy == 1:
self.vy = 2
if self.vy == -3:
self.vy = 0
self.picture = pygame.transform.rotate(
self.scaledPicture, (math.atan(self.vy / self.vx) * 180) / math.pi
)
def move(self):
self.x += self.vx
self.y -= self.vy
def draw(self):
WINDOW.blit(self.picture, (self.x, self.y))
def behave(self):
self.move()
self.draw()
class GuidedBullet(Bullet):
def __init__(self, x, y):
super().__init__(x, y)
self.vx = 2
self.vy = 0
self.picture = self.scaledPicture
def track(self, target):
self.vy = target.vy
self.y = target.y
self.picture = pygame.transform.rotate(
self.scaledPicture, (math.atan(self.vy / self.vx) * 180) / math.pi
)
def behave(self, target):
self.track(target)
self.move()
self.draw()
class Car:
scale = 0.17
def __init__(self, sessionStartTime):
self.pictures = []
self.bullets = []
for i in range(1, 7):
rawPicture = pygame.image.load(ASSETSPATH + "car00" + str(i) + ".png")
picture = pygame.transform.scale(
rawPicture,
(
int(rawPicture.get_width() * self.scale),
int(rawPicture.get_height() * self.scale),
),
)
self.pictures.append(picture)
self.width = self.pictures[0].get_width()
self.height = self.pictures[0].get_height()
self.x = 15
self.y = WINDOW.get_height() - 160 - self.height
self.guidedBullet = GuidedBullet(self.x + 135, self.y + 50)
self.lastUpdateTime = 0
self.pictureUpdatePeriod = 70 # Unit: ms
self.frame = 0
self.lastFiringTime = sessionStartTime
self.reloadTime = 3000 # Unit: ms
def getBullets(self):
return self.bullets
def fire(self):
currentTime = pygame.time.get_ticks()
if currentTime - self.lastFiringTime >= self.reloadTime:
numBullets = random.randint(1, 3)
for i in range(numBullets):
self.bullets.append(Bullet(self.x + 155, self.y + 50))
self.lastFiringTime = currentTime
def killElwood(self, target):
self.guidedBullet.behave(target)
def draw(self):
currentTime = pygame.time.get_ticks()
if currentTime - self.lastUpdateTime >= self.pictureUpdatePeriod:
if self.frame == 5:
self.frame = 0
else:
self.frame += 1
self.lastUpdateTime = currentTime
WINDOW.blit(self.pictures[self.frame], (self.x, self.y))
def behave(self):
self.fire()
self.draw()
for bullet in self.bullets:
bullet.behave()
self.bullets = [i for i in self.bullets if not i.x > WINDOW.get_width()]
class Character:
scale = 0.2
def __init__(self, name):
self.pictures = []
self.name = name
try:
if self.name == "Elwood":
fileNameStart = "elwood00"
self.x = WINDOW.get_width() - (WINDOW.get_width() // 4)
elif self.name == "Turner":
fileNameStart = "turner00"
self.x = WINDOW.get_width() - (WINDOW.get_width() // 4) - 40
else:
raise FileNotFoundError
except FileNotFoundError:
print("ERROR: File doesn't exist")
for i in range(1, 7):
rawPicture = pygame.image.load(ASSETSPATH + fileNameStart + str(i) + ".png")
picture = pygame.transform.scale(
rawPicture,
(
int(rawPicture.get_width() * self.scale),
int(rawPicture.get_height() * self.scale),
),
)
self.pictures.append(picture)
self.width = self.pictures[0].get_width()
self.height = self.pictures[0].get_height()
self.y = WINDOW.get_height() - 160 - self.height
self.groundHeight = self.y
self.vx = 3
self.vy = 0
self.ay = 0.5
self.canJump = True
self.lastUpdateTime = 0
self.pictureUpdatePeriod = 70 # Unit: ms
self.frame = 0
def jump(self):
if not self.canJump:
self.y -= self.vy
self.vy -= self.ay
if self.y >= self.groundHeight:
self.y = self.groundHeight
self.canJump = True
self.vy = 0
def move(self, keys, obstacles):
if self.name == "Elwood":
if keys[pygame.K_RIGHT]:
if (
self.x + self.width < WINDOW.get_width()
and not self.collideWithObstacles(obstacles)
):
self.x += self.vx
if keys[pygame.K_LEFT]:
self.x -= self.vx
if keys[pygame.K_UP] and self.canJump:
self.vy = 11
self.canJump = False
else:
if keys[pygame.K_d]:
if (
self.x + self.width < WINDOW.get_width()
and not self.collideWithObstacles(obstacles)
):
self.x += self.vx
if keys[pygame.K_a]:
self.x -= self.vx
if keys[pygame.K_w] and self.canJump:
self.vy = 11
self.canJump = False
self.jump()
def collidesWith(self, object):
if self.x <= object.x:
left = self
right = object
else:
left = object
right = self
return (
(
(
left.x + left.width > right.x
and left.x + left.width < right.x + right.width
)
and (
(left.y > right.y and left.y < right.y + right.height)
or (
left.y + left.height > right.y
and left.y + left.height < right.y + right.height
)
)
)
or (
(left.x < right.x and left.x + left.width > right.x + right.width)
and (
(left.y > right.y and left.y < right.y + right.height)
or (
left.y + left.height > right.y
and left.y + left.height < right.y + right.height
)
)
)
or (
left.x + left.width > right.x + right.width
and left.y < right.y
and left.y + left.height > right.y + right.height
)
)
def collideWithObstacles(self, obstacles):
hasCollision = False
for obstacle in obstacles:
if self.collidesWith(obstacle):
self.x -= Ground.speed
hasCollision = True
return hasCollision
def draw(self):
currentTime = pygame.time.get_ticks()
if currentTime - self.lastUpdateTime >= self.pictureUpdatePeriod:
if self.frame == 5:
self.frame = 0
else:
self.frame += 1
self.lastUpdateTime = currentTime
WINDOW.blit(self.pictures[self.frame], (self.x, self.y))
def behave(self, keys, obstacles):
self.move(keys, obstacles)
self.collideWithObstacles(obstacles)
self.draw()
class Button:
def __init__(self, pictureName, x, y, scale):
self.rawPicture = pygame.image.load(ASSETSPATH + pictureName)
self.picture = pygame.transform.scale(
self.rawPicture,
(
int(self.rawPicture.get_width() * scale),
int(self.rawPicture.get_height() * scale),
),
)
self.buttonRectangle = self.picture.get_rect()
self.buttonRectangle.center = (x, y) # the coordinate is about the center
self.clicked = False
def draw(self):
WINDOW.blit(self.picture, (self.buttonRectangle.x, self.buttonRectangle.y))
def isClicked(self):
mousePos = pygame.mouse.get_pos()
if self.buttonRectangle.collidepoint(mousePos):
if pygame.mouse.get_pressed()[0] == 1 and not self.clicked:
self.clicked = True
return True
if pygame.mouse.get_pressed()[0] == 0:
self.clicked = False
return False
ground = []
obstacles = [Obstacle(WINDOW.get_width() + int(0.5 * WINDOW.get_width()))]
elwood = Character("Elwood")
turner = Character("Turner")
car = Car(0)
playButton = Button(
"play_button.png",
(3 * WINDOW.get_width()) // 4 - 140,
WINDOW.get_height() - (WINDOW.get_height() // 6),
0.3,
)
playAgainButton = Button(
"play_again_button.png",
WINDOW.get_width() // 4 + 40,
WINDOW.get_height() // 2 + WINDOW.get_height() // 4,
0.3,
)
quitButton = Button(
"quit_button.png",
(3 * WINDOW.get_width()) // 4 - 40,
WINDOW.get_height() // 2 + WINDOW.get_height() // 4,
0.3,
)
startScreenPicture = pygame.image.load(ASSETSPATH + "start_screen_background.png")
deathScreenText = pygame.image.load(ASSETSPATH + "you_died.png")
rawFailScreenQuote = pygame.image.load(ASSETSPATH + "fail_screen_quote.png")
failScreenQuote = pygame.transform.scale(
rawFailScreenQuote,
(
int(rawFailScreenQuote.get_width() * 0.8),
int(rawFailScreenQuote.get_height() * 0.8),
),
)
def generateGround():
position = 0
for i in range(3):
ground.append(Ground(position))
position += WINDOW.get_width()
def behaveGround():
for segment in ground:
segment.behave()
if segment.x + segment.width < 0:
segment.x += len(ground) * segment.width
def spawnObstacles():
position = obstacles[-1].x
minSpacing = int(WINDOW.get_width() / 4)
maxSpacing = WINDOW.get_width() + (WINDOW.get_width() // 3)
while len(obstacles) < 4:
x = position + random.randint(minSpacing, maxSpacing)
obstacles.append(Obstacle(x))
position = x
def behaveObstacles():
for obstacle in obstacles:
obstacle.behave()
if obstacles[0].x + obstacles[0].width < 0:
obstacles.pop(0)
def behaveCharacters(keys, obstacles):
elwood.behave(keys, obstacles)
turner.behave(keys, obstacles)
def playerIsKilled():
if (
elwood.collidesWith(car)
or turner.collidesWith(car)
or elwood.collidesWith(car.guidedBullet)
or turner.collidesWith(car.guidedBullet)
):
return True
for bullet in car.getBullets():
if elwood.collidesWith(bullet) or turner.collidesWith(bullet):
return True
return False
def returnToStartScreen(keys):
return keys[pygame.K_ESCAPE]
def checkRun():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
return True
def drawGameBackground():
WINDOW.fill(RED_GRAY)
def drawDeathScreen(deathScreenText):
WINDOW.blit(
deathScreenText,
(
(WINDOW.get_width() // 2) - (deathScreenText.get_width() // 2),
(WINDOW.get_height() // 2) - (deathScreenText.get_height() // 2),
),
)
def drawFailScreen():
WINDOW.fill(LIGHT_RED_GRAY)
WINDOW.blit(
failScreenQuote,
(
(WINDOW.get_width() // 2) - (failScreenQuote.get_width() // 2),
(WINDOW.get_height() // 2) - (failScreenQuote.get_height() // 2) - 50,
),
)
playAgainButton.draw()
quitButton.draw()
def drawStartScreen():
WINDOW.blit(startScreenPicture, (0, 0))
descriptionBlock = [
DESCRIPTION_FONT.render(
"It is Elwood and Turner's last and only chance", 1, BLACK
),
DESCRIPTION_FONT.render(
"to escape the absolute hell of Nickel Academy.", 1, BLACK
),
DESCRIPTION_FONT.render(
"Now, or never. They attempt a run for freedom,", 1, BLACK
),
DESCRIPTION_FONT.render("but are pursued by Nickel staff.", 1, BLACK),
DESCRIPTION_FONT.render("", 1, BLACK),
DESCRIPTION_FONT.render("Don't get hit by their car or bullets.", 1, BLACK),
]
playerOneControlsBlock = [
CONTROLS_FONT.render(" Player 1 (blue):", 1, LIGHT_BLUE),
CONTROLS_FONT.render(
" Up arrow -> jump, right arrow -> move forward, ", 1, LIGHT_BLUE
),
CONTROLS_FONT.render(" left arrow -> move backward", 1, LIGHT_BLUE),
]
playerTwoControlsBlock = [
CONTROLS_FONT.render(" Player 2 (green):", 1, GREEN),
CONTROLS_FONT.render(
" W -> jump, D -> move forward, A -> move backward", 1, GREEN
),
]
x = WINDOW.get_width() // 2 - 140
y = WINDOW.get_height() // 4 - 50
for text in descriptionBlock:
WINDOW.blit(text, (x, y))
y += 25
y += 15
for text in playerOneControlsBlock:
WINDOW.blit(text, (x, y))
y += 20
y += 15
for text in playerTwoControlsBlock:
WINDOW.blit(text, (x, y))
y += 20
playButton.draw()
def drawEpilogueScreen():
WINDOW.fill(LIGHT_RED_GRAY)
epilogueBlock = [
DESCRIPTION_FONT.render(
"Elwood died, but his beliefs and values cannot be killed;", 1, BLACK
),
DESCRIPTION_FONT.render(
"they persist under oppression, and transcend through time;", 1, BLACK
),
DESCRIPTION_FONT.render(
"they pass to those who survived, their children,", 1, BLACK
),
DESCRIPTION_FONT.render("and the children of their children...", 1, BLACK),
]
y = (WINDOW.get_height() // 3) + 30
for text in epilogueBlock:
textRect = text.get_rect(center=(WINDOW.get_width() // 2, y))
WINDOW.blit(text, textRect)
y += 25
def resetGame(sessionStartTime):
ground.clear()
generateGround()
obstacles.clear()
obstacles.append(Obstacle(WINDOW.get_width() + int(0.5 * WINDOW.get_width())))
global elwood
elwood = Character("Elwood")
global turner
turner = Character("Turner")
global car
car = Car(sessionStartTime)
def main():
fps = pygame.time.Clock()
game = "start screen"
run = True
generateGround()
while run:
keys = pygame.key.get_pressed()
fps.tick(FPS)
run = checkRun()
if game == "start screen":
drawStartScreen()
if playButton.isClicked():
sessionStartTime = pygame.time.get_ticks()
resetGame(sessionStartTime)
game = "game screen"
if game == "game screen":
gameRunningTime = pygame.time.get_ticks()
drawGameBackground()
behaveGround()
spawnObstacles()
behaveCharacters(keys, obstacles)
behaveObstacles()
car.behave()
if gameRunningTime - sessionStartTime >= 30000:
car.killElwood(elwood)
if playerIsKilled():
deathScreenStartTime = pygame.time.get_ticks()
game = "death screen"
if returnToStartScreen(keys):
game = "start screen"
if game == "death screen":
if pygame.time.get_ticks() - deathScreenStartTime > 2500:
game = "fail screen"
drawDeathScreen(deathScreenText)
if game == "fail screen":
drawFailScreen()
if playAgainButton.isClicked():
sessionStartTime = pygame.time.get_ticks()
resetGame(sessionStartTime)
game = "game screen"
if quitButton.isClicked():
epilogueScreenStartTime = pygame.time.get_ticks()
game = "epilogue screen"
if game == "epilogue screen":
if pygame.time.get_ticks() - epilogueScreenStartTime >= 10000:
game = "start screen"
drawEpilogueScreen()
pygame.display.flip()
pygame.quit()
if __name__ == "__main__":
main()