这个代码实现了一个简单的俄罗斯方块游戏。你可以将其保存为 .py 文件并运行,需要确保你已经安装了 Pygame 库。运行代码后,会弹出一个窗口,你可以使用方向键来控制方块的移动和旋转。

import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH = 300
HEIGHT = 600
BLOCK_SIZE = 30
BOARD_WIDTH = WIDTH // BLOCK_SIZE
BOARD_HEIGHT = HEIGHT // BLOCK_SIZE

# 定义颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
CYAN = (0, 255, 255)
YELLOW = (255, 255, 0)
MAGENTA = (255, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
ORANGE = (255, 165, 0)

# 定义方块形状
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]]
]

# 定义方块颜色
SHAPE_COLORS = [CYAN, YELLOW, MAGENTA, GREEN, RED, BLUE, ORANGE]

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("俄罗斯方块")

# 初始化游戏板
board = [[0] * BOARD_WIDTH for _ in range(BOARD_HEIGHT)]


def draw_board():
    for i in range(BOARD_HEIGHT):
        for j in range(BOARD_WIDTH):
            if board[i][j] != 0:
                pygame.draw.rect(screen, SHAPE_COLORS[board[i][j] - 1],
                                 (j * BLOCK_SIZE, i * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))
    for i in range(BOARD_HEIGHT):
        pygame.draw.line(screen, WHITE, (0, i * BLOCK_SIZE), (WIDTH, i * BLOCK_SIZE))
    for j in range(BOARD_WIDTH):
        pygame.draw.line(screen, WHITE, (j * BLOCK_SIZE, 0), (j * BLOCK_SIZE, HEIGHT))


def draw_shape(shape, x, y, color_index):
    for i in range(len(shape)):
        for j in range(len(shape[0])):
            if shape[i][j] == 1:
                pygame.draw.rect(screen, SHAPE_COLORS[color_index],
                                 ((x + j) * BLOCK_SIZE, (y + i) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))


def is_collision(shape, x, y):
    for i in range(len(shape)):
        for j in range(len(shape[0])):
            if shape[i][j] == 1:
                if y + i >= BOARD_HEIGHT or x + j < 0 or x + j >= BOARD_WIDTH or board[y + i][x + j] != 0:
                    return True
    return False


def merge_shape(shape, x, y, color_index):
    for i in range(len(shape)):
        for j in range(len(shape[0])):
            if shape[i][j] == 1:
                board[y + i][x + j] = color_index + 1


def clear_lines():
    full_lines = []
    for i in range(BOARD_HEIGHT):
        if all(board[i]):
            full_lines.append(i)
    for line in full_lines:
        del board[line]
        board = [[0] * BOARD_WIDTH] + board


# 游戏主循环
clock = pygame.time.Clock()
current_shape = random.choice(SHAPES)
current_x = BOARD_WIDTH // 2 - len(current_shape[0]) // 2
current_y = 0
current_color_index = random.randint(0, len(SHAPE_COLORS) - 1)
fall_time = 0
fall_speed = 0.3

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                if not is_collision(current_shape, current_x - 1, current_y):
                    current_x -= 1
            if event.key == pygame.K_RIGHT:
                if not is_collision(current_shape, current_x + 1, current_y):
                    current_x += 1
            if event.key == pygame.K_DOWN:
                if not is_collision(current_shape, current_x, current_y + 1):
                    current_y += 1
            if event.key == pygame.K_UP:
                rotated_shape = list(map(list, zip(*current_shape[::-1])))
                if not is_collision(rotated_shape, current_x, current_y):
                    current_shape = rotated_shape

    fall_time += clock.get_rawtime()
    clock.tick()

    if fall_time / 1000 >= fall_speed:
        if not is_collision(current_shape, current_x, current_y + 1):
            current_y += 1
        else:
            merge_shape(current_shape, current_x, current_y, current_color_index)
            clear_lines()
            current_shape = random.choice(SHAPES)
            current_x = BOARD_WIDTH // 2 - len(current_shape[0]) // 2
            current_y = 0
            current_color_index = random.randint(0, len(SHAPE_COLORS) - 1)
            if is_collision(current_shape, current_x, current_y):
                running = False
        fall_time = 0

    screen.fill(BLACK)
    draw_board()
    draw_shape(current_shape, current_x, current_y, current_color_index)
    pygame.display.flip()

# 退出 Pygame
pygame.quit()
    

Python最棒
12 声望0 粉丝

码农一枚


引用和评论

0 条评论