Here is my updated game. For some reason, the board now completely fills when a shape hits the bottom of the screen. Why is this occurring?
import sys
import pygame
import random
Initialize Pygame
pygame.init()
Define constants
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
BACKGROUND_COLOR = (0, 0, 0)
GRID_SIZE = 30
GRID_WIDTH, GRID_HEIGHT = SCREEN_WIDTH // GRID_SIZE, SCREEN_HEIGHT // GRID_SIZE
Define shapes
SHAPES = [
T shape
[
[1, 1, 1],
[0, 1, 0],
],
I shape
[
[1, 1, 1, 1],
],
O shape
[
[1, 1],
[1, 1],
],
L shape
[
[1, 1, 1],
[1, 0, 0],
],
J shape
[
[1, 1, 1],
[0, 0, 1],
],
S shape
[
[0, 1, 1],
[1, 1, 0],
],
Z shape
[
[1, 1, 0],
[0, 1, 1],
],
]
Define game functions
def draw_shape(screen, shape, x, y):
shape_color = (255, 255, 255) # White color for the shape
for i, row in enumerate(shape):
for j, cell in enumerate(row):
if cell: # If the cell is part of the shape (has a value of 1)
Calculate the screen position of the cell
screen_x = (x + j) * GRID_SIZE
screen_y = (y + i) * GRID_SIZE
Draw a rectangle on the screen for the cell
pygame.draw.rect(screen, shape_color, (screen_x, screen_y, GRID_SIZE, GRID_SIZE))
Optionally, draw a border around the cell
border_color = (0, 0, 0) # Black color for the border
border_thickness = 2
pygame.draw.rect(screen, border_color, (screen_x, screen_y, GRID_SIZE, GRID_SIZE), border_thickness)
def check_collision(board, shape, x, y):
for i, row in enumerate(shape):
for j, cell in enumerate(row):
if cell: # If the cell is part of the shape (has a value of 1)
Check if the cell is out of the grid boundaries
if x + j < 0 or x + j >= GRID_WIDTH or y + i < 0 or y + i >= GRID_HEIGHT:
return True
Check if the cell is overlapping an occupied cell on the board
if board[y + i][x + j] != -1:
return True
return False
def clear_lines(board):
new_board = []
lines_cleared = 0
for row in board:
if not all(cell for cell in row): # If the row is not full
new_board.append(row) # Keep the row in the new board
else:
lines_cleared += 1 # Increment the lines_cleared counter
Add empty rows to the top of the new board to maintain the same size
empty_row = [0 for _ in range(GRID_WIDTH)]
for _ in range(lines_cleared):
new_board.insert(0, empty_row.copy())
return new_board, lines_cleared
def rotate(shape):
Get the dimensions of the shape