refactor: improve keyboard input logic

This commit is contained in:
2021-06-14 13:17:18 -04:00
parent 7d54c0eb91
commit 6e954c0204
3 changed files with 49 additions and 40 deletions

View File

@@ -4,6 +4,7 @@ import pygame
from pygame import mixer
from tetris.util import ConfigurationManager
from tetris.util import TextGenerator
from tetris.util import Controller
from tetris.entity import PieceGenerator
from tetris.entity import Well
from tetris.entity import Stack
@@ -57,54 +58,17 @@ class Game:
self.current_piece.update(elapsed_time, self)
else:
self.current_piece = PieceGenerator.get_piece((360, 100)) # TODO calculate spawn position
if self.stack and self.current_piece.collide(self.stack): # game over
if self.stack and self.current_piece.collide(self.stack): # TODO game over redo
pygame.quit()
sys.exit()
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_DOWN:
self.is_pressing_down = True
if self.current_piece:
self.current_piece.gravity_time = ConfigurationManager.get("engine", "piece-gravity-time") / 8
self.current_piece.set_time = ConfigurationManager.get("engine", "piece-gravity-time") / 8
if event.type == pygame.KEYUP:
if event.key == pygame.K_DOWN:
self.is_pressing_down = False
if self.current_piece:
self.current_piece.gravity_time = ConfigurationManager.get("engine", "piece-gravity-time")
self.current_piece.set_time = ConfigurationManager.get("engine", "piece-set-time")
if self.is_pressing_down:
if self.current_piece:
self.current_piece.gravity_time = ConfigurationManager.get("engine", "piece-gravity-time") / 8
self.current_piece.set_time = ConfigurationManager.get("engine", "piece-set-time") / 8
def draw(self) -> None: