-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
86 lines (61 loc) · 1.55 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
import math
import random
import tkinter as tk
# Project
from constants import *
import vectorutil as vu
from flora import Flora
from boid import Boid
from predator import Predator
# Constants
INI_PREDS = 4
INI_BOIDS = 25
INI_FLORA = 45
BKGD_CLR = '#030A21'
# Init UI
master = tk.Tk()
master.title("The Life Aquatic")
canvas = tk.Canvas(master, bg=BKGD_CLR, width=CANVAS_W, height=CANVAS_H)
canvas.pack()
# Alias function
def _create_circle(self, x, y, r, **kwargs):
return self.create_oval(x-r, y-r, x+r, y+r, **kwargs)
tk.Canvas.create_circle = _create_circle
# Simulation functions
State = { 'iter': 0 }
#
def initSimulation():
State['iter'] = 0
for i in range(INI_FLORA):
Flora(canvas)
for i in range(INI_BOIDS):
Boid(canvas)
for i in range(INI_PREDS):
Predator(canvas)
Boid.setPredatorGrid(Predator.grid)
#
def updateSimulation():
# Update positions
for boid in Boid.all:
boid.updatePos()
for pred in Predator.all:
pred.updatePos()
# Apply new positions
for boid in Boid.all:
boid.applyNewPos()
for pred in Predator.all:
pred.applyNewPos()
# Update flora
for flora in Flora.all:
flora.updateGrowth()
# Simulation end
if len(Boid.all) == 0:
print('Simulation iterations:', State['iter'])
return
# Simulation loop
State['iter'] += 1
master.after(1000//ITER_SEC, updateSimulation)
# Run simulation
initSimulation()
updateSimulation()
tk.mainloop()