-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
106 lines (85 loc) · 3.16 KB
/
game.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
import pygame
from time import sleep
from functions import drawHUD, displayScreen, addFuel
from functions import catchEvents, catchControllerEvents, catchCollisions, catchFuel
from sounds import startingMusic, endingMusic, emptyFuelSound, motorIdle, motorAccelerated, stopAllSounds
def runGame(screen, clock, texts, playerGroup, enemiesGroup, fuelsGroup, road):
# Player sprite is the only sprite from playerGroup
playerSprite = playerGroup.sprites()[0]
# Incialize variables
gameRunning = False
gameOver = False
moreFuel = False
distance = 0
fuel = 100
# Screens
screenStart = pygame.image.load('screens/start.png').convert()
screenEnd = pygame.image.load('screens/end.png').convert()
# Playing sounds control
playingFuelSound = False
playingMotorIdleSound = False
playingMotorAcceleratedSound = False
playingEndingSound = False
startingMusic.play()
while True:
# Frame inicialization
screen.fill((0, 0, 0))
clock.tick(60)
# Events and controller
catchEvents()
playPressed, accelerated = catchControllerEvents(road, playerSprite, enemiesGroup, fuelsGroup)
# Start
if playPressed and not gameRunning:
gameRunning = True
# Main menu
if not gameRunning:
displayScreen(screen, screenStart)
# Game over
if gameOver:
sleep(0.3)
displayScreen(screen, screenEnd)
if not playingEndingSound:
stopAllSounds()
endingMusic.play()
playingEndingSound = True
# Gameplay
if gameRunning and not gameOver:
drawHUD(screen, texts, distance, fuel)
# In Acceleration
if accelerated:
distance += 1
fuel -= 0.05
# Motor sound accelerated
if not playingMotorAcceleratedSound:
motorIdle.stop()
playingMotorIdleSound = False
motorAccelerated.play()
playingMotorAcceleratedSound = True
# Motor sound idle
if not playingMotorIdleSound and not accelerated:
motorAccelerated.stop()
playingMotorAcceleratedSound = False
motorIdle.play()
playingMotorIdleSound = True
# Draw objects
road.draw()
fuelsGroup.draw(screen)
enemiesGroup.draw(screen)
playerGroup.draw(screen)
# Game collisions
gameOver = catchCollisions(playerSprite, enemiesGroup)
moreFuel = catchFuel(playerSprite, fuelsGroup)
# Add fuel logic
fuel = addFuel(fuel, moreFuel)
# Endgame if fuel is empty
if fuel <= 0:
gameOver = True
# Fuel sound warning
if fuel <= 20 and not playingFuelSound:
emptyFuelSound.play()
playingFuelSound = True
# Stop fuel sound warning
if fuel > 20:
emptyFuelSound.stop()
playingFuelSound = False
pygame.display.flip()