Rotation is now working. /JL
This commit is contained in:
44
bricks.py
44
bricks.py
@@ -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")
|
||||
Reference in New Issue
Block a user