Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Code for new boards. Also some tron stuff I guess.

  • Loading branch information...
commit bc58aa13b534bac8c1285edf3ace019865a48a74 1 parent b2a62fb
Russell Cohen authored
Showing with 106 additions and 49 deletions.
  1. +5 −4 ddrinput.py
  2. +61 −3 renderer.py
  3. +3 −2 tetris.py
  4. +37 −40 tron.py
View
9 ddrinput.py
@@ -85,10 +85,11 @@ def poll(self):
player_index, player_move = self.handle_joy_event(event)
if self.debug_mode:
print (player_index, player_move)
- #if self.debug_mode:
- # if event.type == KEY_EVENT or event.type == KEY_RELEASE:
- # (player_index, player_move) = self.handle_key_event(event)
-
+ #debug mode
+ if self.debug_mode:
+ if event.type == KEY_EVENT or event.type == KEY_RELEASE:
+ (player_index, player_move) = self.handle_key_event(event)
+ #end debug mode
if player_move != None:
if player_move == RELEASE:
View
64 renderer.py
@@ -48,6 +48,7 @@ class PygameGoodRenderer(Renderer):
OFFSET = (50, 50)
SCALE = 20
RADIUS = 6
+ GAP = 0
def __init__(self):
pygame.init()
@@ -63,7 +64,7 @@ def render_game(self, game_board):
y0 = self.OFFSET[1] - 1
x1 = self.OFFSET[0] + 10*self.SCALE
y1 = self.OFFSET[1] + 20*self.SCALE - 1
- b2 = self.SCALE * 13 #x offset for second board
+ b2 = self.SCALE * (10 + self.GAP) #x offset for second board
font = pygame.font.Font(None, 22)
@@ -100,7 +101,7 @@ def render_game(self, game_board):
if y not in ["level","time_left","score"]:
disp_x = x
if x >= 10:
- disp_x+=3
+ disp_x+=self.GAP
pygame.draw.rect(self.background, self.color_deref(game_board[(x,y)]),
(self.OFFSET[0] + disp_x*self.SCALE, self.OFFSET[1] + y*self.SCALE, self.SCALE-1, self.SCALE-1))
@@ -165,6 +166,19 @@ class LedRenderer(Renderer):
def render_game(self, game_board):
packets = self.map_to_packets(game_board)
+ for (ip, port) in packets:
+ packet = packets[(ip, port)]
+ if not ip in self.sockets:
+ self.sockets[ip] = util.getConnectedSocket(ip, self.SOCK_PORT)
+ final_packet = util.composePixelStripPacket(packet, port)
+ try:
+ if self.sockets[ip] != None:
+ self.sockets[ip].send(final_packet, 0x00)
+ except:
+ print 'failure sending packet'
+
+ def old_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_with_destinations:
@@ -184,8 +198,52 @@ def fake_map_to_packets(self, game_board):
strip = zeros((50,3), 'ubyte')
strip[:] = (255,255,0)
return [(1, strip), (2, strip)] * 4
-
+
def map_to_packets(self, game_board):
+ #start (x,y), dir (x,y),
+ boards = [((10, 4), (1, -1), '10.32.0.31', 2), #BOARD 1 TOP
+ ((10, 5), (1, 1), '10.32.0.31', 1), #BOARD 1 MIDDLE UP
+ ((10,14), (1, -1), '10.32.0.32', 1), #BOARD 1 MIDDLE LOW
+ ((10,15), (1, 1), '10.32.0.32', 2), #BOARD 1 BOTTOM
+ ((9, 4), (-1, -1), '10.32.0.33', 2),
+ ((9, 5), (-1, 1), '10.32.0.33', 1),
+ ((9, 10), (-1, 1), '10.32.0.35', 2), #WIRED BACKWARDS
+ ((9, 19), (-1, -1), '10.32.0.35', 1)] #WIRED BACKWARDS
+ width = 10
+ ret = {}
+ for (start_x, start_y), (dir_x, dir_y), ip, port in boards:
+ strip = zeros((50, 3), 'ubyte')
+ index = 0
+ for y in range(start_y, start_y+(5*dir_y), dir_y):
+ #line is running other direction if we aren't the start_line xor we are going backwards
+ #anyway
+ if (dir_x == 1):
+ line_diff = bool((y-start_y) % 2)
+ if line_diff:
+ act_dir_x = dir_x * -1
+ act_start_x = start_x + width - 1
+ else:
+ act_dir_x = dir_x
+ act_start_x = start_x
+ else:
+ line_diff = bool((y-start_y) % 2)
+ if line_diff:
+ act_dir_x = 1
+ act_start_x = start_x - (width - 1)
+ else:
+ act_start_x = start_x
+ act_dir_x = dir_x
+ for x in range(act_start_x, act_start_x+(width*act_dir_x), act_dir_x):
+ if (x,y) in game_board:
+ strip[index] = self.color_deref(game_board[(x,y)])
+ index += 1
+ assert index == 50
+ ret[(ip, port)] = strip
+ return ret
+
+
+
+ def old_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
View
5 tetris.py
@@ -40,7 +40,7 @@
TIME_LIMIT = 5 * 60 #seconds
LINES_TO_ADVANCE = 5 #num lines needed to advance to next level
-LEVEL_SPEEDS = [500,400,350,300,250,100]
+LEVEL_SPEEDS = [500, 400, 350, 300, 250, 100]
MAXX = 10
MAXY = 18
@@ -313,7 +313,8 @@ def gravity(self):
p.handle_move(DOWN)
def update_gui(self):
- [gui.render_game(self.to_dict()) for gui in self.gui]
+ d = self.to_dict()
+ [gui.render_game(d) for gui in self.gui]
#self.gui[0].render_game(self.to_gui_dict())
def end_game(self):
View
77 tron.py
@@ -39,55 +39,44 @@
TIME_LIMIT = 5 * 60 #seconds
LINES_TO_ADVANCE = 8 #num lines needed to advance to next level
-LEVEL_SPEEDS = [300,250,200,150]
+LEVEL_SPEEDS = [200,150]
MAXX = 20
MAXY = 18
-(RIGHT, LEFT, UP, DOWN, DIE) = range(5)
-MOVES = {RIGHT:(1,0), LEFT:(-1,0), UP:(1,0), DOWN:(-1,0)}
+(LEFT, RIGHT, UP, DOWN, DROP, DIE) = range(6)
+MOVES = {LEFT:(-1,0), RIGHT:(1,0), UP:(0,-1), DOWN:(0,1)}
LEVEL_COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
-#one 20x18 board shared between players
-class Board():
- """
- The board represents the playing area. A grid of x by y blocks.
- Stores blocks that are full.
- """
- def __init__(self, max_x=20, max_y=18):
- # blocks are stored in dict of (x,y)->"color"
- self.grid = {}
- self.max_x = max_x
- self.max_y = max_y
-
- def clear(self):
- self.grid = {}
-
#represents a player. each player has a board, current direction, score, etc
class Player():
def __init__(self, player_id, gs, board):
self.id = player_id
self.board = board
- if player_id == 0:
- x_pos = 3
- else:
- x_pos = MAXX - 3
+ x_pos = [3, MAXX - 4][player_id]
+ print "player id= ", player_id
+ print "x_pos = ", x_pos
self.position = (x_pos, MAXY/2)
self.score = 0
self.gs = gs
- self.direction = player_id #0 is right, 1 is left
+ self.direction = [RIGHT,LEFT][player_id] #0 is right, 1 is left
- def handle_move(self, dir):
+ def handle_move(self, d):
#add handling for case when dir is opposite direction of movement
- self.direction = dir
+ if (self.direction==LEFT and d==RIGHT) or (self.direction==RIGHT and d==LEFT) or (self.direction==UP and d==DOWN) or (self.direction==DOWN and d==UP):
+ return
+ else:
+ self.direction = d
def move_tron(self):
- self.position[0] += MOVES[self.direction][0]
- self.position[1] += MOVES[self.direction][1]
- if self.position in self.board:
+ old_pos = self.position
+ self.position = (self.position[0] + MOVES[self.direction][0],
+ self.position[1] + MOVES[self.direction][1])
+ if self.position in self.board or self.position[0] < 0 or self.position[0] >= MAXX or self.position[1] < 0 or self.position[1] >= MAXY:
#you hit something, you lose!
self.gs.state = "ending"
- self.gs.winner = (self.id + 1) % 2
+ self.gs.winner = [self.id, (self.id + 1) % 2][self.gs.num_players - 1]
+ self.board[old_pos] = 2 #yellow
return
else:
self.board[self.position] = self.id
@@ -116,12 +105,12 @@ def __init__(self):
#initializes each game
def init_game(self):
print "init next game"
- self.board = Board(MAXX,MAXY)
+ self.animation = {}
+ self.board = {}
self.players = [None,None]
self.gameState = GameState()
self.board_animation(0,"up_arrow")
self.board_animation(1,"up_arrow")
- self.board.clear()
self.start_time = None
self.input.reset()
self.update_gui()
@@ -138,6 +127,7 @@ def add_player(self,num): # 0=left, 1=right
def start_game(self):
print "start game"
+ self.animation.clear()
self.gameState.state = "playing"
self.start_time = time()
self.drop_time = time()
@@ -189,7 +179,8 @@ def gravity(self):
p.move_tron()
def update_gui(self):
- [gui.render_game(self.to_dict()) for gui in self.gui]
+ d = self.to_dict()
+ [gui.render_game(d) for gui in self.gui]
#self.gui[0].render_game(self.to_gui_dict())
def end_game(self):
@@ -210,18 +201,18 @@ def end_game(self):
winner_id = 1
self.animate_ending(winner_id)
- def board_animation(self, board_id, design, color="green"):
- b = self.boards[board_id]
+ def board_animation(self, board_id, design, color=1):
d = self.create_shapes(design)
for coord in d:
- b.landed[coord]=color
+ c = (coord[0] + board_id * MAXX/2, coord[1])
+ self.animation[c]=color
def animate_ending(self,winner_board):
if winner_board == 2:
self.board_animation(0,"outline")
self.board_animation(1,"outline")
else:
- self.board_animation(winner_board,"outline","yellow")
+ self.board_animation(winner_board,"outline",2)
self.update_gui()
sleep(3)
@@ -245,19 +236,25 @@ def create_shapes(self,design): #in progress.....
shapes["down_arrow"] = down_arrow
shapes["up_arrow"] = up_arrow
shapes["outline"] = outline
+ shapes["whole_outline"] = outline
shapes["test"] = [(5,5)]
return shapes[design]
def to_dict(self):
- player_colors = ["red", "green"]
+ player_colors = ["red", "green", "yellow"]
d = {}
- for (x,y) in board.grid:
- d[(x,y)] = player_colors[board.grid[(x,y)]]
+
+ for (x,y) in self.board:
+ d[(x,y)] = player_colors[self.board[(x,y)]]
+
+ for (x,y) in self.animation:
+ d[(x,y)] = player_colors[self.animation[(x,y)]]
for n in range(2):
if self.players[n]!=None:
p = self.players[n]
+ offset = n*(MAXX/2)
#score
score = p.score
@@ -279,7 +276,7 @@ def to_dict(self):
if time_left/60 >= i:
seconds = time_left - 60*i # is in .5-1 secs, etc
if not (.5<seconds<1.0 or 1.5<seconds<2.0 or 2.5<seconds<3.0):
- coord = (MAXX-1-i + offset, MAXY)
+ coord = (MAXX/2-1-i + offset, MAXY)
d[coord] = "white"
return d
Please sign in to comment.
Something went wrong with that request. Please try again.