-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
53 lines (44 loc) · 1.7 KB
/
game.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
from tkinter import *
from time import sleep
from Population import Population
from Dot import Dot
from Obstical import Obstical
import matplotlib.pyplot as plt
def plot_progress(data, on=True):
if on:
plt.clf()
plt.plot(data, 'bo-')
plt.show(block=False)
WIDTH = 800
HEIGHT = 600
STATE = 4
plt.figure(2)
root = Tk()
root.title('Dot AI')
main_window = Canvas(root, height=HEIGHT, width=WIDTH, bg='white')
curr_gen_label = Label(root, text=f'Generation: {Population.gen}')
curr_gen_label.pack()
main_window.pack()
goal = main_window.create_oval(Dot.goal_position[0], Dot.goal_position[1], Dot.goal_position[0]+Dot.goal_position[2], Dot.goal_position[1]+Dot.goal_position[2], fill='blue')
population = Population(root, main_window, 1000) # population is 1000
obsticals = Obstical(main_window, state=STATE) # 4th state
root.update()
total_reached_goal = []
try:
while True:
if not population.is_all_dots_dead():
population.update()
root.update()
sleep(0.01)
else:
main_window.delete('all')
goal = main_window.create_oval(Dot.goal_position[0], Dot.goal_position[1], Dot.goal_position[0]+Dot.goal_position[2], Dot.goal_position[1]+Dot.goal_position[2], fill='blue')
obsticals = Obstical(main_window, state=STATE) # 4th state
population.calc_fitnesses()
total_reached_goal.append(population.how_many())
population.natural_selection()
population.mutate_population()
curr_gen_label.config(text=f'Generation: {Population.gen} | Dots Reached Goal: {total_reached_goal[Population.gen-2]} / 1000')
plot_progress(total_reached_goal)
except:
pass