-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
524 lines (411 loc) · 16.6 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
from math import sin, pi
import random
import time
from collections import defaultdict
from typing import List
import opensimplex
import arcade
from src.consts import *
from src.player import Player
from src.enemy_manager import EnemyManager
from src.dijsktra import PathFindingMap
from src.vec import Vec2
from src import ctx
from src.maze import Maze
from src.glow import Glow
from src.grid import Grid
from src.slider import Slider
from src.button import TextButton
from pathlib import Path
# from time import perf_counter
SCREEN_TITLE = "Run Hunt Repeat"
SCREEN_WIDTH = 1280
SCREEN_HEIGHT = 720
RATIO = SCREEN_WIDTH / SCREEN_HEIGHT
VIEWPORT_SCALE = 1
VIEWPORT_WIDTH = 32 * RATIO * VIEWPORT_SCALE
VIEWPORT_HEIGHT = 32 * VIEWPORT_SCALE
class MenuView(arcade.View):
def __init__(self, parent_view):
super().__init__()
self.parent_view = parent_view
self.volume_slider = Slider(
percent_x=0.5,
percent_y=0.6,
percent_width=0.2,
value=ctx.volume,
low=0,
high=1
)
self.btn_qwerty = TextButton(
text='qwerty',
font_size=48,
percentx=0.35,
percenty=0.3,
callback=self.btn_qwerty_callback
)
self.btn_azerty = TextButton(
text='azerty',
font_size=48,
percentx=0.65,
percenty=0.3,
callback=self.btn_azerty_callback
)
if ctx.keyboard == 'qwerty':
self.btn_azerty.text.color = arcade.color.GRAY
elif ctx.keyboard == 'azerty':
self.btn_qwerty.text.color = arcade.color.GRAY
def btn_qwerty_callback(self):
ctx.keyboard = 'qwerty'
self.btn_qwerty.text.color = arcade.color.WHITE
self.btn_azerty.text.color = arcade.color.GRAY
def btn_azerty_callback(self):
ctx.keyboard = 'azerty'
self.btn_qwerty.text.color = arcade.color.GRAY
self.btn_azerty.text.color = arcade.color.WHITE
def on_show_view(self):
arcade.set_background_color(arcade.color.BLACK)
self.window.set_mouse_visible(True)
def on_draw(self):
self.clear()
arcade.set_viewport(0, self.window.width, 0, self.window.height)
arcade.draw_text(
text="Volume",
bold=False,
font_size=24,
font_name="Bebas Neue",
start_x=self.window.width * (0.5 - 0.18),
start_y=self.window.height * 0.6,
anchor_x="center",
anchor_y="center"
)
arcade.draw_text(
text=f"{ctx.volume:.2f}",
bold=False,
font_size=24,
font_name="Bebas Neue",
start_x=self.window.width * (0.5 + 0.18),
start_y=self.window.height * 0.6,
anchor_x="center",
anchor_y="center"
)
self.volume_slider.draw()
self.btn_qwerty.draw(self.window)
self.btn_azerty.draw(self.window)
def on_update(self, dt):
self.volume_slider.update(self.window)
ctx.volume = self.volume_slider.get_value()
def on_key_press(self, key, key_modifiers):
if key == arcade.key.ESCAPE:
self.window.set_mouse_visible(False)
self.window.show_view(self.parent_view)
if key == arcade.key.F11:
self.window.set_fullscreen(
True - self.window.fullscreen
)
def on_mouse_press(self, x, y, button, modifiers):
self.volume_slider.on_mouse_press(x, y)
self.btn_qwerty.on_mouse_press(x, y)
self.btn_azerty.on_mouse_press(x, y)
def on_mouse_release(self, x, y, button, modifiers):
self.volume_slider.on_mouse_release(x, y)
def on_mouse_drag(self, x, y, dx, dy, _buttons, _modifiers):
self.volume_slider.on_mouse_drag(x, y, dx, dy)
def on_resize(self, width: int, height: int):
super().on_resize(width, height)
class StartView(arcade.View):
def __init__(self):
super().__init__()
self.time_start = time.time()
self.program = self.window.ctx.program(
vertex_shader=Path('assets/shaders/background.vs').read_text(),
fragment_shader=Path('assets/shaders/background_blue.fs').read_text()
)
self.setup()
def setup(self):
self.screen_quad = arcade.gl.geometry.quad_2d_fs()
self.program['resolution'] = self.window.size
def on_show_view(self):
arcade.set_background_color(arcade.color.BLACK)
def on_draw(self):
self.clear()
self.program['time'] = time.time() - self.time_start
self.screen_quad.render(program=self.program)
arcade.set_viewport(0, self.window.width, 0, self.window.height)
arcade.draw_text(
text="Press SPACE to start !!",
bold=True,
font_size=42,
font_name="Bebas Neue",
start_x=self.window.width/2,
start_y=self.window.height/2,
anchor_x="center",
anchor_y="center",
rotation=sin(time.time() * 3) * 10)
def on_update(self, dt):
pass
def on_key_press(self, key, key_modifiers):
if key == arcade.key.SPACE:
ctx.game = GameView()
self.window.show_view(ctx.game)
if key == arcade.key.F11:
self.window.set_fullscreen(
True - self.window.fullscreen
)
if key == arcade.key.ESCAPE:
self.window.show_view( MenuView(self) )
def on_resize(self, width: int, height: int):
super().on_resize(width, height)
self.setup()
class GameOverView(arcade.View):
def __init__(self, score):
super().__init__()
self.score = score
self.time_start = time.time()
self.program = self.window.ctx.program(
vertex_shader=Path('assets/shaders/background.vs').read_text(),
fragment_shader=Path('assets/shaders/background_red.fs').read_text()
)
self.setup()
def setup(self):
self.screen_quad = arcade.gl.geometry.quad_2d_fs()
self.program['resolution'] = self.window.size
def on_show_view(self):
arcade.set_background_color(arcade.color.BLACK)
def on_draw(self):
self.clear()
self.program['time'] = time.time() - self.time_start
self.screen_quad.render(program=self.program)
arcade.set_viewport(0, self.window.width, 0, self.window.height)
arcade.draw_text(
text=f"Your score is {int(self.score)}",
bold=True,
font_size=42,
font_name="Bebas Neue",
start_x=self.window.width/2,
start_y=self.window.height/2 + 75,
anchor_x="center",
anchor_y="center",
rotation=sin(time.time() * 3) * 10)
arcade.draw_text(
text="Press Space to restart",
bold=True,
font_size=42,
font_name="Bebas Neue",
start_x=self.window.width/2,
start_y=self.window.height/2 - 75,
anchor_x="center",
anchor_y="center",
rotation=sin((time.time()+4.789) * 3) * 10)
def on_key_press(self, key, key_modifiers):
if key == arcade.key.SPACE:
ctx.game = GameView()
self.window.show_view(ctx.game)
if key == arcade.key.F11:
self.window.set_fullscreen(
True - self.window.fullscreen
)
if key == arcade.key.ESCAPE:
self.window.show_view( MenuView(self) )
def on_resize(self, width: int, height: int):
super().on_resize(width, height)
class GameView(arcade.View):
def __init__(self):
super().__init__()
self.glow = Glow(self.window.ctx)
self.pressed = defaultdict(bool)
arcade.set_background_color(arcade.color.AMAZON)
self.camera_center = Vec2(0, 0)
self.grid = Grid(
width=GRID_WIDTH,
height=GRID_HEIGHT,
data=Maze.generate(GRID_WIDTH//2 + 1, GRID_HEIGHT//2 + 1).to_grid()
)
## remove walls inside radius at center
CENTER_HOLE_RADIUS = 1
for y in range(-CENTER_HOLE_RADIUS, CENTER_HOLE_RADIUS+1):
for x in range(-CENTER_HOLE_RADIUS, CENTER_HOLE_RADIUS+1):
i = self.grid.toI(GRID_WIDTH//2 + x, GRID_HEIGHT//2 + y)
self.grid[i] = TILE_EMPTY
## remove walls at and random and with simplex noise
t = int(time.time())
opensimplex.seed(t)
random.seed(t)
for i in range(GRID_HEIGHT * GRID_WIDTH):
y = i // GRID_WIDTH
x = i % GRID_WIDTH
# remove wall at random
if random.random() > 0.9:
self.grid[i] = TILE_EMPTY
# remove wall based on simple noise
if opensimplex.noise2(x*0.4, y*0.4) > 0.2:
self.grid[i] = TILE_EMPTY
## walls and floor
self.shape_list_map_1_wall = arcade.ShapeElementList()
self.shape_list_map_1_empty = arcade.ShapeElementList()
self.shape_list_map_2_wall = arcade.ShapeElementList()
self.shape_list_map_2_empty = arcade.ShapeElementList()
for i in range(GRID_HEIGHT * GRID_WIDTH):
y = i // GRID_WIDTH
x = i % GRID_WIDTH
if self.grid[i] == TILE_WALL:
shape_1 = arcade.create_rectangle_filled(
center_x=x*GRID_SCALE + GRID_SCALE/2,
center_y=y*GRID_SCALE + GRID_SCALE/2,
width=GRID_SCALE,
height=GRID_SCALE,
color=COLOR_DARK)
shape_2 = arcade.create_rectangle_filled(
center_x=x*GRID_SCALE + GRID_SCALE/2,
center_y=y*GRID_SCALE + GRID_SCALE/2,
width=GRID_SCALE,
height=GRID_SCALE,
color=COLOR_BRIGHT_2)
self.shape_list_map_1_wall.append(shape_1)
self.shape_list_map_2_wall.append(shape_2)
if self.grid[i] == TILE_EMPTY:
shape_1 = arcade.create_rectangle_filled(
center_x=x*GRID_SCALE + GRID_SCALE/2,
center_y=y*GRID_SCALE + GRID_SCALE/2,
width=GRID_SCALE,
height=GRID_SCALE,
color=COLOR_BRIGHT_2)
shape_2 = arcade.create_rectangle_filled(
center_x=x*GRID_SCALE + GRID_SCALE/2,
center_y=y*GRID_SCALE + GRID_SCALE/2,
width=GRID_SCALE,
height=GRID_SCALE,
color=COLOR_DARK)
self.shape_list_map_1_empty.append(shape_1)
self.shape_list_map_2_empty.append(shape_2)
self.pathFindingMap = PathFindingMap(self)
self.player = Player(x=(GRID_WIDTH*GRID_SCALE)/2, y=(GRID_HEIGHT*GRID_SCALE)/2)
self.enemy_manager = EnemyManager()
## blood
self.blood_splashes: List[Vec2] = []
self.sprite_list_blood = arcade.SpriteList()
arcade.Sprite("assets/blood.png", scale=0.002) # preload this sprite
self.partial_dt = 0
self.score = 0
self.sound_limit = 0
def end_game(self):
arcade.play_sound(SOUND_GAME_OVER, volume=ctx.volume)
self.window.show_view( GameOverView(self.score) )
def on_draw(self):
glow_enabled = self.enemy_manager.rage_mode
if glow_enabled:
self.glow.use()
bg_color = COLOR_BRIGHT if self.enemy_manager.rage_mode else COLOR_DARK
if glow_enabled:
self.glow.fb.clear(bg_color)
if self.enemy_manager.rage_mode:
self.clear(COLOR_BRIGHT)
else:
self.clear(bg_color)
# arcade.set_viewport(0, GRID_WIDTH*GRID_SCALE, 0, GRID_HEIGHT*GRID_SCALE)
ratio = self.window.aspect_ratio
arcade.set_viewport(
left=self.camera_center.x - VIEWPORT_WIDTH/2,
right=self.camera_center.x + VIEWPORT_WIDTH/2,
bottom=self.camera_center.y - VIEWPORT_WIDTH/ratio/2,
top=self.camera_center.y + VIEWPORT_WIDTH/ratio/2
)
if self.enemy_manager.rage_mode:
self.shape_list_map_2_empty.draw()
else:
self.shape_list_map_1_empty.draw()
self.sprite_list_blood.draw()
if self.enemy_manager.rage_mode:
self.shape_list_map_2_wall.draw()
else:
self.shape_list_map_1_wall.draw()
self.player.draw()
self.enemy_manager.draw()
# t1 = perf_counter()
# t2 = perf_counter()
# print(f"Elapsed time: {(t2 - t1)*1000:.2f}ms {len(self.enemy_manager.enemies)}")
## draws gradient map
if False:
if self.pathFindingMap.gradient:
for i in range(GRID_HEIGHT * GRID_WIDTH):
y = i // GRID_WIDTH
x = i % GRID_WIDTH
startx = x*GRID_SCALE + GRID_SCALE/2
starty = y*GRID_SCALE + GRID_SCALE/2
arcade.draw_line(
startx,
starty,
startx + self.pathFindingMap.gradient[i].x,
starty + self.pathFindingMap.gradient[i].y,
arcade.color.RED, line_width=0.2)
arcade.set_viewport(0, self.window.width, 0, self.window.height)
## draws dijsktra map
if False:
for i in range(GRID_HEIGHT * GRID_WIDTH - 100):
y = i // GRID_WIDTH
x = i % GRID_WIDTH
arcade.draw_text(str(self.pathFindingMap.dijkstra[i]),
start_x=x * (self.window.width/(GRID_WIDTH*GRID_SCALE)) * GRID_SCALE + GRID_SCALE/2,
start_y=y * (self.window.width/(GRID_WIDTH*GRID_SCALE)) * GRID_SCALE + GRID_SCALE/2,
color=arcade.color.RED)
time_factor = 1
tm = (self.enemy_manager.until_rage + time_factor/2) % RAGE_DELAY
if tm < time_factor:
bright = COLOR_BRIGHT if glow_enabled else COLOR_BRIGHT_2
color = bright if self.enemy_manager.rage_mode == (tm > time_factor/2) else COLOR_DARK
border_width = sin((tm) / time_factor * pi) * min(self.window.height, self.window.width)
arcade.draw_rectangle_outline(self.window.width / 2, self.window.height / 2, self.window.width, self.window.height, color, border_width)
if glow_enabled:
self.glow.render(self.window.ctx.screen)
arcade.draw_text(f"Score: {int(self.score)}", self.window.width/2, self.window.height-20, color=arcade.color.SAE, anchor_x='center', anchor_y='center', font_name=FONT, font_size=16)
def alloc_sound(self):
if self.sound_limit > 1:
self.sound_limit -= 1
return True
return False
def on_update(self, dt):
self.score += SCORE_PER_SECOND * dt
self.partial_dt += dt
if self.sound_limit < 8:
self.sound_limit += 8 * dt
DT = 1/60
if self.partial_dt > 1:
self.partial_dt = 1
while self.partial_dt > DT:
self.partial_dt -= DT
self.player.update(DT)
self.enemy_manager.update(DT)
for x, y in self.blood_splashes:
blood_sprite = arcade.Sprite("assets/blood.png", scale=0.002, center_x=x, center_y=y, angle=random.randrange(0, 360))
self.sprite_list_blood.append(blood_sprite)
self.blood_splashes = []
self.camera_center = self.camera_center + (self.player.pos - self.camera_center) * 0.3
def on_key_press(self, key, key_modifiers):
self.pressed[key] = True
if key == arcade.key.F11:
self.window.set_fullscreen(
True - self.window.fullscreen
)
if key == arcade.key.ESCAPE:
self.window.show_view( MenuView(self) )
def on_key_release(self, key, key_modifiers):
self.pressed[key] = False
def on_resize(self, width: int, height: int):
super().on_resize(width, height)
self.glow.gen_fbs((width, height))
def main():
window = arcade.Window(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE, resizable=True)
window.set_minimum_size(720, 480)
window.set_vsync(True)
window.set_mouse_visible(False)
arcade.load_font("assets/BebasNeue-Regular.ttf")
arcade.play_sound(SOUND_GAME_OVER, volume=0) # Let arcade initialize sound so it doesn't freeze later
startView = StartView()
window.show_view(startView)
arcade.run()
if __name__ == "__main__":
import sys
import os
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
os.chdir(sys._MEIPASS)
main()