Move block working. /JL

This commit is contained in:
2025-04-22 20:23:05 +02:00
parent d5f9188865
commit 2e3460dca5
4 changed files with 19 additions and 10 deletions

View File

@@ -1,4 +1,5 @@
import pygame import pygame
from enums import BrickColor
class DropNext(): class DropNext():
def __init__(self, width, height): def __init__(self, width, height):
@@ -11,5 +12,6 @@ class DropNext():
screen.blit(self.dropnext, (tile_size * 15, tile_size * 2)) screen.blit(self.dropnext, (tile_size * 15, tile_size * 2))
def draw_block(self, brick): def draw_block(self, brick):
self.dropnext.fill(BrickColor.Black.value)
self.dropnext.blit(brick, ((self.width - brick.get_width()) / 2, (self.height - brick.get_height()) / 2)) self.dropnext.blit(brick, ((self.width - brick.get_width()) / 2, (self.height - brick.get_height()) / 2))

View File

@@ -1,4 +1,5 @@
import pygame import pygame
from enums import BrickColor
class DropZone(): class DropZone():
def __init__(self, tile_size, width, height): def __init__(self, tile_size, width, height):
@@ -6,7 +7,6 @@ class DropZone():
self.width = width self.width = width
self.height = height self.height = height
self.tile_size = tile_size self.tile_size = tile_size
self.dropzone.fill((0, 0, 0)) # Fill with black
self.grid_row = [" "] * self.width self.grid_row = [" "] * self.width
self.grid = [self.grid_row] * self.height self.grid = [self.grid_row] * self.height
@@ -14,5 +14,5 @@ class DropZone():
screen.blit(self.dropzone, (self.tile_size * 4, self.tile_size * 1)) screen.blit(self.dropzone, (self.tile_size * 4, self.tile_size * 1))
def draw_brick(self, brick, location): def draw_brick(self, brick, location):
self.dropzone.fill(BrickColor.Black.value)
self.dropzone.blit(brick, (location[0] * self.tile_size, location[1] * self.tile_size)) self.dropzone.blit(brick, (location[0] * self.tile_size, location[1] * self.tile_size))

View File

@@ -8,6 +8,7 @@ class BrickColor(Enum):
Magenta = (255, 0, 255) Magenta = (255, 0, 255)
Yellow = (255, 255, 0) Yellow = (255, 255, 0)
Cyan = (0, 255, 255) Cyan = (0, 255, 255)
Black = (0, 0, 0)
class BrickState(Enum): class BrickState(Enum):
Current = 0 Current = 0

View File

@@ -7,7 +7,7 @@ from dropzone import DropZone
from dropnext import DropNext from dropnext import DropNext
from hud import Hud from hud import Hud
__version__ = "0.0.2" __version__ = "0.0.3"
# Constants # Constants
HAT_REPEAT_DELAY = 0 # milliseconds before first repeat HAT_REPEAT_DELAY = 0 # milliseconds before first repeat
@@ -21,6 +21,9 @@ class Tetris:
while self.running: while self.running:
self.main_loop() self.main_loop()
if self.joysticks:
self.joysticks[self.joy.get_instance_id()].controllers[0].close()
pygame.quit() pygame.quit()
@@ -105,14 +108,15 @@ class Tetris:
self.current.direction = enums.BrickDirection.Left self.current.direction = enums.BrickDirection.Left
self.current.move_left() self.current.move_left()
self.dropzone.draw_brick(self.current.brick, self.current.location) self.dropzone.draw_brick(self.current.brick, self.current.location)
case pygame.K_UP: case pygame.K_SPACE:
if self.current.direction == enums.BrickDirection.Dropped: if self.current.direction == enums.BrickDirection.Dropped:
break break
self.current.rotate() self.current.rotate()
case pygame.K_DOWN: case pygame.K_RETURN:
if self.current.direction == enums.BrickDirection.Dropped: if self.current.direction == enums.BrickDirection.Dropped:
break break
self.current.drop() self.current.drop()
case pygame.JOYHATMOTION: case pygame.JOYHATMOTION:
self.hat_direction = event.value self.hat_direction = event.value
self.hat_x, self.hat_y = self.hat_direction self.hat_x, self.hat_y = self.hat_direction
@@ -129,11 +133,13 @@ class Tetris:
break break
self.current.direction = enums.BrickDirection.Left self.current.direction = enums.BrickDirection.Left
self.current.move_left() self.current.move_left()
elif self.hat_y == 1:
if self.current.direction == enums.BrickDirection.Dropped: case pygame.JOYBUTTONDOWN:
break if event.button == 2:
self.current.rotate() if self.current.direction == enums.BrickDirection.Dropped:
elif self.hat_y == -1: break
self.current.rotate()
elif event.button == 0:
if self.current.direction == enums.BrickDirection.Dropped: if self.current.direction == enums.BrickDirection.Dropped:
break break
self.current.drop() self.current.drop()