forked from muccc/acab-streetlife
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acabsimulator.py
executable file
·128 lines (103 loc) · 3.35 KB
/
acabsimulator.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
#!/usr/bin/python
import pygame
from pygame.locals import *
import sys
import threading
import socket
import thread
import time
import Queue
import sys
UDP_IP="127.0.0.1"
UDP_PORT=int(sys.argv[2])
q = Queue.Queue(100)
xsize = 60
ysize = 40
xpixels = 8
ypixels = 6
acab = [[[0,0,0,0,0,0,0,0,0] for col in range(ypixels)] for row in range(xpixels)]
def handle_events():
surf = pygame.Surface((xsize,ysize))
while 1:
for event in pygame.event.get():
if event.type in (QUIT, QUIT):
import os
os.kill(os.getpid(), 9)
for y in range(0,ypixels):
for x in range (0,xpixels):
current = acab[x][y][0:3]
diff = acab[x][y][3:6]
counts = acab[x][y][6:9]
new = [0, 0, 0]
for i in range(3):
new[i] = max((0,min((current[i] + diff[i],255))))
counts[i] = counts[i] - 1
if counts[i] == 0:
diff[i] = 0
surf.fill(tuple(map(int, new)))
screen.blit(surf, (x*xsize, y*ysize))
acab[x][y][0:3] = new
acab[x][y][3:6] = diff
acab[x][y][6:9] = counts
pygame.display.update()
pygame.time.delay(33)
pygame.init()
screen = pygame.display.set_mode((xpixels*xsize, ypixels*ysize))
screen.fill((255, 255, 255))
pygame.display.set_caption(sys.argv[1])
updatethread = threading.Thread(target=handle_events)
updatethread.start()
def writer():
while 1:
data = q.get()
try:
x = ord(data[0])
y = ord(data[1])
cmd = data[2]
r = ord(data[3])
g = ord(data[4])
b = ord(data[5])
msh = ord(data[6])
msl = ord(data[7])
ms = (msh<<8) + msl;
if cmd == 'C':
time = ms
data = acab[x][y]
current = data[0:3]
target = [r,g,b]
diff = [target[i] - current[i] for i in range(3)]
steps = time/33
if steps == 0:
data[0:3] = target
data[3:6] = [0,0,0]
else:
data[3:6] = [ d/float(steps) for d in diff]
data[6:9] = [steps, steps, steps]
acab[x][y] = data
elif cmd == 'F':
speed = ms / 58.7
data = acab[x][y]
current = data[0:3]
target = [r,g,b]
diff = [target[i] - current[i] for i in range(3)]
data[3:6] = [speed, speed, speed]
data[6:9] = [d/speed for d in diff]
for i in range(3):
if diff[i] < 0:
data[3+i] = -data[3+i]
data[6+i] = -data[6+i]
print data
acab[x][y] = data
elif cmd == 'U':
pass
except Exception as e:
print "Unexpected error:", e
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP,UDP_PORT))
thread.start_new_thread(writer,())
while True:
data = sock.recv(1024)
if not q.full():
q.put(data)
else:
print 'ignoring data'