-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
150 lines (124 loc) · 3.53 KB
/
models.py
File metadata and controls
150 lines (124 loc) · 3.53 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
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
import pygame
import sys, math, random
pygame.init()
class Point:
def __init__(self, x, y, d, col, leaders=[]):
self.x = x
self.y = y
self.d = d
self.c = col
self.mx = self.amx = 0
self.my = self.amy = 0
self.setLeaders(leaders)
#enddef
def setLeaders(self, leaders):
self.leaders = []
distTotal = 0
for l in leaders:
self.leaders.append(l)
distTotal += l.getDist(self.x, self.y)
#endfor
self.dists = []
for l in self.leaders:
self.dists.append( l.getDist(self.x, self.y) )
#self.dists.append((1.0 - l.getDist(self.x, self.y)/float(distTotal)))
#enddef
def getDist(self, x, y):
dx = self.x - x
dy = self.y - y
return math.sqrt(float(dx**2 + dy**2))
#enddef
def draw(self, to):
if not self.c:
return
self.amx = self.mx
self.amy = self.my
for i, l in enumerate(self.leaders):
a = self.dists[i]
if a==0.0:
continue
mx = l.amx
if math.fabs(mx) < a:
mx = (mx/a)**2 * mx
my = l.amy
if math.fabs(my) < a:
my = (my/a)**2 * my
self.amx += mx
self.amy += my
#endfor
x = self.x + self.amx
y = self.y + self.amy
to.fill(self.c, (x-self.d/2, y-self.d/2, self.d, self.d))
#enddef
def behave(self):
pass
#enddef
#endclass
class PointRandom(Point):
def behave(self):
self.mx = random.randint(-1,1)
self.my = random.randint(-1,1)
#enddef
#endclass
points = []
# leaders
#points.append(Point(150,100,4,(250,210,200, 200)))
points.append(Point(150,150,4,(250,210,200, 200)))
#points.append(Point(150,200,4,(250,210,200, 200)))
leads = [points[0]]
#points[1].setLeaders([points[0],points[2]])
ctrP = 0
# other
for i in xrange(0, 10):
prev = [points[0]]
for j in xrange(10, 60, 20):
x = math.cos(math.pi/5*i) * j
y = math.sin(math.pi/5*i) * j
p = PointRandom(150+x,150+y,8,(200-j,200-j,200+random.randint(-20,20), 200), prev)
points.append(p)
prev = [p]
screen = pygame.display.set_mode((640,480), pygame.HWSURFACE|pygame.DOUBLEBUF)
clock = pygame.time.Clock()
left = False
right = False
up = False
down = False
tickCnt = 0
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit(0)
if event.type == pygame.KEYDOWN:
key = event.key
if key==27: sys.exit(0) #self.exit = True
if key==273: up = True
if key==274: down = True
if key==276: left = True
if key==275: right = True
# if key==305: self.fire = True
if event.type == pygame.KEYUP:
key = event.key
if key==273: up = False
if key==274: down = False
if key==276: left = False
if key==275: right = False
if event.type == pygame.MOUSEBUTTONDOWN:
#print pygame.mouse.get_pressed()
(cx, cy) = pygame.mouse.get_pos()
#endfor
if up: points[ctrP].my -= 1
if down: points[ctrP].my += 1
if left: points[ctrP].mx -= 1
if right: points[ctrP].mx += 1
screen.fill((0,0,0))
for p in points:
p.draw(screen)
#endfor
pygame.display.flip()
clock.tick(30)
tickCnt += 1
if tickCnt > 5:
for p in points:
p.behave()
tickCnt = 0
#endif
#endwhile