Untitled

 avatar
unknown
plain_text
a year ago
2.3 kB
4
Indexable
import sys
import random
import pygame

# Initialize Pygame
pygame.init()

# Define constants
WIDTH, HEIGHT = 600, 600
GRID_SIZE = 9
GRID_WIDTH, GRID_HEIGHT = GRID_SIZE * WIDTH // GRID_SIZE, GRID_SIZE * HEIGHT // GRID_SIZE
BLOCK_SIZE = GRID_WIDTH // GRID_SIZE
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BACKGROUND_COLOR = (200, 200, 200)

screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Block Puzzle")
clock = pygame.time.Clock()

grid = []
for i in range(GRID_SIZE):
    row = []
    for j in range(GRID_SIZE):
        row.append(0)
    grid.append(row)

block_shapes = [
    [
        [1, 1, 1],
        [0, 0, 0],
    ],
    [
        [1, 1],
        [1, 0],
    ],
    [
        [1, 0],
        [1, 1],
    ],
    [
        [1, 0, 0],
        [1, 1, 0],
    ],
    [
        [0, 0, 1],
        [1, 1, 1],
    ],
    [
        [1, 1, 0],
        [0, 1, 0],
    ],
    [
        [0, 1, 0],
        [1, 1, 0],
    ],
]


def draw_grid():
    for i in range(GRID_SIZE):
        for j in range(GRID_SIZE):
            pygame.draw.rect(screen, WHITE, (GRID_WIDTH + j * BLOCK_SIZE, GRID_HEIGHT + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
            if grid[i][j] != 0:
                pygame.draw.rect(screen, BLACK, (GRID_WIDTH + j * BLOCK_SIZE, GRID_HEIGHT + i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))


def draw_block(shape, x, y):
    for i, row in enumerate(shape):
        for j, cell in enumerate(row):
            if cell:
                pygame.draw.rect(screen, (0, 255, 0), (GRID_WIDTH + (x + j) * BLOCK_SIZE, GRID_HEIGHT + (y + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))


def check_collision(shape, x, y):
    for i, row in enumerate(shape):
        for j, cell in enumerate(row):
            if cell:
                if x + j < 0 or x + j >= GRID_SIZE or y + i < 0 or y + i >= GRID_SIZE or grid[y + i][x + j] != 0:
                    return True
    return False


def merge_blocks(shape, x, y):
    for i, row in enumerate(shape):
        for j, cell in enumerate(row):
            if cell:
                grid[y + i][x + j] = 1


def remove_line(y):
    for i in range(y, 0, -1):
        for j in range(GRID_SIZE):
            grid[i][j] = grid[i
Editor is loading...
Leave a Comment