import pygame import globals import enums from random import choice class Brick: def __init__(self, brick, state = enums.BrickState.Next): self.shape = [] self.color = choice(list(enums.BrickColor)) self.set_state(state) self.angle = 0 self.direction = None self.load_brick(brick) self.get_image() self.block_image = pygame.transform.scale(self.img, (48, 48)) if self.img else None self.draw_brick() self.x, self.y = (3, 0) self.location = list((self.x, self.y)) def get_image(self): match self.color: case enums.BrickColor.Magenta: self.img = pygame.image.load("assets/magenta_block.png").convert_alpha() case enums.BrickColor.Cyan: self.img = pygame.image.load("assets/cyan_block.png").convert_alpha() case enums.BrickColor.Yellow: self.img = pygame.image.load("assets/yellow_block.png").convert_alpha() case _: self.img = pygame.image.load("assets/magenta_block.png").convert_alpha() def load_brick(self, brick): self.shape = [l for l in globals.BRICKS[brick]] 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.shape): for x, char in enumerate(row): 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 def update(self): if self.y + self.rows == globals.GRID_HEIGHT: return False self.y += 1 self.location = (self.x, self.y) return True def rotate_clockwise(self, shape): return [list(row)[::-1] for row in zip(*shape)] def rotate(self): new_shape = self.rotate_clockwise(self.shape) if not self.collision(new_shape, self.x, self.y): 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() def collision(self, shape, x, y): for row_idx, row in enumerate(shape): for col_idx, cell in enumerate(row): if cell: new_x = x + col_idx new_y = y + row_idx if new_x <= 0 or new_x >= globals.GRID_WIDTH or new_y >= globals.GRID_HEIGHT: print(f"Collision X:{new_x}, Y:{new_y}") return True if new_y >= 0 and globals.grid[new_y][new_x]: return True return False def set_state(self, state): self.state = state print(f"State set to {self.state}") def move_right(self): print(f"X: {self.x}") if self.x != 0: if self.collision(self.shape, self.x, self.y): return self.x += 1 self.location = (self.x, self.y) def move_left(self): print(f"X: {self.x}") if self.x != globals.GRID_WIDTH: if self.collision(self.shape, self.x, self.y): return self.x -= 1 self.location = (self.x, self.y) def drop(self): print("Dropping")