Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.7 kB
3
Indexable
Never
import pygame
import random

# Initialize Pygame
pygame.init()

# Constants
WIDTH, HEIGHT = 300, 600
GRID_SIZE = 30
GRID_WIDTH, GRID_HEIGHT = WIDTH // GRID_SIZE, HEIGHT // GRID_SIZE
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)

# Tetris shapes (represented as lists of (x, y) offsets)
SHAPES = [
    [(0, 0), (-1, 0), (1, 0), (2, 0)],  # I-shape
    [(0, 0), (0, -1), (1, 0), (1, -1)],  # O-shape
    [(0, 0), (-1, 0), (1, 0), (1, -1)],  # L-shape
    [(0, 0), (-1, 0), (1, 0), (-1, -1)],  # J-shape
    [(0, 0), (-1, 0), (0, -1), (1, -1)],  # S-shape
    [(0, 0), (-1, 0), (0, -1), (1, 0)],  # T-shape
    [(0, 0), (-1, 0), (0, -1), (1, -1)]   # Z-shape
]

# Colors for the shapes
SHAPE_COLORS = [
    (0, 255, 255),  # Cyan
    (255, 255, 0),  # Yellow
    (255, 165, 0),  # Orange
    (0, 0, 255),    # Blue
    (0, 255, 0),    # Green
    (128, 0, 128),  # Purple
    (255, 0, 0)     # Red
]

# Initialize the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Eyebrow Tetris")

# Initialize game variables
grid = [[0 for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]
current_shape = None
current_color = None
shape_x, shape_y = 0, 0

# Function to create a new random shape
def new_shape():
    global current_shape, current_color, shape_x, shape_y
    shape_idx = random.randint(0, len(SHAPES) - 1)
    current_shape = SHAPES[shape_idx]
    current_color = SHAPE_COLORS[shape_idx]
    shape_x = GRID_WIDTH // 2
    shape_y = 0

# Function to check if a position is valid for the current shape
def is_valid_position():
    for x, y in current_shape:
        if shape_x + x < 0 or shape_x + x >= GRID_WIDTH or shape_y + y >= GRID_HEIGHT:
            return False
        if grid[shape_y + y][shape_x + x] != 0:
            return False
    return True

# Function to place the current shape in the grid
def place_shape():
    for x, y in current_shape:
        grid[shape_y + y][shape_x + x] = current_color

# Function to check and clear completed rows
def clear_rows():
    global grid
    full_rows = []
    for y in range(GRID_HEIGHT):
        if all(grid[y]):
            full_rows.append(y)
    for y in full_rows:
        del grid[y]
        grid.insert(0, [0] * GRID_WIDTH)

# Main game loop
clock = pygame.time.Clock()
game_over = False

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                shape_x -= 1
                if not is_valid_position():
                    shape_x += 1
            if event.key == pygame.K_RIGHT:
                shape_x += 1
                if not is_valid_position():
                    shape_x -= 1
            if event.key == pygame.K_DOWN:
                shape_y += 1
                if not is_valid_position():
                    shape_y -= 1

    # Move the shape down
    shape_y += 1
    if not is_valid_position():
        shape_y -= 1
        place_shape()
        clear_rows()
        new_shape()

    # Clear the screen
    screen.fill(WHITE)

    # Draw the grid
    for y in range(GRID_HEIGHT):
        for x in range(GRID_WIDTH):
            if grid[y][x] != 0:
                pygame.draw.rect(screen, grid[y][x], (x * GRID_SIZE, y * GRID_SIZE, GRID_SIZE, GRID_SIZE))

    # Draw the current shape
    for x, y in current_shape:
        pygame.draw.rect(screen, current_color, ((shape_x + x) * GRID_SIZE, (shape_y + y) * GRID_SIZE, GRID_SIZE, GRID_SIZE))

    # Update the display
    pygame.display.flip()

    # Set the game's frame rate
    clock.tick(5)

# Quit Pygame
pygame.quit()