From 6de3f45ba320bcf59d088d8812fbf614997e2974 Mon Sep 17 00:00:00 2001 From: Giovani Date: Fri, 11 Jun 2021 15:15:15 -0400 Subject: [PATCH] refactor: improve piece class readability --- entity/Entity.py | 10 ++++- entity/Piece.py | 99 +++++++++++++++++++++++++++++++----------------- 2 files changed, 74 insertions(+), 35 deletions(-) diff --git a/entity/Entity.py b/entity/Entity.py index 83fccbb..b3af22b 100644 --- a/entity/Entity.py +++ b/entity/Entity.py @@ -1,4 +1,4 @@ -from typing import Tuple +from typing import List, Tuple import pygame from util.ConfigurationManager import ConfigurationManager @@ -28,3 +28,11 @@ class Entity: if square_one[i][0] == square_two[i][0] and square_one[i][1] == square_two[i][1]: return True return False + + def collide_points(self, points: List) -> bool: # TODO change name later + for square_one in self.points: + for square_two in points: + for i in range(4): # 4 vertices + if square_one[i][0] == square_two[i][0] and square_one[i][1] == square_two[i][1]: + return True + return False diff --git a/entity/Piece.py b/entity/Piece.py index 1c3d038..da3b586 100644 --- a/entity/Piece.py +++ b/entity/Piece.py @@ -17,39 +17,30 @@ class Piece(Entity): self.inner_border_color = inner_border_color self.center = self.__get_center(shape, position) - self.gravity_time = ConfigurationManager.configuration["engine"]["piece-gravity-time"] - self.current_gravity_time = 0 - self.set_time = ConfigurationManager.configuration["engine"]["piece-set-time"] - self.current_set_time = 0 self.piece_set_sound = mixer.Channel(2) - self.previous_points = None self.previous_center = None + # Gravity + self.gravity_time = ConfigurationManager.configuration["engine"]["piece-gravity-time"] + self.current_gravity_time = 0 + self.applying_gravity = True + + # Set + self.set_time = ConfigurationManager.configuration["engine"]["piece-set-time"] + self.current_set_time = 0 + self.applying_set = False + def update(self, elapsed_time: int, tetris) -> None: super().update(elapsed_time) - well = tetris.well - stack = tetris.stack + if self.applying_gravity: + self.applying_gravity = self.__apply_gravity(elapsed_time, tetris.well, tetris.stack) + self.applying_set = not self.applying_gravity - tile_size = ConfigurationManager.configuration["engine"]["tile-size"] - if self.elapsed_time >= self.gravity_time and self.current_set_time == 0: - self.elapsed_time = 0 - - self.move((0, tile_size)) - if well and self.collide(well) or stack and self.collide(stack): - self.revert() - self.current_set_time += elapsed_time - - if self.current_set_time > 0: - self.current_set_time += elapsed_time - - if self.current_set_time >= self.set_time: - self.__play_piece_set_sound() - self.current_set_time = 0 - - stack.add_piece(self) - tetris.current_piece = None + if self.applying_set: + self.applying_set = self.__apply_set(elapsed_time, tetris) + self.applying_gravity = not self.applying_set def draw(self, surface: pygame.Surface) -> None: super().draw(surface) @@ -65,20 +56,16 @@ class Piece(Entity): pygame.draw.polygon(surface, pygame.Color(self.inner_border_color), new_square, max(tile_size // 6, 1)) 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) self.center[0] += vector[0] self.center[1] += vector[1] - for sub_points in self.points: - for point in sub_points: - point[0] += vector[0] - point[1] += vector[1] + for square in self.points: + for vertex in square: + vertex[0] += vector[0] + vertex[1] += vector[1] ''' For more information on a rotation of a piece go here: @@ -104,6 +91,16 @@ class Piece(Entity): self.points = self.previous_points self.center = self.previous_center + def __mimic_move(self, vector: Tuple): # TODO figure out how to annotate return type as Piece + mimic_points = copy.deepcopy(self.points) + + for square in mimic_points: + for vertex in square: + vertex[0] += vector[0] + vertex[1] += vector[1] + + return mimic_points + def __play_piece_set_sound(self) -> None: piece_set_sound_file = ConfigurationManager.configuration["sound"]["piece-set"] self.piece_set_sound.play(mixer.Sound(piece_set_sound_file)) @@ -125,9 +122,43 @@ class Piece(Entity): tile_size = ConfigurationManager.configuration["engine"]["tile-size"] center = shape[-1] - # cast to int and avoid exception from pygame + # cast to int and avoid exception from pygame (center can be a floating point) return [int(center[0] * tile_size + position[0]), int(center[1] * tile_size + position[1])] + def __apply_gravity(self, elapsed_time, well, stack) -> bool: # TODO define well and stack type + tile_size = ConfigurationManager.configuration["engine"]["tile-size"] + self.current_gravity_time += elapsed_time + + if self.current_gravity_time >= self.gravity_time: + self.current_gravity_time = 0 + if not self.__entity_is_below(well) and not self.__entity_is_below(stack): + self.move((0, tile_size)) + else: + return False + + return True + + def __apply_set(self, elapsed_time, tetris) -> bool: # TODO define tetris type + self.current_set_time += elapsed_time + + if self.current_set_time >= self.set_time: + self.current_set_time = 0 + if self.__entity_is_below(tetris.well) or self.__entity_is_below(tetris.stack): + self.__play_piece_set_sound() + tetris.stack.add_piece(self) # TODO do on tetris object level? + tetris.current_piece = None # TODO turn into a method + else: + return False + + return True + + def __entity_is_below(self, entity: Entity) -> bool: # TODO + tile_size = ConfigurationManager.configuration["engine"]["tile-size"] + mimic_points = self.__mimic_move((0, tile_size)) + + return entity and entity.collide_points(mimic_points) + + # shape attributes I_SHAPE = (((0, 0), (1, 0), (1, 1), (0, 1)), ((1, 0), (2, 0), (2, 1), (1, 1)), ((2, 0), (3, 0), (3, 1), (2, 1)), ((3, 0), (4, 0), (4, 1), (3, 1)), (2, 0)) J_SHAPE = (((0, 0), (1, 0), (1, 1), (0, 1)), ((1, 0), (2, 0), (2, 1), (1, 1)), ((2, 0), (3, 0), (3, 1), (2, 1)), ((2, 1), (3, 1), (3, 2), (2, 2)), (1.5, 0.5))