-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsim.py
86 lines (68 loc) · 2.48 KB
/
sim.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
import random
import collision
from human import Human
pygame.init()
WIDTH = 640
HEIGHT = 480
screen = pygame.display.set_mode((WIDTH, HEIGHT)) # try out larger values and see what happens !
screenrect = screen.get_rect()
background = pygame.Surface(screen.get_size()) #create surface for background
background.fill((255, 255, 255)) #fill the background white (red,green,blue)
background = background.convert() #convert surface for faster blitting
clock = pygame.time.Clock() #create pygame clock object
mainloop = True
FPS = 60 # desired max. framerate in frames per second.
def quarantine():
return True if random.randint(0, 10) < 9 else False
def sick():
return True if random.randint(0, 10) < 1 else False
barriers = [
collision.Poly(
collision.Vector(0, 0),
[collision.Vector(0, 0),
collision.Vector(1, 0),
collision.Vector(1, HEIGHT),
collision.Vector(0, HEIGHT)]),
collision.Poly(
collision.Vector(WIDTH, 0),
[collision.Vector(0, 0),
collision.Vector(10, 0),
collision.Vector(10, HEIGHT),
collision.Vector(0, HEIGHT)]),
collision.Poly(
collision.Vector(0, -10),
[collision.Vector(0, 0),
collision.Vector(WIDTH, 0),
collision.Vector(WIDTH, 10),
collision.Vector(0, 10)]),
collision.Poly(
collision.Vector(0, HEIGHT),
[collision.Vector(0, 0),
collision.Vector(WIDTH, 0),
collision.Vector(WIDTH, 10),
collision.Vector(0, 10)]),
]
humans = [
Human(background, quarantine(), sick(), random.randint(0, WIDTH), random.randint(0, HEIGHT), WIDTH, HEIGHT,
barriers) for x in range(0, 60)
]
while mainloop:
milliseconds = clock.tick(FPS) # milliseconds passed since last frame
seconds = milliseconds / 1000.0 # seconds passed since last frame (float)
for event in pygame.event.get():
if event.type == pygame.QUIT:
mainloop = False # pygame window closed by user
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
mainloop = False # user pressed ESC
background.fill((0, 0, 0))
for human in humans:
human.collide(humans)
for human in humans:
human.update(seconds)
human.render()
screen.blit(background, (0, 0)) #draw background on screen (overwriting all)
pygame.display.flip() # flip the screen 30 times a second