import pygame
import random
# Initialize pygame
pygame.init()
# Set up the game window
width, height = 800, 600
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Tetris")
# Define colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
YELLOW = (255, 255, 0)
ORANGE = (255, 165, 0)
# Define the block shapes
SHAPES = [
[[1, 1, 1, 1]],
[[1, 1], [1, 1]],
[[1, 1, 0], [0, 1, 1]],
[[0, 1, 1], [1, 1, 0]],
[[1, 1, 1], [0, 1, 0]],
[[1, 1, 1], [1, 0, 0]],
[[1, 1, 1], [0, 0, 1]]
]
# Define the block colors
COLORS = [CYAN, YELLOW, ORANGE, BLUE, RED, GREEN, MAGENTA]
# Define the size of each block
BLOCK_SIZE = 30
# Calculate the number of rows and columns in the grid
rows = height // BLOCK_SIZE
cols = width // BLOCK_SIZE
# Initialize the grid with empty cells
grid = [[BLACK] * cols for _ in range(rows)]
# Define the current shape and its position
current_shape = random.choice(SHAPES)
shape_color = random.choice(COLORS)
current_row = 0
current_col = cols // 2 - len(current_shape[0]) // 2
# Function to draw the grid
def draw_grid():
for row in range(rows):
for col in range(cols):
pygame.draw.rect(screen, grid[row][col], (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
# Function to draw the current shape
def draw_shape():
for