aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Leah Alpert <lalpert@mit.edu>2011-08-29 18:50:20 -0700
committerGravatar Leah Alpert <lalpert@mit.edu>2011-08-29 18:50:20 -0700
commitfcd6f419d2252188c84990f2af19a7fb4c6687ea (patch)
tree8036a234585027ca1952c49c13332e63997dfcb9
parent5050c33c2df4d126b4dfa17b09379b6b5922e657 (diff)
Changed to gui-based tetris
-rw-r--r--renderer.py46
-rw-r--r--tetris.py31
2 files changed, 65 insertions, 12 deletions
diff --git a/renderer.py b/renderer.py
index c057f3d..949df38 100644
--- a/renderer.py
+++ b/renderer.py
@@ -37,29 +37,53 @@ class PygameGoodRenderer(Renderer):
self.background.fill(Color(0,0,0))
def render_game(self, game_board):
+ #print game_board
self.background.fill(Color(0,0,0))
x0 = self.OFFSET[0] - 1
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
+
+ font = pygame.font.Font(None, 22)
+
+ for n in [0,1]:
+ if (n,"score") in game_board:
+ score = game_board[(n,"score")]
+ score_string = "Score: %d" % (score,)
+ text = font.render(score_string, 1, (255,255,255))
+ textpos = (x0 + self.SCALE*3 + b2*n,y1 - self.SCALE- 6)
+ self.background.blit(text, textpos)
+
+ if (2,"level") in game_board:
+ level = game_board[(2,"level")]
+ level_string = "Level: %d" % (level,)
+ text = font.render(level_string, 1, (255,255,255))
+ textpos = (x0 + self.SCALE * 6 ,y0 - self.SCALE * 2)
+ self.background.blit(text, textpos)
+
+ if (2,"time_left") in game_board:
+ time = game_board[(2,"time_left")]
+ time_string = "Time left: %d" % (round(time),)
+ text = font.render(time_string, 1, (255,255,255))
+ textpos = (x1 + self.SCALE * 3 ,y0 - self.SCALE * 2)
+ self.background.blit(text, textpos)
+
+
line_endpoints = [((x0,y0), (x0,y1)), ((x0,y1), (x1,y1)), ((x1,y1), (x1,y0)), ((x1,y0), (x0,y0)),
- ((x0,y1 - 15), (x1,y1 - 15)), ((x0,y1 - 30), (x1,y1 - 30))]
+ ((x0,y1 - self.SCALE*2), (x1,y1 - self.SCALE*2))]
for p1,p2 in line_endpoints:
pygame.draw.line(self.background, self.color_deref("white"), p1, p2)
pygame.draw.line(self.background, self.color_deref("white"), (p1[0]+b2,p1[1]),(p2[0]+b2,p2[1]))
- x_mid = (x0+x1)/2 + self.SCALE
- pygame.draw.line(self.background, self.color_deref("white"), (x_mid,y1 - 15),(x_mid,y1 - 30))
- pygame.draw.line(self.background, self.color_deref("white"), (x_mid+b2,y1 - 15),(x_mid+b2,y1 - 30))
-
for (x,y) in game_board:
- disp_x = x
- if x >= 10:
- disp_x+=3
- 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))
-
+ if y not in ["level","time_left","score"]:
+ disp_x = x
+ if x >= 10:
+ disp_x+=3
+ 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))
+
self.screen.blit(self.background, (0,0))
pygame.display.flip()
diff --git a/tetris.py b/tetris.py
index 1d90d8d..6ba750a 100644
--- a/tetris.py
+++ b/tetris.py
@@ -295,7 +295,8 @@ class TetrisGame(object):
p.handle_move(DOWN)
def update_gui(self):
- [gui.render_game(self.to_dict()) for gui in self.gui]
+ #[gui.render_game(self.to_dict()) for gui in self.gui]
+ self.gui[0].render_game(self.to_gui_dict())
def end_game(self):
if self.gameState.winner!=None:
@@ -398,6 +399,34 @@ class TetrisGame(object):
d[coord] = "white"
return d
+
+ def to_gui_dict(self):
+ d = {}
+ if self.start_time!=None:
+ d[(2,"level")] = self.gameState.level
+ d[(2,"time_left")] = self.start_time + TIME_LIMIT - time()
+
+ for n in range(2):
+ board = self.boards[n]
+ offset = n*MAXX
+
+ #blocks
+ for (x,y) in board.landed:
+ d[(x+offset,y)] = board.landed[(x,y)]
+
+ if self.players[n]!=None:
+ p = self.players[n]
+ #score
+ d[(n,"score")] = p.score
+
+ #shapes
+ if p.shape:
+ blocks = p.shape.blocks
+ for b in blocks:
+ if b.y >= 0:
+ d[(b.x+offset*n,b.y)] = b.color
+
+ return d
if __name__ == "__main__":