Rotation is now working. /JL

This commit is contained in:
2025-04-25 17:12:10 +02:00
parent 5cbbf9bdcb
commit 3b76f02f92
5 changed files with 57 additions and 44 deletions

View File

@@ -1,5 +1,5 @@
import pygame
from globals import BRICKS, TILE_SIZE, GRID_WIDTH, GRID_HEIGHT, grid
import globals
import enums
from random import choice
@@ -7,7 +7,7 @@ from random import choice
class Brick:
def __init__(self, brick, state = enums.BrickState.Next):
self.layout = []
self.shape = []
self.color = choice(list(enums.BrickColor))
self.set_state(state)
self.angle = 0
@@ -31,19 +31,19 @@ class Brick:
self.img = pygame.image.load("assets/magenta_block.png").convert_alpha()
def load_brick(self, brick):
self.layout = [l for l in BRICKS[brick].splitlines()]
self.shape = [l for l in globals.BRICKS[brick]]
self.rows = len(self.layout)
self.cols = len(self.layout[0])
self.width = self.cols * TILE_SIZE
self.height = self.rows * TILE_SIZE
self.rows = len(self.shape)
self.cols = len(self.shape[0])
self.width = self.cols * globals.TILE_SIZE
self.height = self.rows * globals.TILE_SIZE
def draw_brick(self):
self.brick = pygame.Surface((self.width, self.height))
for y, row in enumerate(self.layout):
for y, row in enumerate(self.shape):
for x, char in enumerate(row):
if char == "X":
self.brick.blit(self.block_image, (1 + x * TILE_SIZE, 1 + y * TILE_SIZE))
if char:
self.brick.blit(self.block_image, (1 + x * globals.TILE_SIZE, 1 + y * globals.TILE_SIZE))
def is_current(self):
return True if self.state == enums.BrickState.Current else False
@@ -56,9 +56,16 @@ class Brick:
return [list(row)[::-1] for row in zip(*shape)]
def rotate(self):
new_shape = self.rotate_clockwise(self.layout)
new_shape = self.rotate_clockwise(self.shape)
print(new_shape)
if not self.collision(new_shape, self.x, self.y):
self.layout = new_shape
self.shape = new_shape
self.rows = len(self.shape)
self.cols = len(self.shape[0])
self.width = self.cols * globals.TILE_SIZE
self.height = self.rows * globals.TILE_SIZE
self.draw_brick()
print(self.shape)
def collision(self, shape, x, y):
for row_idx, row in enumerate(shape):
@@ -66,9 +73,9 @@ class Brick:
if cell:
new_x = x + col_idx
new_y = y + row_idx
if new_x <= 0 or new_x >= GRID_WIDTH or new_y >= GRID_HEIGHT:
if new_x <= 0 or new_x >= globals.GRID_WIDTH or new_y >= globals.GRID_HEIGHT:
return True
if new_y >= 0 and grid[new_y][new_x]:
if new_y >= 0 and globals.dropgrid[new_y][new_x]:
return True
return False
@@ -77,15 +84,18 @@ class Brick:
print(f"State set to {self.state}")
def move_right(self):
if self.collision(self.layout, self.x, self.y):
if self.collision(self.shape, self.x, self.y):
return
else:
self.x += 1
self.location = (self.x, self.y)
def move_left(self):
self.x -= 1
self.location = (self.x, self.y)
if self.collision(self.shape, self.x, self.y):
return
else:
self.x -= 1
self.location = (self.x, self.y)
def drop(self):
print("Dropping")

View File

@@ -1,5 +1,5 @@
import pygame
from globals import TILE_SIZE
import globals
from enums import BrickColor
class DropNext():
@@ -10,7 +10,7 @@ class DropNext():
self.dropnext.fill((0, 0, 0)) # Fill with black
def draw(self, screen):
screen.blit(self.dropnext, (TILE_SIZE * 15, TILE_SIZE * 2))
screen.blit(self.dropnext, (globals.TILE_SIZE * 15, globals.TILE_SIZE * 2))
def draw_block(self, brick):
self.dropnext.fill(BrickColor.Black.value)

View File

@@ -1,18 +1,17 @@
import pygame
from globals import TILE_SIZE, grid
import globals
from enums import BrickColor
class DropZone():
def __init__(self, width, height):
self.dropzone = pygame.Surface((width * TILE_SIZE, height * TILE_SIZE))
self.dropzone = pygame.Surface((width * globals.TILE_SIZE, height * globals.TILE_SIZE))
self.width = width
self.height = height
self.grid_row = [" "] * self.width
grid = [self.grid_row] * self.height
print(globals.dropgrid)
def draw(self, screen):
screen.blit(self.dropzone, (TILE_SIZE * 4, TILE_SIZE * 1))
screen.blit(self.dropzone, (globals.TILE_SIZE * 4, globals.TILE_SIZE * 1))
def draw_brick(self, brick, location):
self.dropzone.fill(BrickColor.Black.value)
self.dropzone.blit(brick, (location[0] * TILE_SIZE, location[1] * TILE_SIZE))
self.dropzone.blit(brick, (location[0] * globals.TILE_SIZE, location[1] * globals.TILE_SIZE))

View File

@@ -1,16 +1,16 @@
def init():
global BRICKS
BRICKS = [
" X " + "\n" + "XXX" + "\n" + " X ",
" X" + "\n" + "XXX",
"X " + "\n" + "XXX",
"X",
"XXXX",
"XX" + "\n" + "XX",
" XX" + "\n" + "XX ",
"XX " + "\n" + " XX",
"X X" + "\n" + "XXX",
"XXX" + "\n" + " X "
[[0,1,0], [1,1,1], [0,1,0]],
[[0, 0, 1], [1, 1, 1]],
[[1, 0, 0], [1, 1, 1]],
[[1]],
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[0, 1, 1], [1, 1, 0]],
[[1, 1, 0], [0, 1, 1]],
[[1, 0, 1], [1, 1, 1]],
[[1, 1, 1], [0, 1, 0]]
]
global TILE_SIZE
TILE_SIZE = 48
@@ -18,5 +18,5 @@ def init():
GRID_WIDTH = 10
global GRID_HEIGHT
GRID_HEIGHT = 18
global grid
grid = None
global dropgrid
dropgrid = [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]

View File

@@ -41,17 +41,17 @@ class Tetris:
self.screen = pygame.display.set_mode((self.screen_width, self.screen_height))
pygame.display.set_caption("Tetris " + __version__)
self.current = Brick(brick = randrange(0, len(BRICKS)), state = enums.BrickState.Current)
print(self.current.layout)
print(self.current.color)
self.next = Brick(brick = randrange(0, len(BRICKS)))
print(self.next.layout)
print(self.next.color)
self.hud = Hud(self.screen_width, self.screen_height)
self.dropzone = DropZone(GRID_WIDTH, GRID_HEIGHT)
self.dropnext = DropNext(width = TILE_SIZE * 4, height = TILE_SIZE * 4)
self.current = Brick(brick = randrange(0, len(BRICKS)), state = enums.BrickState.Current)
print(self.current.shape)
print(self.current.color)
self.next = Brick(brick = randrange(0, len(BRICKS)))
print(self.next.shape)
print(self.next.color)
self.clock = pygame.time.Clock()
self.rumble_timer = pygame.time.get_ticks()
self.fall_speed = 1000 # in milliseconds
@@ -121,6 +121,7 @@ class Tetris:
if self.current.direction == enums.BrickDirection.Dropped:
break
self.current.rotate()
self.dropzone.draw_brick(self.current.brick, self.current.location)
case pygame.K_RETURN:
if self.current.direction == enums.BrickDirection.Dropped:
break
@@ -137,17 +138,20 @@ class Tetris:
break
self.current.direction = enums.BrickDirection.Right
self.current.move_right()
self.dropzone.draw_brick(self.current.brick, self.current.location)
elif self.hat_x == -1:
if self.current.direction == enums.BrickDirection.Dropped:
break
self.current.direction = enums.BrickDirection.Left
self.current.move_left()
self.dropzone.draw_brick(self.current.brick, self.current.location)
case pygame.JOYBUTTONDOWN:
if event.button == 2:
if self.current.direction == enums.BrickDirection.Dropped:
break
self.current.rotate()
self.dropzone.draw_brick(self.current.brick, self.current.location)
elif event.button == 0:
if self.current.direction == enums.BrickDirection.Dropped:
break