-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
97 lines (76 loc) · 2.63 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
# 171229 joo
# github.com/a9
# thingiverse.com/joo
#
# hexsnakes for pixelflut
# 34C3
#
# code base from standard python example for pixelflut
# python 2.7.11
import socket
import time
import random
HOST = '151.217.47.77'
PORT = 8080
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
send = sock.send
def pixel(x, y, r, g, b, a=255):
global sock
try:
if a == 255:
send('PX %d %d %02x%02x%02x\n' % (x, y, r, g, b))
#print('PX %d %d %02x%02x%02x\n' % (x, y, r, g, b))
else:
send('PX %d %d %02x%02x%02x%02x\n' % (x, y, r, g, b, a))
#print('PX %d %d %02x%02x%02x%02x\n' % (x, y, r, g, b, a))
except:
# Socket Connection restart doesn't work right now
print("new socket")
sock.shutdown(1)
sock.close()
time.sleep(1)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
def rect(x,y,w,h,r,g,b):
for i in xrange(x,x+w):
for j in xrange(y,y+h):
pixel(i,j,r,g,b)
def clamp(n, minn, maxn):
return max(min(maxn, n), minn)
class Hexsnake():
def __init__(self):
# Startparameters for the snake
self.x = 400
self.y = 200
self.direction = 0
self.dist = 18
self.width = 12
# Color is randomized
self.red = random.random()*255
self.green = random.random()*255
self.blue = random.random()*255
def run(self):
# Instead of using math functions, the movement offset for each update
# is stored in a lookup table (array)
offsx = [0, 2, 2, 0, -2, -2]
offsy = [3, 1, -1, -3, -1, 1]
# the change of the movement direction is achieved by moving the index
# of the lookup table by a maximum of +- one position
#
# circle buffer overflow is achived using the modulu (%) method
self.direction = (self.direction + random.randint(-1, 1)) % len(offsx)
for i in range(self.dist):
self.x += offsx[self.direction]
self.y += offsy[self.direction]
self.x = clamp(self.x, 20, 750)
self.y = clamp(self.y, 20, 550)
# to achieve a 3D effect, two rectangles (resulting in a square) are drawn
# the upper one with normal color, the lower one with half the brightness
rect(self.x, self.y, self.width, self.width/2, self.red, self.green, self.blue)
rect(self.x, self.y+self.width/2, self.width, self.width/2, self.red/2, self.green/2, self.blue/2)
hexsnake = Hexsnake()
def loopit():
while True:
hexsnake.run()
loopit()