编辑代码

import pygame
import random
# 初始化pygame
pygame.init()
# 游戏窗口尺寸
WINDOW_WIDTH = 600
WINDOW_HEIGHT = 800
# 定义方块尺寸
BLOCK_SIZE = 30
# 定义方块颜色
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
CYAN = (0, 255, 255)
MAGENTA = (255, 0, 255)
# 定义方块形状
SHAPES = [
    [[1, 1, 1],
     [0, 1, 0]],
    [[2, 2, 0],
     [0, 2, 2]],
    [[0, 3, 3],
     [3, 3, 0]],
    [[4, 4],
     [4, 4]],
    [[5, 0, 0],
     [5, 5, 5]],
    [[0, 0, 6],
     [6, 6, 6]],
    [[7, 7, 7, 7]]
]
# 随机生成下一个方块
def new_block():
    shape = random.choice(SHAPES)
    color = random.choice([RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA])
    return (shape, color)
# 画方块
def draw_block(screen, color, row, col):
    pygame.draw.rect(screen, color, (col * BLOCK_SIZE + 1, row * BLOCK_SIZE + 1, BLOCK_SIZE - 2, BLOCK_SIZE - 2))
# 画方块矩阵
def draw_matrix(screen, matrix, offset):
    rows, cols = matrix.shape
    for row in range(rows):
        for col in range(cols):
            if matrix[row][col] != 0:
                draw_block(screen, offset[matrix[row][col]], row, col)
# 检查方块是否与边界或其他方块重叠
def check_collision(board, shape, offset):
    rows, cols = shape.shape
    for row in range(rows):
        for col in range(cols):
            if shape[row][col] != 0:
                if row + offset[0] < 0 or row + offset[0] >= len(board) or col + offset[1] < 0 or col + offset[1] >= len(board[0]):
                    return True
                if board[row + offset[0]][col + offset[1]] != 0:
                    return True
    return False
# 将方块加入游戏板中
def add_to_board(board, shape, offset):
    rows, cols = shape.shape
    for row in range(rows):
        for col in range(cols):
            if shape[row][col] != 0:
                board[row + offset[0]][col + offset[1]] = shape[row][col]
# 消除整行
def remove_complete_lines(board):
    rows, cols = board.shape
    num_complete_lines = 0
    for row in range(rows):
        if all(board[row, col] != 0 for col in range(cols)):
            num_complete_lines += 1
            for r in range(row, 0, -1):
                for col in range(cols):
                    board[r][col] = board[r-1][col]
            for col in range(cols):
                board[0][col] = 0
    return num_complete_lines
# 游戏循环
def game_loop(screen):
    board = [[0] * (WINDOW_WIDTH // BLOCK_SIZE) for _ in range(WINDOW_HEIGHT // BLOCK_SIZE)]
    current_block = new_block()
    next_block = new_block()
    score = 0
    game_over = False
    clock = pygame.time.Clock()
    while not game_over:
        # 处理游戏事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    new_offset = (current_block[1], current_block[2] - 1)
                    if not check_collision(board, current_block[0], new_offset):
                        current_block = (current_block[0], current_block[1], current_block[2] - 1)
                elif event.key == pygame.K_RIGHT:
                    new_offset = (current_block[1], current_block[2] + 1)
                    if not check_collision(board, current_block[0], new_offset):
                        current_block = (current_block[0], current_block[1], current_block[2] + 1)
                elif event.key == pygame.K_DOWN:
                    new_offset = (current_block[1] + 1, current_block[2])
                    if not check_collision(board, current_block[0], new_offset):
                        current_block = (current_block[0], current_block[1] + 1, current_block[2])
                elif event.key == pygame.K_SPACE:
                    while not check_collision(board, current_block[0], (current_block[1] + 1, current_block[2])):
                        current_block = (current_block[0], current_block[1] + 1, current_block[2])
                    add_to_board(board, current_block[0], (current_block[1], current_block[2]))
                    num_complete_lines = remove_complete_lines(board)
                    score += num_complete_lines
                    current_block = next_block
                    next_block = new_block()
                    if check_collision(board, current_block[0], (current_block[1], current_block[2])):
                        game_over = True
        # 绘制游戏画面
        screen.fill(BLACK)
        draw_matrix(screen, board, [BLACK] + SHAPES)
        draw_matrix(screen, current_block[0], [current_block[1]])
        pygame.display.set_caption("Tetris - Score: " + str(score))
        pygame.display.flip()
        # 控制游戏帧率
        clock.tick(10)
    # 退出pygame
    pygame.quit()
# 启动游戏
if __name__ == '__main__':
    screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    game_loop(screen)