Skip to content
Snippets Groups Projects
Commit b1544e36 authored by Jan Luebbe's avatar Jan Luebbe
Browse files

Add python clients and simulator

parent d3acaa8f
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/python
PWIDTH = 5
WIDTH = PWIDTH*96
PHEIGHT = 7
PPAD = 5
HEIGHT = PHEIGHT*10
import sys
SERVER = "tcp://localhost:5571"
if len(sys.argv) >= 2:
SERVER = sys.argv[1]
import struct
import zmq
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(SERVER)
def set_pixel(x, y, v):
tx = bytearray(9)
struct.pack_into('iiB', tx, 0, x, y, v)
socket.send(tx)
rx = socket.recv()
#print("Received reply %s [%r]" % ((x, y, v), rx))
#!/usr/bin/python
import client
SIZE = 1
import time
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)
client.set_pixel(i, j, r)
def draw(x, y, v):
rect(x*SIZE, y*SIZE, SIZE, SIZE, 255*v, 255*v, 255*v)
import random
class Game(object):
def __init__(self, state, infinite_board = True):
self.state = state
self.width = state.width
self.height = state.height
self.infinite_board = infinite_board
def step(self, count = 1):
for generation in range(count):
new_board = [[False] * self.width for row in range(self.height)]
for y, row in enumerate(self.state.board):
for x, cell in enumerate(row):
neighbours = self.neighbours(x, y)
previous_state = self.state.board[y][x]
should_live = neighbours == 3 or (neighbours == 2 and previous_state == True)
new_board[y][x] = should_live
self.state.update(new_board)
def neighbours(self, x, y):
count = 0
for hor in [-1, 0, 1]:
for ver in [-1, 0, 1]:
if not hor == ver == 0 and (self.infinite_board == True or (0 <= x + hor < self.width and 0 <= y + ver < self.height)):
count += self.state.board[(y + ver) % self.height][(x + hor) % self.width]
return count
def display(self):
return self.state.display()
class State(object):
def __init__(self, positions, x, y, width, height):
self.width = width
self.height = height
self.board = [[False] * self.width for row in range(self.height)]
active_cells = []
for l, row in enumerate(positions.splitlines()):
for r, cell in enumerate(row.strip()):
if cell == 'o':
active_cells.append((r,l))
board = [[False] * width for row in range(height)]
for cell in active_cells:
board[cell[1] + y][cell[0] + x] = True
self.update(board)
def update(self, new):
for y, row in enumerate(new):
for x, cell in enumerate(row):
if self.board[y][x] != cell:
self.board[y][x] = cell
draw(x, y, cell)
def display(self):
output = ''
for y, row in enumerate(self.board):
for x, cell in enumerate(row):
if self.board[y][x]:
output += 'o'
else:
output += '.'
output += '\n'
return output
glider = """
oo.
o.o
o..
"""
gun = """
........................o...........
......................o.o...........
............oo......oo............oo
...........o...o....oo............oo
oo........o.....o...oo..............
oo........o...o.oo....o.o...........
..........o.....o.......o...........
...........o...o....................
............oo......................
"""
my_game = Game(State(gun,
x = 20, y = 10,
width = client.WIDTH/SIZE, height = client.HEIGHT/SIZE)
)
while True:
#print my_game.display()
my_game.step(1)
#!/usr/bin/python
import sys
SERVER = "tcp://localhost:5571"
XOFF = 0
YOFF = 0
TEXT = "Hello World!"
if __name__=="__main__":
if len(sys.argv) >= 2:
SERVER = sys.argv[1]
if len(sys.argv) >= 4:
XOFF = int(sys.argv[2])
YOFF = int(sys.argv[3])
if len(sys.argv) >= 5:
TEXT = sys.argv[4]
import struct
import zmq
# Prepare our context and sockets
context = zmq.Context()
socket = context.socket(zmq.REQ)
socket.connect(SERVER)
def set_pixel(x, y, v):
tx = bytearray(9)
struct.pack_into('iiB', tx, 0, x, y, v)
socket.send(tx)
rx = socket.recv()
#print("Received reply %s [%r]" % ((x, y, v), rx))
import pygame
pygame.init()
font = pygame.font.Font("/usr/share/fonts/X11/misc/5x7.pcf.gz", 7)
text = font.render(TEXT, True, (255, 255, 255), (0, 0, 0))
pxarray = pygame.PixelArray(text)
for x in range(text.get_width()):
for y in range(text.get_height()):
set_pixel(XOFF+x, YOFF+y, pxarray[x][y])
del pxarray
#!/usr/bin/python
import zmq, sys
context = zmq.Context()
# Socket facing clients
frontend = context.socket(zmq.ROUTER)
frontend.bind("tcp://*:5571")
# Socket facing services
backend = context.socket(zmq.DEALER)
backend.connect(sys.argv[1])
zmq.device(zmq.QUEUE, frontend, backend)
# We never get here...
frontend.close()
backend.close()
context.term()
#!/usr/bin/python
PWIDTH = 5
WIDTH = PWIDTH*96
PHEIGHT = 7
PPAD = 5
HEIGHT = (PHEIGHT+PPAD)*9+PHEIGHT
ZOOM = 2
import pygame, sys
pygame.init()
size = (WIDTH*ZOOM, HEIGHT*ZOOM)
screen = pygame.display.set_mode(size)
import struct
import zmq
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5570")
def set_pixel(x, y, v):
v = v/255.0
row = y / 7
if row in [0, 1, 2, 9]:
c = (0xff*v, 0, 0)
elif row in [3, 4, 5]:
c = (0, 0xff*v, 0)
else:
c = (0xff*v, 0xa5*v, 0)
y += y / PHEIGHT * PPAD
screen.fill(c, (x*ZOOM, y*ZOOM, ZOOM, ZOOM))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if not socket.poll(1):
pygame.display.update()
continue
message = socket.recv()
x, y, v = struct.unpack_from('iiB', message)
# print("Received request: %r, %r, %r" % (x, y, v))
set_pixel(x, y, v)
socket.send(b"")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment