Created
June 3, 2017 07:58
-
-
Save qguv/b22d9cceca9e477b8e767a5368c64578 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import socket | |
from random import randrange, shuffle | |
from threading import Thread | |
from time import sleep | |
HOST = '192.168.1.160' | |
PORT = 1234 | |
def in_bounds(x, y, xmin, ymin, xmax, ymax): | |
return x >= xmin and x <= xmax and y >= ymin and y <= ymax | |
def constrain(x, y, xmin, ymin, xmax, ymax): | |
return (min(xmax, max(xmin, x)), min(ymax, max(ymin, y))) | |
def count_repeat(low, high): | |
while True: | |
for i in range(low, high): | |
yield i | |
class Field(socket.socket): | |
def __init__(self, host, port): | |
super().__init__() | |
self.connect((host, port)) | |
self.send(b'SIZE\n') | |
s = self.recv(32) | |
s = s.split(b' ')[1:] | |
self.width, self.height = tuple(int(dim) for dim in s) | |
def in_bounds(self, x, y): | |
return in_bounds(x, y, 0, 0, self.width - 1, self.height - 1) | |
def constrain(self, x, y): | |
return constrain(x, y, 0, 0, self.width - 1, self.height - 1) | |
def plot(self, x, y, c='ffffff') -> bool: | |
if not self.in_bounds(x, y): | |
return False | |
self.send(b'PX %d %d %b\n' % (x, y, bytes(c, encoding='UTF-8'))) | |
return True | |
def rect(self, x, y, xmax, ymax, c='ffffff'): | |
for j in range(y, ymax + 1): | |
for i in range(x, xmax + 1): | |
self.plot(i, j, c) | |
def fill(self, c='000000'): | |
self.rect(0, 0, self.width, self.height, c) | |
def eat(self, body='ffff00', trail='000000'): | |
pacman = ''' | |
......... | |
..*********.. | |
..*************.. | |
..*****************.. | |
.*********************. | |
.***********************. | |
.*************************. | |
.*************************. | |
.***************************. | |
.***************************. | |
.****************************. | |
.**************************.. | |
.*************************.. | |
.***********************.. | |
.*********************.. | |
.*******************.. | |
.*****************.. | |
.*****************.. | |
.*******************.. | |
.*********************.. | |
.***********************.. | |
.*************************.. | |
.**************************.. | |
.****************************. | |
.***************************. | |
.***************************. | |
.*************************. | |
.*************************. | |
.***********************. | |
.*********************. | |
..*****************.. | |
..*************.. | |
..*********.. | |
.........''' | |
cmap = {'*': body, '.': trail} | |
while True: | |
y = randrange(self.height) | |
for lhs in range(-20, self.width - 20): | |
for j, line in enumerate(pacman.split('\n')): | |
for i, c in enumerate(line): | |
try: | |
self.plot((lhs + i) % self.width, y + j, cmap[c]) | |
except KeyError: | |
continue | |
def bright_color() -> str: | |
color = [255, 255, randrange(0, 256)] | |
shuffle(color) | |
return '%02x%02x%02x' % tuple(color) | |
with Field(HOST, PORT) as f: | |
Thread(target=lambda: f.fill('222222')).start() | |
Thread(target=lambda: f.eat(body=bright_color())).start() | |
while True: | |
x = randrange(0, f.width) | |
y = randrange(0, f.height) | |
size = randrange(10, 100) | |
f.rect(x, y, x + size, y + size, bright_color()) | |
sleep(0.3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment