-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
135 lines (100 loc) · 3.33 KB
/
functions.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
import os
import sys
import pygame
from dotenv import load_dotenv
from classes.fuel import Fuel
from classes.enemy import Enemy
from classes.player import Player
from sounds import explosionSound, collectFuelSound
load_dotenv()
DISPLAY_WIDTH = int(os.environ.get('DISPLAY_WIDTH', 1200))
DISPLAY_HEIGHT = int(os.environ.get('DISPLAY_HEIGHT', 800))
QTY_ENEMIES = int(os.environ.get('QTY_ENEMIES', 5))
QTY_FUEL = int(os.environ.get('QTY_FUEL', 1))
def startEngine():
pygame.init()
screen = pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
pygame.display.set_caption('Road Fighter')
clock = pygame.time.Clock()
return screen, clock
def startTexts():
pygame.font.init()
texts = pygame.font.Font('sprites/font.ttf', 30)
return texts
def loadPlayerGroup():
player = Player(400, 600, 5)
playerGroup = pygame.sprite.Group()
playerGroup.add(player)
return playerGroup
def loadEnemiesGroup():
enemiesGroup = pygame.sprite.Group()
for _ in range(QTY_ENEMIES):
enemiesGroup.add(Enemy())
return enemiesGroup
def loadFuelsGroup():
fuelsGroup = pygame.sprite.Group()
for _ in range(QTY_FUEL):
fuelsGroup.add(Fuel())
return fuelsGroup
def catchEvents():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
def displayScreen(screen, image):
width = image.get_width()
screen.blit(image, (width*0.25, 0))
def catchControllerEvents(road, playerSprite, enemiesGroup, fuelsGroup):
keys = pygame.key.get_pressed()
playPressed = False
accelerated = False
if keys[pygame.K_ESCAPE]:
pygame.quit()
sys.exit()
if keys[pygame.K_RETURN]:
playPressed = True
if keys[pygame.K_LEFT]:
playerSprite.update("left")
if keys[pygame.K_RIGHT]:
playerSprite.update("right")
if keys[pygame.K_z]:
road.update(20)
enemiesGroup.update(4)
fuelsGroup.update(4)
accelerated = True
else:
enemiesGroup.update(-5)
fuelsGroup.update(-5)
return playPressed, accelerated
def catchCollisions(playerSprite, enemiesGroup):
collision = pygame.sprite.spritecollide(playerSprite, enemiesGroup, False)
if collision:
explosionSound.play()
return collision
def catchFuel(playerSprite, fuelsGroup):
moreFuel = pygame.sprite.spritecollide(playerSprite, fuelsGroup, False)
if moreFuel:
collectFuelSound.play()
for fuel in fuelsGroup.sprites():
fuel.posY = -2500
return moreFuel
def drawHUD(screen, texts, distance, fuel):
fuel = int(fuel)
title = texts.render('ROAD FIGTHER', False, (255, 255, 255))
distanceText1 = texts.render('DISTANCE:', False, (255, 255, 255))
distanceText2 = texts.render(str(distance), False, (255, 255, 255))
fuelText1 = texts.render('FUEL:', False, (255, 255, 255))
fuelText2 = texts.render(str(fuel), False, (255, 255, 255))
margin = DISPLAY_WIDTH * 0.683
screen.blit(title, (margin, 100))
screen.blit(distanceText1, (margin, 300))
screen.blit(distanceText2, (margin, 350))
screen.blit(fuelText1, (margin, 500))
screen.blit(fuelText2, (margin, 550))
def addFuel(fuel, moreFuel):
if moreFuel:
if fuel < 100:
fuel += 30
if fuel > 100:
fuel = 100
return fuel