有赞新零售社区

发帖
小红书»C++:const 的空间,常量也能占内存?

C++:const 的空间,常量也能占内存?

38db16b0d91d 04-14 453 浏览 0 评论 | 只看楼主 [打印]
以下是基于 Python + Pygame 实现的完整俄罗斯方块游戏代码,包含核心功能(方块生成、移动、旋转、消除、计分),注释详细可直接运行:

第一步:安装依赖

先安装 Pygame 库:
pip install pygame
第二步:完整代码
import pygame
import random

# 初始化Pygame
pygame.init()

# 游戏常量
SCREEN_WIDTH = 300  # 屏幕宽度
SCREEN_HEIGHT = 600  # 屏幕高度
BLOCK_SIZE = 30  # 方块大小(像素)
GRID_WIDTH = SCREEN_WIDTH // BLOCK_SIZE  # 网格列数(10列)
GRID_HEIGHT = SCREEN_HEIGHT // BLOCK_SIZE  # 网格行数(20行)

# 颜色定义(RGB)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (128, 128, 128)
COLORS = [
    (0, 255, 255),    # 青色(I型)
    (0, 0, 255),      # 蓝色(J型)
    (255, 165, 0),    # 橙色(L型)
    (255, 255, 0),    # 黄色(O型)
    (0, 255, 0),      # 绿色(S型)
    (128, 0, 128),    # 紫色(T型)
    (255, 0, 0)       # 红色(Z型)
]

# 俄罗斯方块7种形状(0=空,1=方块)
SHAPES = [
    [[1, 1, 1, 1]],  # I型(横)
    [[1, 0, 0], [1, 1, 1]],  # J型
    [[0, 0, 1], [1, 1, 1]],  # L型
    [[1, 1], [1, 1]],  # O型(正方形)
    [[0, 1, 1], [1, 1, 0]],  # S型
    [[0, 1, 0], [1, 1, 1]],  # T型
    [[1, 1, 0], [0, 1, 1]]   # Z型
]

# 屏幕设置
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("俄罗斯方块")

# 时钟(控制游戏帧率)
clock = pygame.time.Clock()
FPS = 10

# 字体设置(计分板)
font = pygame.font.Font(None, 36)


class Tetromino:
    """方块类:管理单个下落的俄罗斯方块"""
    def __init__(self):
        self.shape = random.choice(SHAPES)  # 随机选择形状
        self.color = random.choice(COLORS)  # 随机选择颜色
        self.x = GRID_WIDTH // 2 - len(self.shape[0]) // 2  # 初始X位置(居中)
        self.y = 0  # 初始Y位置(顶部)

    def rotate(self):
        """旋转方块(矩阵转置+逆序)"""
        # 转置矩阵
        rotated = list(zip(*self.shape[::-1]))
        # 转换为列表格式
        self.shape = [list(row) for row in rotated]

    def draw(self):
        """绘制方块到屏幕"""
        for y, row in enumerate(self.shape):
            for x, cell in enumerate(row):
                if cell:
                    # 计算方块在屏幕上的实际坐标
                    screen_x = (self.x + x) * BLOCK_SIZE
                    screen_y = (self.y + y) * BLOCK_SIZE
                    # 绘制方块(带边框)
                    pygame.draw.rect(screen, self.color, (screen_x, screen_y, BLOCK_SIZE - 1, BLOCK_SIZE - 1))


class Game:
    """游戏主类:管理网格、碰撞检测、计分"""
    def __init__(self):
        self.grid = [[BLACK for _ in range(GRID_WIDTH)] for _ in range(GRID_HEIGHT)]  # 游戏网格(初始全黑)
        self.current_tetromino = Tetromino()  # 当前下落的方块
        self.score = 0  # 分数
        self.game_over = False  # 游戏结束标志

    def draw_grid(self):
        """绘制游戏网格(已落地的方块)"""
        for y in range(GRID_HEIGHT):
            for x in range(GRID_WIDTH):
                pygame.draw.rect(screen, self.grid[y][x], (x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE - 1, BLOCK_SIZE - 1))

    def check_collision(self, tetromino, dx=0, dy=0, rotated=False):
        """检测碰撞:dx=X偏移,dy=Y偏移,rotated=是否旋转后的形状"""
        shape = tetromino.shape
        if rotated:
            # 临时计算旋转后的形状
            shape = [list(row) for row in zip(*shape[::-1])]

        for y, row in enumerate(shape):
            for x, cell in enumerate(row):
                if cell:
                    # 计算偏移后的坐标
                    new_x = tetromino.x + x + dx
                    new_y = tetromino.y + y + dy
                    # 碰撞条件:超出左右边界、超出下边界、碰到已落地的方块
                    if (new_x < 0 or new_x >= GRID_WIDTH or
                        new_y >= GRID_HEIGHT or
                        (new_y >= 0 and self.grid[new_y][new_x] != BLACK)):
                        return True
        return False

本帖最后由 38db16b0d91d 于 2026-4-13 20:52 编辑
用手机打开
收藏 ··· 回复
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    复制链接
    新浪微博
    QQ空间
    微信扫码
    • 回复

    • 评分

    客服工作时间是9:00-18:00,客服妹子当前不在线,若不能及时回复请谅解。试试右上角的搜索吧,论坛有丰富的经验贴、公告贴,相信一定能够帮到您~

    复制成功