diff --git a/Tetris.py b/Tetris.py index 002ba42..d6a37c1 100644 --- a/Tetris.py +++ b/Tetris.py @@ -34,15 +34,15 @@ class Tetris: self.screen = pygame.display.set_mode((win_width, win_height)) self.clock = pygame.time.Clock() self.main_music = mixer.Channel(0) - self.well = Well((280, 80), ConfigurationManager.configuration["color"]["border"]) # TODO calculate position later and redo color config for well - self.stack = Stack(ConfigurationManager.configuration["color"]["base-4"], ConfigurationManager.configuration["color"]["border"]) + self.well = Well((280, 80), ConfigurationManager.configuration["color"]["well-1"], ConfigurationManager.configuration["color"]["well-border-1"]) # TODO calculate position later and redo color config for well + self.stack = Stack(ConfigurationManager.configuration["color"]["stack-1"], ConfigurationManager.configuration["color"]["stack-border-1"]) loaded_icon = pygame.image.load(win_icon) pygame.display.set_caption(win_title) pygame.display.set_icon(loaded_icon) main_music_file = ConfigurationManager.configuration["sound"]["main-music"] - self.main_music.set_volume(0.7) + self.main_music.set_volume(0.7) # TODO add volume to the config self.main_music.play(mixer.Sound(main_music_file), -1) # gets called from the games main loop @@ -81,12 +81,6 @@ class Tetris: self.current_piece.revert() if self.stack and self.current_piece.collide(self.stack): self.current_piece.revert() - if event.key == pygame.K_UP: - self.current_piece.move((0, -self.tile_size)) - if self.well and self.current_piece.collide(self.well): - self.current_piece.revert() - if self.stack and self.current_piece.collide(self.stack): - self.current_piece.revert() if event.key == pygame.K_DOWN: self.current_piece.move((0, self.tile_size)) if self.well and self.current_piece.collide(self.well): @@ -104,7 +98,7 @@ class Tetris: # TODO write not initialized exception # draw window bg - bg_color = pygame.Color(ConfigurationManager.configuration["window"]["bg-color"]) + bg_color = pygame.Color(ConfigurationManager.configuration["color"]["window-bg"]) self.screen.fill(bg_color) # draw all game objects diff --git a/config.yaml b/config.yaml index 7c81e27..d416c8d 100644 --- a/config.yaml +++ b/config.yaml @@ -3,7 +3,6 @@ window: height: 600 icon: "tetris_icon.png" title: "Tetris" - bg-color: "#6495ED" # cornflower-blue sound: main-music: "main_music.ogg" @@ -14,10 +13,14 @@ engine: fps: 60 tile-size: 20 -# https://coolors.co/6495ed-ee6352-59cd90-fac05e-dfd9e2 color: - base-1: "#EE6352" - base-2: "#59CD90" - base-3: "#FAC05E" - base-4: "#4C5B5C" - border: "#DFD9E2" + window-bg: "#000000" + piece-1: "#1F37EC" + piece-2: "#3DBBFC" + piece-3: "#FFFFFF" + piece-inner-border-1: "#1F37EC" + piece-border-1: "#000000" + well-1: "#9BFCF0" + well-border-1: "#000000" + stack-1: "#747474" + stack-border-1: "#000000" diff --git a/entity/Piece.py b/entity/Piece.py index 0925fde..c964460 100644 --- a/entity/Piece.py +++ b/entity/Piece.py @@ -1,5 +1,6 @@ from typing import List, Tuple from pygame import mixer +import pygame import copy from entity.Entity import Entity @@ -11,10 +12,11 @@ from util.ConfigurationManager import ConfigurationManager ''' class Piece(Entity): - GRAVITY = 300 # A move down every 600 ms + GRAVITY = 300 # A move down every 300 ms - def __init__(self, shape: Tuple, position: Tuple, color: str, border_color: str): + def __init__(self, shape: Tuple, position: Tuple, color: str, inner_border_color: str, border_color: str): super().__init__(self.__get_points(shape, position), color, border_color) + self.inner_border_color = inner_border_color self.center = self.__get_center(shape, position) self.piece_set_sound = mixer.Channel(2) @@ -34,6 +36,19 @@ class Piece(Entity): if stack and self.collide(stack): self.revert() + def draw(self, surface: pygame.Surface) -> None: + super().draw(surface) + + tile_size = ConfigurationManager.configuration["engine"]["tile-size"] + for square in self.points: + if self.inner_border_color: + vertex_one = (square[0][0] + (tile_size // 10), square[0][1] + (tile_size // 10)) + vertex_two = (square[1][0] - (tile_size // 10), square[1][1] + (tile_size // 10)) + vertex_three = (square[2][0] - (tile_size // 10), square[2][1] - (tile_size // 10)) + vertex_four = (square[3][0] + (tile_size // 10), square[3][1] - (tile_size // 10)) + new_square = (vertex_one, vertex_two, vertex_three, vertex_four) + 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: diff --git a/entity/Well.py b/entity/Well.py index 129488e..570a8b0 100644 --- a/entity/Well.py +++ b/entity/Well.py @@ -8,8 +8,8 @@ class Well(Entity): WIDTH = 10 # standard tetris well width, should not be changed HEIGHT = 20 # standard tetris well height, should not be changed - def __init__(self, position: Tuple, color): - super().__init__(self.__get_points(position), color) + def __init__(self, position: Tuple, color: str, border_color: str): + super().__init__(self.__get_points(position), color, border_color) def __get_points(self, position: Tuple) -> List: tile_size = ConfigurationManager.configuration["engine"]["tile-size"] diff --git a/util/PieceGenerator.py b/util/PieceGenerator.py index 655b6ba..062c10e 100644 --- a/util/PieceGenerator.py +++ b/util/PieceGenerator.py @@ -16,9 +16,8 @@ class PieceGenerator: if len(cls.__bucket) == 0: cls.__generate_bucket() - border_color = ConfigurationManager.configuration["color"]["border"] # TODO abstract color call to config out? - - return Piece(cls.__get_piece_shape(cls.__bucket.pop()), position, cls.__get_piece_color(), border_color) + base_color, inner_border_color, border_color = cls.__get_piece_color() + return Piece(cls.__get_piece_shape(cls.__bucket.pop()), position, base_color, inner_border_color, border_color) @classmethod def __generate_bucket(cls) -> None: @@ -45,6 +44,11 @@ class PieceGenerator: return None - def __get_piece_color() -> str: + def __get_piece_color() -> Tuple: random_number = random.randint(1, 3) - return ConfigurationManager.configuration["color"]["base-" + str(random_number)] + + base_color = ConfigurationManager.configuration["color"]["piece-" + str(random_number)] + inner_border_color = None if random_number != 3 else ConfigurationManager.configuration["color"]["piece-inner-border-1"] + border_color = ConfigurationManager.configuration["color"]["piece-border-1"] + + return (base_color, inner_border_color, border_color)