diff --git a/Tetris.py b/Tetris.py index 4d1ef6d..f6cc2df 100644 --- a/Tetris.py +++ b/Tetris.py @@ -40,9 +40,11 @@ class Tetris: # gets called from the games main loop def update(self) -> None: # TODO write not initialized exception + elapsed_time = self.clock.tick(self.fps) + + if self.current_piece: + self.current_piece.update(elapsed_time, self.well, self.stack) - self.clock.tick(self.fps) - # TODO create control utility class for event in pygame.event.get(): if event.type == pygame.QUIT: diff --git a/entity/Entity.py b/entity/Entity.py index e7f3159..83fccbb 100644 --- a/entity/Entity.py +++ b/entity/Entity.py @@ -8,9 +8,10 @@ class Entity: self.points = points self.color = color self.border_color = border_color + self.elapsed_time = 0 - def update(self) -> None: - pass + def update(self, elapsed_time) -> None: + self.elapsed_time += elapsed_time def draw(self, surface: pygame.Surface) -> None: tile_size = ConfigurationManager.configuration["engine"]["tile-size"] @@ -20,7 +21,7 @@ class Entity: if self.border_color: pygame.draw.polygon(surface, pygame.Color(self.border_color), square, max(tile_size // 6, 1)) - def collide(self, entity) -> bool: + def collide(self, entity) -> bool: # TODO figure out how to do type hint for entity param of type Entity for square_one in self.points: for square_two in entity.points: for i in range(4): # 4 vertices diff --git a/entity/Piece.py b/entity/Piece.py index c63ced8..9c621b3 100644 --- a/entity/Piece.py +++ b/entity/Piece.py @@ -9,7 +9,9 @@ from util.ConfigurationManager import ConfigurationManager https://tetris.fandom.com/wiki/Tetromino ''' class Piece(Entity): - + + GRAVITY = 800 # A move down every 800 ms + def __init__(self, shape: Tuple, position: Tuple, color: str, border_color: str): super().__init__(self.__get_points(shape, position), color, border_color) self.center = self.__get_center(shape, position) @@ -17,7 +19,24 @@ class Piece(Entity): self.previous_points = None self.previous_center = None + def update(self, elapsed_time: int, well: Entity, stack: Entity): + super().update(elapsed_time) + + tile_size = ConfigurationManager.configuration["engine"]["tile-size"] + if self.elapsed_time >= Piece.GRAVITY: + self.elapsed_time -= Piece.GRAVITY + + self.move((0, tile_size)) + if well and self.collide(well): + self.revert() + if stack and self.collide(stack): + self.revert() + def move(self, vector: Tuple) -> None: + # reset elapsed time if user moves down to stall gravity + if vector[1] > 0: + self.elapsed_time = 0 + self.previous_points = copy.deepcopy(self.points) self.previous_center = copy.deepcopy(self.center)