aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar rcoh <rcoh@mit.edu>2011-08-28 23:48:21 -0400
committerGravatar rcoh <rcoh@mit.edu>2011-08-28 23:48:21 -0400
commitf02e5cc761a7dff602d41754a018376e19227fa1 (patch)
treef324eb9f1f7ed269161f86cfb1f27a4acb7dc78e
parentcb161151aa4001a7b71a4d2610c8a9f462cf1acd (diff)
Adding "Space" functionality to ddrinput, adding untested support for Leds in the renderer.
-rw-r--r--ddrinput.py4
-rw-r--r--renderer.py38
-rw-r--r--tetris.py9
-rw-r--r--util.py4
4 files changed, 39 insertions, 16 deletions
diff --git a/ddrinput.py b/ddrinput.py
index a1bf482..f374781 100644
--- a/ddrinput.py
+++ b/ddrinput.py
@@ -3,7 +3,7 @@ JOY_EVENT = 7
KEY_EVENT = 2
X = 0
Y = 1
-(LEFT, RIGHT, UP, DOWN) = range(4)
+(LEFT, RIGHT, UP, DOWN, DROP) = range(5)
KEY_LEFT = 276
KEY_UP = 273
KEY_DOWN = 274
@@ -15,7 +15,7 @@ KEY_W = 119
KEY_SPACE = 32
KEY_ESC = 27
-DIRECTIONS = {0:'LEFT', 1:'RIGHT', 2:'UP', 3:'DOWN'}
+DIRECTIONS = {0:'LEFT', 1:'RIGHT', 2:'UP', 3:'DOWN', 5:'DROP'}
class DdrInput(object):
"""
DdrInput is a class to get input from the particular DDR pads and adapters we have. It is not
diff --git a/renderer.py b/renderer.py
index d96f6e0..1e40c7b 100644
--- a/renderer.py
+++ b/renderer.py
@@ -1,3 +1,10 @@
+from numpy import zeros
+
+import pygame
+from pygame.locals import Color
+
+import util
+
class Renderer(object):
def render_game(self, game_board):
"""
@@ -10,8 +17,6 @@ class Renderer(object):
def color_deref(self, color_str):
return Color(color_str)
-import pygame
-from pygame.locals import Color
class PygameRenderer(Renderer):
"""
@@ -53,31 +58,48 @@ class PygameRenderer(Renderer):
self.screen.blit(self.background, (0,0))
pygame.display.flip()
-import util
class LedRenderer(Renderer):
"""
Renderer for the LEDs. Based heavily on IndoorRenderer in Smootlight and
general Smootlight abstraction patterns
"""
- POWER_SUPPLY_IPS = [0,0,0,0] #TODO: Fill in
+ POWER_SUPPLY_IPS = ['10.32.97.17',0,0,0] #TODO: Fill in
SOCK_PORT = 6038
sockets = {}
def render_game(self, game_board):
packets = self.map_to_packets(game_board)
packets_with_destinations = zip(self.POWER_SUPPLY_IPS, packets)
- for (ip, (port, packet)) in packets:
+ for (ip, (port, packet)) in packets_with_destinations:
if not ip in self.sockets:
self.sockets[ip] = util.getConnectedSocket(ip, self.SOCK_PORT)
final_packet = util.composePixelStripPacket(packet, port)
try:
- self.sockets[ip].send(packet, 0x00)
+ if self.sockets[ip] != None:
+ self.sockets[ip].send(final_packet, 0x00)
except:
print 'failure sending packet'
- def map_to_packets(game_board):
+ def map_to_packets(self, game_board):
"""
Performs the mapping between a game_board and a list of (port,packet) pairs. The port,packet
pairs should line up with the ip's in IP_ADDRESSES
"""
- #TODO(rcoh): Write this when we decide on a layout
+ #This is hardcoded, mostly because I'm curious of the complexity
+ packets = []
+ board_x_min = 0
+ board_x_max = 10
+ section_width = 10
+ section_height = 5
+ for board_x_min in [0, 10]:
+ packet = []
+ for y_start in [20, 15, 10, 5]:
+ strip = zeros((50,3),'ubyte')
+ index = 0
+ for y in range(board_y_min+section_height, board_y_min, -1):
+ for x in range(board_x_min+section_width, board_x_min, -1):
+ strip[index] = self.color_deref(game_board[(x,y)])
+ packet.append((1+len(packet), strip))
+ packets.append(packet)
+
+ return packets
diff --git a/tetris.py b/tetris.py
index 4d3bb8f..1486369 100644
--- a/tetris.py
+++ b/tetris.py
@@ -13,6 +13,7 @@ from time import sleep, time
import random
import sys
from renderer import PygameRenderer
+from renderer import LedRenderer
from tetris_shape import *
from ddrinput import DdrInput
from ddrinput import DIRECTIONS
@@ -189,7 +190,7 @@ class Player():
#contains variables that are shared between the players:
#levels, delay time, etc
class GameState():
- def __init__(self, gui):
+ def __init__(self):
self.shapes = [square_shape, t_shape,l_shape, reverse_l_shape,
z_shape, s_shape,i_shape ]
self.num_players = 0
@@ -206,7 +207,7 @@ class TetrisGame(object):
#one-time initialization for gui etc
def __init__(self):
print "initialize tetris"
- self.gui = PygameRenderer()
+ self.gui = [PygameRenderer(), LedRenderer()]
self.input = DdrInput()
while True:
self.init_game()
@@ -216,7 +217,7 @@ class TetrisGame(object):
print "init next game"
self.boards = [Board(MAXX,MAXY), Board(MAXX,MAXY)]
self.players = [None,None]
- self.gameState = GameState(self.gui)
+ self.gameState = GameState()
self.board_animation(0,"up_arrow")
self.board_animation(1,"up_arrow")
self.update_gui()
@@ -290,7 +291,7 @@ class TetrisGame(object):
p.move_my_shape()
def update_gui(self):
- self.gui.render_game(self.to_dict())
+ [gui.render_game(self.to_dict()) for gui in self.gui]
def end_game(self):
if self.gameState.winner!=None:
diff --git a/util.py b/util.py
index 97982e1..bb6779a 100644
--- a/util.py
+++ b/util.py
@@ -1,16 +1,16 @@
from numpy import zeros
+import socket
argDict = {'flags': 0, 'startcode': 0x0fff, 'pad':0}
# Allocate a buffer for transmitted packets and fill it with magic
# Only works for strips of 50 pixels
xmit = zeros(174, dtype='ubyte')
-xmit[:8], xmit[20:24] = [4,1,220,74,1,0,8,1], [150,0,255,15]
+xmit[:8], xmit[20:25] = [4,1,220,74,1,0,8,1], [150,0,255,15,191]
def composePixelStripPacket(values, port):
xmit[16], xmit[24:] = port, values.ravel()
return xmit
-import socket
def getConnectedSocket(ip,port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try: