-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpart2.py
More file actions
114 lines (95 loc) · 3.55 KB
/
Copy pathpart2.py
File metadata and controls
114 lines (95 loc) · 3.55 KB
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
"""
Boids simulation using python-csp and pygame.
Part 2 -- Adding movement to the boids.
Copyright (C) Sarah Mount, 2009.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have rceeived a copy of the GNU General Public License
along with this program; if not, write to the Free Software
"""
from csp.cspprocess import *
__author__ = 'Sarah Mount <s.mount@wlv.ac.uk>'
__date__ = 'October 2009'
@process
def simulate(poschan, SIZE):
"""
readset =
writeset = poschan
"""
centre = [random.randint(0, SIZE[0]), random.randint(0, SIZE[1])]
default_velocity = [random.choice((-1.0, 0.0, 1.0)),
random.choice((-1.0, 0.0, 1.0))]
velocity = default_velocity
while True:
centre = [centre[0] + velocity[0],
centre[1] + velocity[1]]
# Wrap the screen.
if centre[0]<0: centre[0] += SIZE[0]
elif centre[0]>SIZE[0]: centre[0] -= SIZE[0]
if centre[1]<0: centre[1] += SIZE[1]
elif centre[1]>SIZE[1]: centre[1] -= SIZE[1]
# Write to the drawing process.
poschan.write(centre)
return
@process
def drawboids(poschans, SIZE):
"""
readset = poschans
writeset =
"""
import pygame
FGCOL = (137, 192, 210, 100) # Foreground colour.
BGCOL = pygame.Color('black') # Background colour.
FPS = 60 # Maximum frames per second.
CAPTION = 'python-csp example: Boids'
FILENAME = 'boids.png' # Screenshot file.
QUIT = False
clock = pygame.time.Clock()
dirty, last = [], []
# chansize = len(poschans)
pygame.init()
screen = pygame.display.set_mode((SIZE[0], SIZE[1]), 0)
pygame.display.set_caption(CAPTION)
while not QUIT:
ms_elapsed = clock.tick(FPS)
print(ms_elapsed)
dirty = last
for rect in last: screen.fill(BGCOL, rect)
last = []
for channel in poschans:
x, y = channel.read()
rect = pygame.draw.circle(screen, FGCOL, (int(x), int(y)), 2, 0)
dirty.append(rect)
last.append(rect)
pygame.display.update(dirty) # Update dirty rects.
for event in pygame.event.get(): # Process events.
if event.type == pygame.QUIT:
QUIT = True
elif event.type == pygame.KEYDOWN and event.key == pygame.K_s:
pygame.image.save(screen, FILENAME)
print('Saving boids in:', FILENAME)
for chan in poschans: chan.poison()
pygame.quit()
return
@process
def main():
NUMBOIDS = 100 # Number of boids in simulation.
SIZE = (800, 600) # Screen size.
# Set up channels for reporting boid positions / velocities.
poschans = [Channel() for i in range(NUMBOIDS)]
# Draw channel for the drawboids process.
# drawchan = Channel()
# Generate a list of all processes in the simulation.
procs = [simulate(poschans[i], SIZE) for i in range(NUMBOIDS)]
procs.append(drawboids(poschans, SIZE)) # Drawing process.
simulation = Par(*procs) # Start simulation.
simulation.start()
return
if __name__ == '__main__':
main().start()