forked from superiorshrimp/quick_racing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
205 lines (162 loc) · 7.7 KB
/
engine.py
File metadata and controls
205 lines (162 loc) · 7.7 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
from __future__ import annotations
import random
import pygame as pg
from back_elements.vector2d import Vector2D
from back_elements.car import Car
from back_elements.map import Map
from back_elements.stopwatch import Stopwatch
from back_elements.booster import Booster
from enums_and_parser.boosterType import BoosterType
from enums_and_parser.CSVParser import CSVParser
class Results:
def __init__(self, x, y, width, height, background_color, color, font, button_font):
self.x = x
self.y = y
self.width = width
self.height = height
self.background_color = background_color
self.color = color
self.font = font
self.button_font = button_font
self.background_rect = pg.Rect(x, y, width, height)
self.finished_rect = pg.Rect(x + 10, y + 10, width - 20, 40)
self.return_to_menu_rect = pg.Rect(x + 10, y + height - 30, width // 2 - 20, 25)
self.restart_rect = pg.Rect(x + width // 2 + 10, y + height - 30, width // 2 - 20, 25)
self.return_text_rect = pg.Rect(x + 10, y + height - 30, width // 2 - 20, 25)
self.restart_text_rect = pg.Rect(x + width // 2 + 10, y + height - 30, width // 2 - 20, 25)
def draw(self, surf, times, pressed):
pg.draw.rect(surf, self.background_color, self.background_rect)
pg.draw.rect(surf, self.color, self.finished_rect)
finished = self.font.render("FINISHED!", 1, (0, 0, 0))
surf.blit(finished, finished.get_rect(center=self.finished_rect.center))
results = [pg.Rect(self.x + 10, self.y + 60 + 50 * i, self.width - 20, 40) for i in range(6)]
for i in range(6):
t = self.font.render(times[i], 1, (0, 0, 0))
surf.blit(t, t.get_rect(center=results[i].center))
return_button = pg.draw.rect(surf, self.color, self.return_to_menu_rect)
restart_button = pg.draw.rect(surf, self.color, self.restart_rect)
return_text = self.button_font.render("To menu", 1, (0, 0, 0))
restart_text = self.button_font.render("Restart", 1, (0, 0, 0))
surf.blit(return_text, return_text.get_rect(center=self.return_text_rect.center))
surf.blit(restart_text, restart_text.get_rect(center=self.restart_text_rect.center))
if return_button.collidepoint(pg.mouse.get_pos()):
return_color = (240, 230, 140)
if pressed:
return 0
else:
return_color = (100, 200, 255)
if restart_button.collidepoint(pg.mouse.get_pos()):
restart_color = (240, 230, 140)
if pressed:
return 1
else:
restart_color = (100, 200, 255)
pg.draw.rect(return_text, return_color, [self.x + 10, self.y + self.height - 30, self.width // 2 - 20, 25])
pg.draw.rect(restart_text, restart_color,
[self.x + self.width // 2 + 10, self.y + self.height - 30, self.width // 2 - 20, 25])
return 2
class NitroBar():
def __init__(self, x, y, width, heigth, bg_color, color, font):
self.x = x
self.y = y
self.width = width
self.height = heigth
self.background_color = bg_color
self.color = color
self.background_rect = pg.Rect(x, y, width, heigth)
self.font = font
def draw(self, surf, val, cap):
pg.draw.rect(surf, self.background_color, self.background_rect)
rect = pg.Rect(self.x + 5, self.y + 5, int((self.width - 10) * max(val / cap, 0)), self.height - 10)
pg.draw.rect(surf, self.color, rect)
nitro_text = self.font.render("NITRO", 1, (0, 0, 0))
surf.blit(nitro_text, nitro_text.get_rect(center=self.background_rect.center))
class Engine:
def __init__(self, refresh_rate, name, car, map):
self.refresh = refresh_rate
pg.init()
self.screen = pg.display.set_mode((1480, 780))
pg.display.update()
pg.display.set_caption("QUICK RACING")
self.clock = pg.time.Clock()
self.player_name = name
self.car = car
self.map = map
self.nitro_bar = NitroBar(5, 5, 250, 40, (240, 230, 140), (100, 100, 255), pg.font.SysFont('Calibri', 35))
self.results_popup = Results(1480 // 2 - 300 // 2, 780 // 2 - 400 // 2, 300, 400, (240, 230, 140),
(100, 100, 255), pg.font.SysFont('Calibri', 35), pg.font.SysFont('Calibri', 25))
def spawn_booster(self, map: Map, dt):
if random.randrange(0, 256) != 8:
return
place = random.randrange(0, len(map.places_for_boosters) - 1)
x_coordinate = random.randrange(map.places_for_boosters[place][0], map.places_for_boosters[place][2])
y_coordinate = random.randrange(map.places_for_boosters[place][1], map.places_for_boosters[place][3])
what_booster = random.randrange(0, 6)
new_booster_type = None
change = None
if what_booster == 0:
new_booster_type = BoosterType.SPEED
change = random.uniform(-0.95, .5)
elif what_booster == 1:
new_booster_type = BoosterType.TURNING
change = random.randrange(-3, 3)
elif what_booster == 2:
new_booster_type = BoosterType.NO_COLLISIONS
change = 1
elif what_booster == 3:
new_booster_type = BoosterType.DECREASE_TIMER
change = 1
elif what_booster == 4:
new_booster_type = BoosterType.NO_TURNING
change = 1
elif what_booster == 5:
new_booster_type = BoosterType.FREEZE
change = 1
map.all_boosters.add(Booster(Vector2D(x_coordinate, y_coordinate), change, new_booster_type, dt))
def display_laps(self, map):
out = 'laps {lp}/6'.format(lp=map.laps_completed)
map.font.render_to(self.screen, (1100, 40), out, pg.Color('black'))
def start_timer(self):
stopwatch = Stopwatch(self.screen, self.clock, Vector2D(1300, 40))
stopwatch.restart_timer(pg.time.get_ticks())
return stopwatch
def run(self):
x, y = self.screen.get_size()
run = True
stopwatch = self.start_timer()
current_map = Map(self.map, x, y, stopwatch, self.player_name)
map_img = pg.image.load("./data/grass.png")
current_map.place_objects()
id, name, engine = CSVParser(None, None, "./data/Cars.csv").read_car_statistics(self.car)
car = Car(id, Vector2D(50, 100), 0, 0, 10, engine, name, current_map)
while run:
dt = self.clock.tick(self.refresh)
self.screen.blit(map_img, (0, 0))
current_map.all_surfaces.draw(self.screen)
current_map.all_surfaces.update()
current_map.all_walls.draw(self.screen)
current_map.all_walls.update()
self.spawn_booster(current_map, dt)
current_map.all_boosters.draw(self.screen)
current_map.all_boosters.update()
self.nitro_bar.draw(self.screen, car.nitro_duration, car.nitro_capacity)
car.move(dt)
car.update(dt)
self.screen.blit(car.image, (car.position.x, car.position.y))
self.display_laps(current_map)
pressed = False
for event in pg.event.get():
if event.type == pg.QUIT:
run = False
elif event.type == pg.MOUSEBUTTONDOWN:
pressed = True
ticks = pg.time.get_ticks()
stopwatch.display_timer(ticks)
if current_map.won == 1:
r = self.results_popup.draw(self.screen, current_map.times, pressed)
if r == 0:
run = False
elif r == 1:
self.run()
pg.display.flip()
pg.display.set_mode((1080, 720))