-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
241 lines (196 loc) · 7.74 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
import pygame
import random
"""
1. place start
"""
def draw_square(surface, color, x, y, width, height):
pygame.draw.rect(surface, color, ((x, y), (width, height)), 0)
class Pandemic:
def __init__(self):
pygame.init()
pygame.display.set_caption('Pandemic Simulation')
pygame.display.set_icon(pygame.image.load('assets/window_icon.png'))
self.grid = []
self.clock = pygame.time.Clock()
self.fps = 400
self.alive = True
self.running = True
self.updating = False
self.alpha = 0.25 # infection rate
self.beta = 1 / 14
self.gamma = 0.05
self.load_data()
def load_data(self):
with open('assets/fullworld.txt', 'r') as file:
c = 0
for i in file:
self.grid.append([])
for j in i:
if j.isdigit():
self.grid[c].append(int(j))
c += 1
with open('settings.txt', 'r') as settingsfile:
self.alpha = float(settingsfile.readline().split("=")[1].replace("\n", ""))
self.beta = float(settingsfile.readline().split("=")[1].replace("\n", ""))
self.gamma = float(settingsfile.readline().split("=")[1].replace("\n", ""))
self.mortalityrate = float(settingsfile.readline().split("=")[1].replace("\n", ""))
# self.grid[60][300] = 1
def new(self):
"""
Initializes new vizualisation
"""
# sqreensize based on the grid
self.square = 4
self.width = len(self.grid[0]) * self.square
self.height = len(self.grid) * self.square
self.sucseptible = []
for i in range(len(self.grid)):
for j in range(len(self.grid[i])):
if self.grid[i][j] == 0:
self.sucseptible.append((i, j))
# self.infected = [(60, 300)]
self.infected = []
self.recovered = []
self.deaths = 0
self.day = 0
# screen object
self.screen = pygame.display.set_mode((self.width, self.height))
self.run()
def startscreen(self):
pass
def run(self):
"""
The runloop
"""
while self.running:
self.clock.tick(self.fps)
self.events()
if self.updating and self.infected:
self.update()
self.draw()
def events(self):
"""
Keyboard and mouse events
"""
for event in pygame.event.get():
# Quit
if event.type == pygame.QUIT:
self.running = False
self.alive = False
if event.type == pygame.KEYDOWN:
# Quit
if event.key == pygame.K_ESCAPE:
self.running = False
self.alive = False
# Pause
if event.key == pygame.K_SPACE:
self.updating = True if not self.updating else False
self.fps = 60
# Place and remove infected
try:
# Adding
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
col, row = (int(pos[0] / self.square), int(pos[1] / self.square)) # Grid coordinates
if self.grid[row][col] == 0:
self.grid[row][col] = 1
self.infected.append((row, col))
self.sucseptible.remove((row, col))
# Removing
if pygame.mouse.get_pressed()[2]:
pos = pygame.mouse.get_pos()
col, row = (int(pos[0] / self.square), int(pos[1] / self.square)) # Grid coordinates
if self.grid[row][col] == 1:
self.grid[row][col] = 0
self.infected.remove((row, col))
self.sucseptible.append((row, col))
except Exception as e:
print(e)
def find_neighbours(self, i, j):
"""
:returns: list of neighbouring nodes in the grid
"""
neighbours = []
cords = [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]
for row, col in cords:
if 0 <= row <= len(self.grid) - 1:
if 0 <= col <= len(self.grid[0]) - 1:
neighbours.append((row, col))
return neighbours
def infect(self, alpha):
return True if random.random() < alpha else False
def recover(self, beta):
return True if random.random() < beta else False
def spread(self, gamma):
return True if random.random() < gamma else False
def die(self, mortalityrate):
return True if random.random() < mortalityrate else False
def update(self):
"""
Updates all data
"""
# Day counter
self.day += 1
# Infect
preliminary_infections = []
preliminary_recovered = []
for row, col in self.infected:
n = self.find_neighbours(row, col)
# Infect Neigbours
for i, j in n:
if self.grid[i][j] == 0:
if self.infect(self.alpha):
self.grid[i][j] = 1
preliminary_infections.append((i, j))
# Recover
if self.recover(self.beta):
if self.die(self.mortalityrate):
self.grid[row][col] = 4
self.recovered.append((row, col))
preliminary_recovered.append((row, col))
else:
self.grid[row][col] = 2
self.recovered.append((row, col))
preliminary_recovered.append((row, col))
for cordinate in preliminary_infections:
self.sucseptible.remove(cordinate)
# Risk of spreading to random locations
if self.infected:
if self.spread(self.gamma):
if self.sucseptible:
i, j = random.choice(self.sucseptible)
self.grid[i][j] = 1
self.infected.append((i, j))
self.sucseptible.remove((i, j))
for cordinate in preliminary_recovered:
self.infected.remove(cordinate)
self.infected += preliminary_infections
def draw_text(self, text, size, color, x, y):
#font = pygame.font.Font(pygame.font.match_font('arial'), size)
font = pygame.font.Font("assets/ghostclanlaserital.ttf", size)
text_surface = font.render(text, True, color)
text_rect = text_surface.get_rect()
text_rect.midleft = (x, y)
self.screen.blit(text_surface, text_rect)
def draw(self):
"""
Draws the new data onto the screen
"""
self.screen.fill((255, 255, 255))
colors = [(255, 255, 255), (255, 0, 0), (0, 100, 0), (0, 0, 100), (0, 0, 0)]
for i in range(len(self.grid)):
for j in range(len(self.grid[i])):
draw_square(self.screen, colors[self.grid[i][j]], self.square * j, self.square * i, self.square,
self.square)
# 1440x980
self.draw_text('Day: ' + str(self.day), 40, (255, 255, 0), 20, 50)
self.draw_text('Infected: ' + str(len(self.infected)), 40, (100, 0, 0), 20, 500)
self.draw_text('Recovered: ' + str(len(self.recovered)), 40, (0, 100, 12), 20, 600)
self.draw_text('Suceptible: ' + str(len(self.sucseptible)), 40, (200, 200, 200), 20, 700)
self.draw_text('Leftclick to infect, Rightclick to remove infection, Space to start and pause simulation', 25,
(0, 0, 0), 200, self.height - 20)
pygame.display.flip()
c = Pandemic()
while c.alive:
c.new()
pygame.quit()