Created
February 17, 2014 00:53
-
-
Save mkosler/9042885 to your computer and use it in GitHub Desktop.
TwitchDrawsPokemon
This file contains 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
import re, socket, time | |
from PIL import Image | |
def send(sock, msg): | |
print(">> {}".format(msg)) | |
sock.send(msg + "\r\n") | |
if __name__ == "__main__": | |
IMG = Image.new("RGB", (128, 128), "white") | |
PIXELS = IMG.load() | |
RGB = [ 0, 0, 0 ] | |
CURSOR = [ 0, 0 ] | |
RECV_BUFFER = 4096 | |
VALID_COMMANDS = [ "left", "right", "down", "up", "a", "b", "start" ] | |
HOSTNAME = "irc.twitch.tv" | |
PORT = 6667 | |
CHANNEL = "#twitchplayspokemon" | |
NICKNAME = "ircplays" | |
REALNAME = "IRC Plays..." | |
OAUTH = "" | |
with open("oauth") as f: | |
OAUTH = f.read().replace("\n", "") | |
IRC = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
IRC.connect((HOSTNAME, PORT)) | |
print("Connecting to {}:{} {}".format(HOSTNAME, PORT, CHANNEL)) | |
IRC.send("PASS {}\r\n".format(OAUTH)) | |
IRC.send("NICK {}\r\n".format(NICKNAME)) | |
IRC.send("USER {} {} {} :{}\r\n".format(NICKNAME, HOSTNAME, "", REALNAME)) | |
IRC.send("JOIN {}\r\n".format(CHANNEL)) | |
START = time.clock() | |
while True: | |
data = IRC.recv(RECV_BUFFER) | |
if data.find("PING") != -1: | |
send(IRC, data.replace("PING", "PONG")) | |
if data.find("PRIVMSG") != -1: | |
for command in re.findall(r":(\w+)", data): | |
if command in VALID_COMMANDS: | |
if command == "left": | |
CURSOR[0] = max(0, CURSOR[0] - 1) | |
elif command == "right": | |
CURSOR[0] = min(IMG.size[0] - 1, CURSOR[0] + 1) | |
elif command == "up": | |
CURSOR[1] = max(0, CURSOR[1] - 1) | |
elif command == "down": | |
CURSOR[1] = min(IMG.size[1] - 1, CURSOR[1] + 1) | |
elif command == "a": | |
RGB[0] = (RGB[0] + 10) % 255 | |
elif command == "b": | |
RGB[1] = (RGB[1] + 10) % 255 | |
elif command == "start": | |
RGB[2] = (RGB[2] + 10) % 255 | |
print(command, CURSOR, RGB) | |
PIXELS[CURSOR[0], CURSOR[1]] = tuple(RGB) | |
if time.clock() - START >= 5.0: | |
print("Saving image...") | |
START = time.clock() | |
IMG.save("tpp.jpeg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment