From e20ec6d9ec09d955ef7eca1d1375bbf49394584c Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 16 May 2014 15:44:50 +0900 Subject: [PATCH] Collision detection --- tetris.py | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tetris.py b/tetris.py index 06d1fa6..d763522 100755 --- a/tetris.py +++ b/tetris.py @@ -29,7 +29,7 @@ class Tetrad: def color(self): return self.block_configs[self.config][0] - + def layout(self): layout = self.block_configs[self.config][1][self.rotation] return map(lambda p: (self.position[0]+p[0], self.position[1]+p[1]), layout) @@ -51,7 +51,6 @@ class Tetrad: return Tetrad(self.config, self.position, (self.rotation + 1) % self.block_rotations) - # # Board # @@ -115,6 +114,18 @@ class Board: return pygame.Rect(top_left, self.block_dims) + def can_place_tetrad(self, tetrad): + for point in tetrad.layout(): + if point[0] < 0 or point[1] < 0: + return False + if point[0] >= self.grid_dims[0] or point[1] >= self.grid_dims[1]: + return False + if self.blocks[point[0]][point[1]] != 0: + return False + + return True + + # # Game # @@ -133,20 +144,25 @@ class Game: pass + def try_tetrad_action(self, tetrad): + if self.board.can_place_tetrad(tetrad): + self.tetrad = tetrad + + def move_left(self): - self.tetrad = self.tetrad.moved_left() + self.try_tetrad_action(self.tetrad.moved_left()) def move_right(self): - self.tetrad = self.tetrad.moved_right() + self.try_tetrad_action(self.tetrad.moved_right()) def move_down(self): - self.tetrad = self.tetrad.moved_down() + self.try_tetrad_action(self.tetrad.moved_down()) def rotate(self): - self.tetrad = self.tetrad.rotated() + self.try_tetrad_action(self.tetrad.rotated()) # @@ -217,22 +233,6 @@ class Engine: return True - def move_right(self): - print 'right' - - - def move_left(self): - print 'left' - - - def move_down(self): - print 'down' - - - def rotate(self): - print 'rotate' - - # # Entry #