Python小游戏之俄罗斯方块

以下是一个简单的俄罗斯方块游戏示例代码:
import pygame
import random

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

# 方块类型
SHAPES = [
    [[1, 1, 1, 1]],  # 长条型
    [[1, 1], [1, 1]],  # 正方形
    [[1, 1, 1], [0, 1, 0]],  # T 型
    [[1, 1, 1], [1, 0, 0]],  # L 型
    [[1, 1, 1], [0, 0, 1]],  # 反 L 型
    [[1, 1, 0], [0, 1, 1]],  # S 型
    [[0, 1, 1], [1, 1, 0]]  # Z 型
]

# 屏幕尺寸
SCREEN_WIDTH = 400
SCREEN_HEIGHT = 600

# 小方格大小
GRID_SIZE = 20

# 初始化 pygame
pygame.init()

# 创建屏幕
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("俄罗斯方块游戏")

# 游戏区域的行数和列数
GRID_ROWS = SCREEN_HEIGHT // GRID_SIZE
GRID_COLS = SCREEN_WIDTH // GRID_SIZE

# 当前方块
current_shape = None
current_shape_pos = [0, 0]

# 游戏区域的网格
grid = [[0 for _ in range(GRID_COLS)] for _ in range(GRID_ROWS)]

# 生成新的方块
def new_shape():
    global current_shape, current_shape_pos
    current_shape = random.choice(SHAPES)
    current_shape_pos = [GRID_COLS // 2 - len(current_shape[0]) // 2, 0]

# 检查是否可以移动方块
def can_move(x_offset, y_offset):
    for row in range(len(current_shape)):
        for col in range(len(current_shape[row])):
            if current_shape[row][col] and (
                current_shape_pos[0] + col + x_offset < 0 or
                current_shape_pos[0] + col + x_offset >= GRID_COLS or
                current_shape_pos[1] + row + y_offset < 0 or
                current_shape_pos[1] + row + y_offset >= GRID_ROWS or
                grid[current_shape_pos[1] + row + y_offset][current_shape_pos[0] + col + x_offset]
            ):
                return False
    return True

# 移动方块
def move(x_offset, y_offset):
    if can_move(x_offset, y_offset):
        current_shape_pos[0] += x_offset
        current_shape_pos[1] += y_offset

# 固定方块到网格
def fix_shape():
    for row in range(len(current_shape)):
        for col in range(len(current_shape[row])):
            if current_shape[row][col]:
                grid[current_shape_pos[1] + row][current_shape_pos[0] + col] = 1

# 检查是否有满行
def check_lines():
    lines_cleared = 0
    for row in range(GRID_ROWS):
        if all(grid[row]):
            del grid[row]
            grid.insert(0, [0 for _ in range(GRID_COLS)])
            lines_cleared += 1
    return lines_cleared

# 绘制网格
def draw_grid():
    for row in range(GRID_ROWS):
        for col in range(GRID_COLS):
            if grid[row][col]:
                pygame.draw.rect(screen, WHITE, [col * GRID_SIZE, row * GRID_SIZE, GRID_SIZE, GRID_SIZE])

# 绘制当前方块
def draw_shape():
    for row in range(len(current_shape)):
        for col in range(len(current_shape[row])):
            if current_shape[row][col]:
                color = random.choice([CYAN, YELLOW, MAGENTA, GREEN, RED, ORANGE, BLUE])
                pygame.draw.rect(screen, color, [
                    (current_shape_pos[0] + col) * GRID_SIZE,
                    (current_shape_pos[1] + row) * GRID_SIZE,
                    GRID_SIZE, GRID_SIZE
                ])

# 游戏主循环
clock = pygame.time.Clock()
game_over = False
new_shape()

while not game_over:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True

    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        move(-1, 0)
    if keys[pygame.K_RIGHT]:
        move(1, 0)
    if keys[pygame.K_DOWN]:
        move(0, 1)

    if can_move(0, 1):
        move(0, 1)
    else:
        fix_shape()
        lines_cleared = check_lines()
        new_shape()

    screen.fill(BLACK)
    draw_grid()
    draw_shape()

    pygame.display.flip()
    clock.tick(10)

# 退出程序
pygame.quit()
代码解释:

• 定义了各种颜色和方块类型。

• 确定屏幕尺寸和小方格大小,以及游戏区域的行数和列数。

• 定义了当前方块、方块位置和游戏区域的网格。

• new_shape 函数用于生成新的方块。

• can_move 函数检查是否可以移动方块。

• move 函数根据条件移动方块。

• fix_shape 函数将当前方块固定到网格。

• check_lines 函数检查并处理满行。

• draw_grid 绘制网格,draw_shape 绘制当前方块。

• 在主循环中,处理事件、根据按键移动方块、根据情况固定方块并生成新方块,然后绘制画面并控制帧率。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值