Python——俄罗斯方块

发布于:2024-09-17 ⋅ 阅读:(36) ⋅ 点赞:(0)

俄罗斯方块游戏是一款经典的益智游戏,通常使用编程语言Python来实现。下面是一个简单的俄罗斯方块游戏的示例代码:

import pygame
import random

# 定义颜色
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)

# 定义方块的形状
SHAPES = [
    [[1, 1, 1, 1]],
    [[1, 1], [1, 1]],
    [[1, 1, 0], [0, 1, 1]],
    [[0, 1, 1], [1, 1, 0]],
    [[1, 0, 0], [1, 1, 1]],
    [[0, 0, 1], [1, 1, 1]],
    [[1, 1, 1], [0, 1, 0]]
]

# 初始化游戏
pygame.init()

# 设置屏幕宽高
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption("俄罗斯方块")

# 定义方块的大小和间隙
block_size = 30
block_gap = 2

# 初始化方块的位置
x_pos = 3
y_pos = 0

# 初始化方块的形状
shape = random.choice(SHAPES)

# 初始化方块的颜色
color = random.choice([RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, ORANGE])

# 设置游戏时钟
clock = pygame.time.Clock()

# 判断方块是否可以移动
def can_move(x_offset, y_offset):
    for i in range(len(shape)):
        for j in range(len(shape[i])):
            if shape[i][j]:
                x = x_pos + j + x_offset
                y = y_pos + i + y_offset
                if (x < 0 or x >= SCREEN_WIDTH // (block_size + block_gap) or
                    y >= SCREEN_HEIGHT // (block_size + block_gap) or
                    grid[y][x]):
                    return False
    return True

# 绘制方块
def draw_block(x, y, color):
    pygame.draw.rect(screen, color, [(block_size + block_gap) * x,
                                     (block_size + block_gap) * y,
                                     block_size,
                                     block_size])

# 绘制网格
def draw_grid(grid):
    for i in range(len(grid)):
        for j in range(len(grid[i])):
            if grid[i][j]:
                draw_block(j, i, grid[i][j])

# 更新网格
def update_grid(x_offset, y_offset):
    global x_pos, y_pos
    x_pos += x_offset
    y_pos += y_offset
    for i in range(len(shape)):
        for j in range(len(shape[i])):
            if shape[i][j]:
                x = x_pos + j
                y = y_pos + i
                if y >= 0:
                    grid[y][x] = color

# 消除行
def clear_rows():
    full_rows = []
    for i in range(len(grid)):
        if all(grid[i]):
            full_rows.append(i)
    for row in full_rows:
        del grid[row]
        grid.insert(0, [BLACK] * (SCREEN_WIDTH // (block_size + block_gap)))

# 初始化网格
grid = [[BLACK] * (SCREEN_WIDTH // (block_size + block_gap))
        for _ in range(SCREEN_HEIGHT // (block_size + block_gap))]

# 游戏主循环
running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT and can_move(-1, 0):
                update_grid(-1, 0)
            elif event.key == pygame.K_RIGHT and can_move(1, 0):
                update_grid(1, 0)
            elif event.key == pygame.K_DOWN and can_move(0, 1):
                update_grid(0, 1)
            elif event.key == pygame.K_SPACE:
                rot_shape = list(zip(*reversed(shape)))
                if can_move(0, 0):
                    shape = rot_shape
                elif can_move(-1, 0):
                    update_grid(-1, 0)
                    shape = rot_shape
                elif can_move(1, 0):
                    update_grid(1, 0)
                    shape = rot_shape

    if can_move(0, 1):
        update_grid(0, 1)
    else:
        clear_rows()
        x_pos = 3
        y_pos = 0
        shape = random.choice(SHAPES)
        color = random.choice([RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, ORANGE])
        if not can_move(0, 1):
            running = False

    # 绘制背景
    screen.fill(BLACK)

    # 绘制方块
    for i in range(len(shape)):
        for j in range(len(shape[i])):
            if shape[i][j]:
                draw_block(x_pos + j, y_pos + i, color)

    # 绘制网格
    draw_grid(grid)

    # 设置帧率
    clock.tick(5)

    # 更新屏幕
    pygame.display.flip()

# 退出游戏
pygame.quit()

这段代码使用了Pygame库来实现游戏的图形界面,通过键盘控制方块的移动和旋转。游戏循环不断更新方块的位置和网格状态,并绘制在屏幕上。在方块达到底部或无法继续移动时,判断是否有满行,并清除满行的方块。游戏会根据方块的状态和移动情况不断更新,直到无法继续下落为止,游戏结束。

请注意,这只是一个简单的示例,可能还有一些功能和优化方面的改进。您可以根据自己的需求进行修改和扩展。