import sys import pygame from pygame import mixer from util.ConfigurationManager import ConfigurationManager from util.PieceGenerator import PieceGenerator from entity.Well import Well from entity.Stack import Stack # TODO should be a singleton? class Tetris: def __init__(self): self.fps = -1 self.tile_size = -1 self.screen = None self.clock = None self.current_piece = None self.well = None self.stack = None def initialize(self) -> None: pygame.init() mixer.init() win_width = ConfigurationManager.configuration["window"]["width"] win_height = ConfigurationManager.configuration["window"]["height"] win_icon = ConfigurationManager.configuration["window"]["icon"] win_title = ConfigurationManager.configuration["window"]["title"] self.fps = ConfigurationManager.configuration["engine"]["fps"] self.tile_size = ConfigurationManager.configuration["engine"]["tile-size"] self.screen = pygame.display.set_mode((win_width, win_height)) self.clock = pygame.time.Clock() mixer.music.load("main_music.mp3") mixer.music.set_volume(0.7) 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"]) loaded_icon = pygame.image.load(win_icon) pygame.display.set_caption(win_title) pygame.display.set_icon(loaded_icon) mixer.music.play(-1) # 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) if self.stack: self.stack.update(elapsed_time) # TODO create control utility class for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if self.current_piece: if event.key == pygame.K_SPACE: self.current_piece.rotate() 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_LEFT: self.current_piece.move((-self.tile_size, 0)) 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_RIGHT: self.current_piece.move((self.tile_size, 0)) 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_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): self.current_piece.revert() if self.stack and self.current_piece.collide(self.stack): self.current_piece.revert() if event.key == pygame.K_z: if self.current_piece: self.stack.add_piece(self.current_piece) self.__generate_piece((300, 100)) def draw(self) -> None: # TODO write not initialized exception # draw window bg bg_color = pygame.Color(ConfigurationManager.configuration["window"]["bg-color"]) self.screen.fill(bg_color) # draw all game objects if self.current_piece: self.current_piece.draw(self.screen) if self.well: self.well.draw(self.screen) if self.stack: self.stack.draw(self.screen) # update display pygame.display.update() # TODO one line method is questionable def __generate_piece(self, position): self.current_piece = PieceGenerator.get_piece(position)