-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
261 lines (199 loc) · 6.49 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
""" imports """
from ursina import (Audio,
Entity,
Sprite,
Text,
Ursina,
application,
camera,
load_texture,
mouse,
window,
random)
from player import Player # pylint: disable=import-error
from mainmenu import MainMenu # pylint: disable=import-error
from obstacles import (Pickup, Sedan, Truck, HealthOrb, SpeedOrb) # pylint: disable=import-error
from endmenu import Endmenu # pylint: disable=import-error
app = Ursina()
def setup_window():
"""setup window"""
window.title = "F1Runner"
camera.orthographic = True
mouse.locked = False
camera.fov = 50
window.fullscreen = False
window.borderless = False
window.exit_button.visible = False
window.fps_counter.enabled = False
window.vsync = 60
setup_window()
ROADS_LENGTH = 28.7
LEFT_BOUND = -5.5
RIGHT_BOUND = 5.5
MAX_CARS = 30
MAX_ORBS = 2
INITIAL_AMOUNT_ROADS = 4
INITIAL_Y = -20
CAMERA_OFFSET = 5
HEART_TEXTURE = load_texture('resources/sprites/heart.png')
ROAD_TEXTURE = load_texture('resources/sprites/road_1.png')
GRASS_L = load_texture('resources/sprites/grass_left.png')
GRASS_R = load_texture('resources/sprites/grass_right.png')
START_SOUND = Audio('/resources/sounds/car_starting_idle.wav', loop=False, autoplay=False)
roads = []
grass = []
orbs = []
hearts = []
cars = []
def new_cars(x_coord, y_coord):
"""spawn new cars"""
if random.randint(0, 1) == 0:
new = Pickup(x_coord, y_coord)
elif random.randint(0, 1) == 0:
new = Sedan(x_coord, y_coord)
else:
new = Truck(x_coord, y_coord)
cars.append(new)
def new_grass(y_coord):
"""spawn grass"""
new = Sprite(name="grass", model='quad',
texture=GRASS_L, scale=1.5, x=-5.5, y=y_coord, z=0)
grass.append(new)
new = Sprite(name="grass", model='quad',
texture=GRASS_R, scale=1.5, x=5.5, y=y_coord, z=0)
grass.append(new)
def new_road(y):
"""spawn new road"""
new = Sprite(name="road", model='quad',
texture=ROAD_TEXTURE, scale=1.5, x=0, y=y, z=0)
roads.append(new)
def remove_road(y_coord):
"""remove road"""
iter_road = roads
for road in iter_road:
if road.y <= y_coord:
roads.remove(road)
break
def remove_car(y_coord):
"""remove car"""
iter_car = cars
for car in iter_car:
if car.y <= y_coord:
cars.remove(car)
break
def remove_orb(y_coord):
"""remove orb"""
iter_orb = orbs
for orb in iter_orb:
if orb.y <= y_coord:
orbs.remove(orb)
break
def remove_grass(y_coord):
"""remove grass"""
iter_grass = grass
for gras in iter_grass:
if gras.y <= y_coord:
grass.remove(gras)
break
def spawn_orbs(x_coord, y_coord):
"""spawn orbs"""
if random.randint(0, 1) == 0:
new = HealthOrb(x_coord, y_coord)
orbs.append(new)
else:
new = SpeedOrb(x_coord, y_coord)
orbs.append(new)
def initial_spawn(roads_amount):
"""create initial road & grass sprites"""
for _ in range(0, roads_amount):
spawn_y = INITIAL_Y
new_road(spawn_y)
spawn_y += ROADS_LENGTH
initial_spawn(INITIAL_AMOUNT_ROADS)
player = Player()
mainmenu = MainMenu(player, False)
def clean():
"""cleaning memory"""
if player.y >= roads[0].y + ROADS_LENGTH:
remove_road(roads[0].y + ROADS_LENGTH)
remove_car(roads[0].y + ROADS_LENGTH)
remove_orb(roads[0].y + ROADS_LENGTH)
def general_spawn():
"""spawning objects before player reach end of screen + score"""
if player.y >= roads[-1].y - ROADS_LENGTH - (player.max_speed + player.speed):
new_road(roads[-1].y + ROADS_LENGTH)
if len(cars) < MAX_CARS:
new_cars(random.uniform(LEFT_BOUND, RIGHT_BOUND), roads[-1].y + ROADS_LENGTH)
new_grass(roads[-1].y + ROADS_LENGTH)
player.score += 1
def orbs_spawn():
"""spawn orbs"""
if player.y >= roads[-1].y - ROADS_LENGTH - random_distance_to_orb() and len(orbs) < MAX_ORBS:
spawn_orbs(random.uniform(LEFT_BOUND, RIGHT_BOUND),
roads[-1].y + ROADS_LENGTH + random_distance_to_orb())
def random_distance_to_orb():
"""random distance to orb"""
distance_to_orb = random.uniform(ROADS_LENGTH * 2, 30 * ROADS_LENGTH)
return distance_to_orb
def spawn_hearts():
"""spawn hearts"""
for i in range(player.maxlife):
heart = Sprite(texture=HEART_TEXTURE, scale=0.3, x=(-0.80 + (i/10)), y=0.40,
parent=camera.ui, always_on_top=True)
hearts.append(heart)
if player.enabled is False:
START_SOUND.play()
def updating_hearts():
"""updating hearts"""
if player.life != player.maxlife:
for i in range(player.life + 1, player.maxlife):
hearts[i].visible = True
for i in range(player.life, player.maxlife):
hearts[i].visible = False
def create_texts():
"""creating speed and score texts"""
speed = Text(size=0.05, x=(-0.85), y=0.30)
score = Text(size=0.05, x=(-0.85), y=0.20)
return speed, score
spawn_hearts()
speed_text, score_text = create_texts()
def move_camera(offset_y):
"""move camera"""
camera.y = player.y + offset_y
def updating_texts():
"""updating texts"""
speed_text.text = "Speed: " + str(int(player.speed * 350)) + "km/h"
score_text.text = "Score: " + str(player.score)
def game_cycle():
"""game cycle"""
move_camera(CAMERA_OFFSET)
general_spawn()
orbs_spawn()
updating_hearts()
updating_texts()
clean()
def game_over():
"""game over"""
application.pause()
Endmenu(player)
player.engine_sound.stop()
mouse.locked = False
def update():
"""updating game state"""
if player.is_alive and player.enabled is True and mainmenu.is_game_started is True:
START_SOUND.pause()
game_cycle()
elif player.is_alive is False:
game_over()
def pause_handler_input(key):
"""pause/unpause the game"""
if key == "escape" and not application.paused and player.enabled and player.is_alive:
resumemenu = MainMenu(player, True)
resumemenu.main_menu.enabled = True
player.stop_engine()
mouse.locked = False
START_SOUND.pause()
application.pause()
pause_handler = Entity(ignore_paused=True)
pause_handler.input = pause_handler_input
app.run()