gamegrid, falling, Tetrominos, collision and deleting rows

This commit is contained in:
IM23a-bachmannj2
2024-12-08 11:04:04 +01:00
parent 0995c6d8f2
commit 3eb8af9991
3 changed files with 80 additions and 10 deletions
+33 -1
View File
@@ -18,4 +18,36 @@ class Grid():
self.cell_size - 2,
self.cell_size - 2
)
pygame.draw.rect(surface, color, cell_rect)
pygame.draw.rect(surface, color, cell_rect)
def add_tetromino(self, tetromino):
"""Lock the tetromino into the grid."""
for row_idx, row in enumerate(tetromino.shape):
for col_idx, cell in enumerate(row):
if cell: # Only lock non-empty cells
grid_x = tetromino.x + col_idx
grid_y = tetromino.y + row_idx
if 0 <= grid_y < self.rows and 0 <= grid_x < self.cols:
self.grid[grid_y][grid_x] = 1
def check_collision(self, shape, x, y):
"""Check if the tetromino collides with the grid or boundaries."""
for row_idx, row in enumerate(shape):
for col_idx, cell in enumerate(row):
if cell: # Only check cells that are part of the tetromino
grid_x = x + col_idx
grid_y = y + row_idx
# Check bounds
if grid_x < 0 or grid_x >= self.cols or grid_y >= self.rows:
return True
# Check if cell is already occupied
if grid_y >= 0 and self.grid[grid_y][grid_x] != 0:
return True
def check_full_row(self):
"""Check for and clear any full rows."""
new_grid = [row for row in self.grid if not all(cell == 1 for cell in row)]
cleared_rows = len(self.grid) - len(new_grid)
self.grid = [[0] * self.cols for _ in range(cleared_rows)] + new_grid