-
Notifications
You must be signed in to change notification settings - Fork 0
/
pandemic.py
110 lines (91 loc) · 3.21 KB
/
pandemic.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
from Person import Person
import tkinter as tk
from random import randint
def pauseresume():
global running
if running:
running = False
else:
running = True
germs_radius = Person.radius
window_size = 666
def move():
global running, speed, canvas
if running:
# moving every dot
for person in people:
person.move()
# sickness spreading algorithm
for person1 in people:
for person2 in people:
if person1.is_sick and (not person2.is_sick) and person1.distance(person2) < germs_radius:
person2.is_sick = True
canvas.itemconfig(person2.circle, fill="red", outline='red')
window.after(30, move)
def change_values(speed_var, amount_var):
global speed
global amount
global canvas
if speed_var.get():
new_speed = int(speed_var.get())
if new_speed != speed:
speed = new_speed
for person in people:
person.set_speed(speed)
if amount_var.get() and (int(amount_var.get()) > 0):
diff = int(amount_var.get()) - amount
if diff < 0:
for i in range(-diff):
canvas.delete(people[-1].circle)
del people[-1]
elif diff == 0:
pass
else:
for i in range(diff):
people.append(Person(canvas,
randint(Person.radius, window_size-Person.radius),
randint(Person.radius, window_size-Person.radius),
speed=speed,
window_size=window_size))
amount = len(people)
# --- main ---
# creating canvas and buttons
window = tk.Tk()
window.title('pandemic')
canvas = tk.Canvas(window, bg="white", height=window_size, width=window_size)
pauseresume_btn = tk.Button(window, text="Pause/Resume", command=lambda: pauseresume())
submit_btn = tk.Button(window, text="Submit", command=lambda: change_values(speed_var, amount_var))
# creating variables for storing text from input widgets
speed_var = tk.StringVar()
amount_var = tk.StringVar()
# creating input widgets
speedlbl = tk.Label(window, text="Enter speed")
speedentry = tk.Entry(window, textvariable=speed_var)
amountlbl = tk.Label(window, text="Enter amount")
amountentry = tk.Entry(window, textvariable=amount_var)
# packing content
pauseresume_btn.pack(side='top')
speedlbl.pack(side='top')
speedentry.pack(side='top')
amountlbl.pack(side='top')
amountentry.pack(side='top')
submit_btn.pack(side='top')
canvas.pack()
# creating objects
amount = 8
speed = 5
people = [Person(canvas,
randint(Person.radius, window_size-Person.radius),
randint(Person.radius, window_size-Person.radius),
is_sick=True,
speed=speed,
window_size=window_size)]
for i in range(amount-1):
people.append(Person(canvas,
randint(Person.radius, window_size-Person.radius),
randint(Person.radius, window_size-Person.radius),
speed=speed,
window_size=window_size))
running = True
move()
window.mainloop()