-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoidsWindow.py
More file actions
100 lines (80 loc) · 3.11 KB
/
BoidsWindow.py
File metadata and controls
100 lines (80 loc) · 3.11 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
import os
import arcade
from dotenv import load_dotenv
import BoidVisualiser
from Flock import Flock
from Mouse import Mouse
load_dotenv()
WIDTH: int = int(os.getenv('WIDTH'))
HEIGHT: int = int(os.getenv('HEIGHT'))
MAXBOIDS: int = int(os.getenv('MAXBOIDS'))
RANGE_MIN: int = int(os.getenv('RANGE_MIN'))
RANGE_FOV: int = int(os.getenv('RANGE_FOV'))
BUTTON_LEFT: int = 1
BUTTON_RIGHT: int = 4
class BoidsWindow(arcade.Window):
buffer: int = 15
def __init__(self):
super().__init__(WIDTH, HEIGHT, "The Boid Simulator")
self.mouse = Mouse()
self.flocks = [Flock(WIDTH, HEIGHT, MAXBOIDS, RANGE_MIN, RANGE_FOV),
Flock(WIDTH, HEIGHT, MAXBOIDS, RANGE_MIN, RANGE_FOV),
Flock(WIDTH, HEIGHT, MAXBOIDS, RANGE_MIN, RANGE_FOV),
]
batch_visuals = os.getenv("BATCH_VISUALS", 'False').lower() in ('true', '1', 't')
if batch_visuals:
BoidVisualiser.setup_batches() #Prelim work to enable batching & set batching flag for visuals
arcade.enable_timings() #required for FPS counter
def on_update(self, delta_time: float):
for flock in self.flocks:
flock.update(self.mouse)
def on_draw(self):
self.clear()
# Draw the border
arcade.draw_rectangle_outline(WIDTH / 2,
HEIGHT / 2,
WIDTH - self.buffer,
HEIGHT - self.buffer,
(255, 255, 255, 128),
2,
0)
# Add Text
arcade.draw_text(f"Mouse: {self.mouse.active}",
WIDTH - 150,
HEIGHT - 50,
[255, 255, 255, 128],
12)
arcade.draw_text(f"Follow: {self.mouse.chase}",
WIDTH - 150,
HEIGHT - 70,
[255, 255, 255, 128],
12)
arcade.draw_text(f"FPS: {arcade.get_fps():.2f}",
WIDTH - 150,
HEIGHT - 90,
[255, 255, 255, 128],
12)
# Draw mouse
if self.mouse.active:
arcade.draw_circle_filled(self.mouse.x,
self.mouse.y,
5,
arcade.color.GREEN)
# Draw each flock
for flock in self.flocks:
BoidVisualiser.draw_flock(flock)
def on_mouse_motion(self, x, y, dx, dy):
"""
Called whenever the mouse moves.
"""
if self.mouse.active:
self.mouse.x = x
self.mouse.y = y
else:
self.mouse.x = 0
self.mouse.y = 0
def on_mouse_press(self, x, y, button, modifiers):
if button == BUTTON_LEFT:
self.mouse.active = not self.mouse.active
if button == BUTTON_RIGHT:
self.mouse.chase = not self.mouse.chase