俄罗斯方块,需要安装pygram模块

import pygame
import random

# 游戏区域的大小
WIDTH = 10
HEIGHT = 20
BLOCK_SIZE = 30

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

# 七种不同的方块形状
SHAPES = [
    [[1, 1, 1, 1]],  # 长条型
    [[1, 1], [1, 1]],  # 方块型
    [[1, 1, 0], [0, 1, 1]],  # Z 型
    [[0, 1, 1], [1, 1, 0]],  # S 型
    [[1, 0, 0], [1, 1, 1]],  # L 型
    [[0, 0, 1], [1, 1, 1]],  # J 型
    [[1, 1, 1], [0, 1, 0]]  # T 型
]

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

# 初始化 pygame
pygame.init()

# 设置屏幕大小
screen = pygame.display.set_mode((WIDTH * BLOCK_SIZE, HEIGHT * BLOCK_SIZE))
pygame.display.set_caption("俄罗斯方块")

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

# 初始化游戏区域
grid = [[0 for _ in range(WIDTH)] for _ in range(HEIGHT)]

# 生成新的方块
def generate_shape():
    shape = random.choice(SHAPES)
    color = random.choice(SHAPE_COLORS)
    x = WIDTH // 2 - len(shape[0]) // 2
    y = 0
    return shape, color, x, y

# 检查是否可以移动
def is_valid_move(shape, x, y):
    for row in range(len(shape)):
        for col in range(len(shape[row])):
            if shape[row][col] and (x + col < 0 or x + col >= WIDTH or y + row >= HEIGHT or grid[y + row][x + col]):
                return False
    return True

# 合并方块到游戏区域
def merge_shape(shape, color, x, y):
    for row in range(len(shape)):
        for col in range(len(shape[row])):
            if shape[row][col]:
                grid[y + row][x + col] = color

# 消除满行
def clear_lines():
    full_rows = [row for row in range(HEIGHT) if all(grid[row])]
    for row in full_rows:
        del grid[row]
        grid.insert(0, [0] * WIDTH)

# 游戏主循环
running = True
current_shape, current_color, current_x, current_y = generate_shape()

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 and is_valid_move(current_shape, current_x - 1, current_y):
                current_x -= 1
            elif event.key == pygame.K_RIGHT and is_valid_move(current_shape, current_x + 1, current_y):
                current_x += 1
            elif event.key == pygame.K_DOWN and is_valid_move(current_shape, current_x, current_y + 1):
                current_y += 1
            elif event.key == pygame.K_SPACE:
                # 旋转方块
                rotated_shape = [[current_shape[col][row] for col in range(len(current_shape))] for row in range(len(current_shape[0]) - 1, -1, -1)]
                if is_valid_move(rotated_shape, current_x, current_y):
                    current_shape = rotated_shape

    if is_valid_move(current_shape, current_x, current_y + 1):
        current_y += 1
    else:
        merge_shape(current_shape, current_color, current_x, current_y)
        clear_lines()
        current_shape, current_color, current_x, current_y = generate_shape()

    screen.fill(BLACK)

    for row in range(HEIGHT):
        for col in range(WIDTH):
            if grid[row][col]:
                pygame.draw.rect(screen, grid[row][col], (col * BLOCK_SIZE, row * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))

    for row in range(len(current_shape)):
        for col in range(len(current_shape[row])):
            if current_shape[row][col]:
                pygame.draw.rect(screen, current_color, ((current_x + col) * BLOCK_SIZE, (current_y + row) * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE))

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

pygame.quit()
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值