Python Tetromino怎么实现?python俄罗斯方块代码

Python Tetromino (俄罗斯方块)

下面是一个完整的、可运行的 Python Tetromino(俄罗斯方块)实现,使用 pygame 库。

安装依赖

pip install pygame

完整代码

import pygame
import random
import sys
# 初始化 pygame
pygame.init()
# ==================== 常量 ====================
BLOCK_SIZE = 30
COLS = 10
ROWS = 20
SCREEN_WIDTH = COLS  BLOCK_SIZE + 200  # 留出侧边栏
SCREEN_HEIGHT = ROWS  BLOCK_SIZE
# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GRAY = (40, 40, 40)
DARK_GRAY = (80, 80, 80)
# 方块颜色
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 - 红色
]
# 方块形状定义 (每个方块有4个旋转状态)
SHAPES = [
    # I
    [
        [[1, 1, 1, 1]],
        [[1], [1], [1], [1]],
    ],
    # J
    [
        [[1, 0, 0], [1, 1, 1]],
        [[1, 1], [1, 0], [1, 0]],
        [[1, 1, 1], [0, 0, 1]],
        [[0, 0, 1], [0, 1], [0, 1]],
    ],
    # L
    [
        [[0, 0, 1], [1, 1, 1]],
        [[1, 0], [1, 0], [1, 1]],
        [[1, 1, 1], [1, 0, 0]],
        [[0, 1], [0, 1], [1, 1]],
    ],
    # O
    [
        [[1, 1], [1, 1]],
    ],
    # S
    [
        [[0, 1, 1], [1, 1, 0]],
        [[1, 0], [1, 1], [0, 1]],
    ],
    # T
    [
        [[0, 1, 0], [1, 1, 1]],
        [[0, 1], [1, 1], [0, 1]],
        [[1, 1, 1], [0, 1, 0]],
        [[1, 0], [1, 1], [1, 0]],
    ],
    # Z
    [
        [[1, 1, 0], [0, 1, 1]],
        [[0, 1], [1, 1], [1, 0]],
    ],
]
# ==================== 方块类 ====================
class Piece:
    def __init__(self, shape_index=None):
        if shape_index is None:
            self.shape_index = random.randint(0, len(SHAPES) - 1)
        else:
            self.shape_index = shape_index
        self.color = COLORS[self.shape_index]
        self.rotation = 0
        # 初始位置:顶部中间
        self.x = COLS // 2 - 1
        self.y = 0
    def get_shape(self):
        """获取当前旋转状态下的形状"""
        shapes = SHAPES[self.shape_index]
        return shapes[self.rotation % len(shapes)]
    def rotate(self):
        """旋转方块"""
        self.rotation = (self.rotation + 1) % len(SHAPES[self.shape_index])
# ==================== 游戏主类 ====================
class Tetris:
    def __init__(self):
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("Tetromino - 俄罗斯方块")
        self.clock = pygame.time.Clock()
        self.font = pygame.font.SysFont("arial", 24)
        self.big_font = pygame.font.SysFont("arial", 48, bold=True)
        self.reset_game()
    def reset_game(self):
        """重置游戏状态"""
        self.board = [[None]  COLS for _ in range(ROWS)]
        self.current_piece = Piece()
        self.next_piece = Piece()
        self.score = 0
        self.level = 1
        self.lines_cleared = 0
        self.game_over = False
        self.drop_timer = 0
        self.drop_interval = 500  # 毫秒,随等级加快
    def get_valid_positions(self, piece, dx=0, dy=0):
        """获取方块所有有效位置(用于碰撞检测)"""
        shape = piece.get_shape()
        positions = []
        for r, row in enumerate(shape):
            for c, cell in enumerate(row):
                if cell:
                    new_x = piece.x + c + dx
                    new_y = piece.y + r + dy
                    positions.append((new_x, new_y))
        return positions
    def is_valid(self, piece, dx=0, dy=0):
        """检查方块位置是否合法"""
        positions = self.get_valid_positions(piece, dx, dy)
        for x, y in positions:
    

Python Tetromino怎么实现?python俄罗斯方块代码

全网最详细-使用python实现俄罗斯方块小游戏
加载中
全网最详细-使用python实现俄罗斯方块小游戏
# 检查边界 if x < 0 or x >= COLS or y >= ROWS: return False # 检查是否与已有方块重叠 if y >= 0 and self.board[y][x] is not None: return False return True def lock_piece(self): """将当前方块锁定到棋盘""" shape = self.current_piece.get_shape() for r, row in enumerate(shape): for c, cell in enumerate(row): if cell: x = self.current_piece.x + c y = self.current_piece.y + r if 0 <= y < ROWS and 0 <= x < COLS: self.board[y][x] = self.current_piece.color def clear_lines(self): """清除完整行并计分""" lines_to_clear = [] for r in range(ROWS): if all(cell is not None for cell in self.board[r]): lines_to_clear.append(r) if lines_to_clear: # 从下往上删除行 for line in sorted(lines_to_clear, reverse=True): del self.board[line] self.board.insert(0, [None] COLS) num_lines = len(lines_to_clear) # 计分规则 points = [0, 100, 300, 500, 800] self.score += points[num_lines] self.level self.lines_cleared += num_lines # 每10行升一级 self.level = self.lines_cleared // 10 + 1 self.drop_interval = max(100, 500 - (self.level - 1) 40) def spawn_new_piece(self): """生成新方块""" self.current_piece = self.next_piece self.next_piece = Piece() # 检查新方块是否立即碰撞(游戏结束) if not self.is_valid(self.current_piece): self.game_over = True def get_ghost_piece(self): """获取幽灵方块(预测落点)""" ghost = Piece() ghost.shape_index = self.current_piece.shape_index ghost.color = self.current_piece.color ghost.rotation = self.current_piece.rotation ghost.x = self.current_piece.x ghost.y = self.current_piece.y while self.is_valid(ghost, dy=1): ghost.y += 1 return ghost def draw_board(self): """绘制游戏棋盘""" # 背景 self.screen.fill(BLACK) # 绘制网格线 for r in range(ROWS + 1): pygame.draw.line(self.screen, GRAY, (0, r BLOCK_SIZE), (COLS BLOCK_SIZE, r BLOCK_SIZE)) for c in range(COLS + 1): pygame.draw.line(self.screen, GRAY, (c BLOCK_SIZE, 0), (c BLOCK_SIZE, ROWS BLOCK_SIZE)) # 绘制已锁定的方块 for r in range(ROWS): for c in range(COLS): if self.board[r][c] is not None: rect = pygame.Rect(c BLOCK_SIZE, r BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) pygame.draw.rect(self.screen, self.board[r][c], rect) pygame.draw.rect(self.screen, DARK_GRAY, rect, 2) # 绘制幽灵方块 if not self.game_over: ghost = self.get_ghost_piece() shape = ghost.get_shape() for r, row in enumerate(shape): for c, cell in enumerate(row): if cell: gx = ghost.x + c gy = ghost.y + r if 0 <= gy < ROWS: rect = pygame.Rect(gx BLOCK_SIZE, gy BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) pygame.draw.rect(self.screen, (255, 255, 255, 50), rect, 2) # 绘制当前方块 if not self.game_over: shape = self.current_piece.get_shape()

Python Tetromino怎么实现?python俄罗斯方块代码

for r, row in enumerate(shape): for c, cell in enumerate(row): if cell: x = self.current_piece.x + c y = self.current_piece.y + r if y >= 0: # 只绘制可见部分 rect = pygame.Rect(x BLOCK_SIZE, y BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) pygame.draw.rect(self.screen, self.current_piece.color, rect) pygame.draw.rect(self.screen, DARK_GRAY, rect, 2) def draw_sidebar(self): """绘制侧边栏(分数、下一个方块等)""" sidebar_x = COLS BLOCK_SIZE + 20 # 标题 title = self.big_font.render("TETRIS", True, WHITE) self.screen.blit(title, (sidebar_x, 20)) # 分数 score_text = self.font.render(f"Score: {self.score}", True, WHITE) self.screen.blit(score_text, (sidebar_x, 100)) # 等级 level_text = self.font.render(f"Level: {self.level}", True, WHITE) self.screen.blit(level_text, (sidebar_x, 140)) # 行数 lines_text = self.font.render(f"Lines: {self.lines_cleared}", True, WHITE) self.screen.blit(lines_text, (sidebar_x, 180)) # 下一个方块 next_title = self.font.render("Next:", True, WHITE) self.screen.blit(next_title, (sidebar_x, 240)) # 绘制下一个方块预览 shape = self.next_piece.get_shape() preview_x = sidebar_x + 10 preview_y = 280 for r, row in enumerate(shape): for c, cell in enumerate(row): if cell: rect = pygame.Rect(preview_x + c BLOCK_SIZE, preview_y + r BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE) pygame.draw.rect(self.screen, self.next_piece.color, rect) pygame.draw.rect(self.screen, DARK_GRAY, rect, 2) # 操作说明 controls_y = 400 controls = [ "Controls:", "← → : Move", "↑ : Rotate", "↓ : Soft Drop", "Space: Hard Drop", "P : Pause", "R : Restart", "Q : Quit" ] for i, text in enumerate(controls): color = WHITE if i > 0 else (200, 200, 0) t = self.font.render(text, True, color) self.screen.blit(t, (sidebar_x, controls_y + i 28)) # 游戏结束 if self.game_over: overlay = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT)) overlay.set_alpha(180) overlay.fill(BLACK) self.screen.blit(overlay, (0, 0)) go_text = self.big_font.render("GAME OVER", True, (255, 0, 0)) text_rect = go_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 - 30)) self.screen.blit(go_text, text_rect) score_text = self.font.render(f"Final Score: {self.score}", True, WHITE) score_rect = score_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 20)) self.screen.blit(score_text, score_rect) restart_text = self.font.render("Press R to Restart", True, WHITE) restart_rect = restart_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2 + 60)) self.screen.blit(restart_text, restart_rect) def draw(self): """绘制整个游戏画面""" self.draw_board() self.draw_sidebar() pygame.display.flip() def handle_input(self): """处理用户输入""" keys = pygame.key.get_pressed() dt = self.clock.get_time() if not self.game_over: # 左右移动 if keys[pygame.K_LEFT]: if self.is_valid(self.current_piece, dx=-1):

Python Tetromino怎么实现?python俄罗斯方块代码

self.current_piece.x -= 1 if keys[pygame.K_RIGHT]: if self.is_valid(self.current_piece, dx=1): self.current_piece.x += 1 # 旋转 if keys[pygame.K_UP]: old_rotation = self.current_piece.rotation self.current_piece.rotate() if not self.is_valid(self.current_piece): # 尝试墙踢(wall kick) for offset in [-1, 1, -2, 2]: if self.is_valid(self.current_piece, dx=offset): self.current_piece.x += offset break else: # 回滚旋转 self.current_piece.rotation = old_rotation break # 软降 if keys[pygame.K_DOWN]: self.drop_timer += dt 3 # 加速下落 # 硬降 if keys[pygame.K_SPACE]: while self.is_valid(self.current_piece, dy=1): self.current_piece.y += 1 self.lock_piece() self.clear_lines() self.spawn_new_piece() self.drop_timer = 0 # 暂停 if keys[pygame.K_p]: pass # 简化:不实现暂停切换 # 重启 if keys[pygame.K_r]: self.reset_game() # 退出 if keys[pygame.K_q]: pygame.quit() sys.exit() def update(self): """更新游戏状态""" if not self.game_over: self.drop_timer += self.clock.get_time() if self.drop_timer >= self.drop_interval: if self.is_valid(self.current_piece, dy=1): self.current_piece.y += 1 else: # 锁定方块 self.lock_piece() self.clear_lines() self.spawn_new_piece() self.drop_timer = 0 def run(self): """游戏主循环""" 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_q: running = False elif event.key == pygame.K_r: self.reset_game() self.handle_input() self.update() self.draw() self.clock.tick(60) # 60 FPS pygame.quit() sys.exit() # ==================== 运行游戏 ==================== if __name__ == "__main__": game = Tetris() game.run()

功能特性

功能 说明
7种标准方块 I, J, L, O, S, T, Z
旋转系统 支持墙踢(Wall Kick)
幽灵方块 白色半透明预览落点
下一个方块预览 侧边栏显示下一个方块
计分系统 单行100分,双行300分,三行500分,四行800分(乘以等级)
等级系统 每清除10行升一级,下落速度加快
硬降/软降 空格硬降,↓键软降

操作说明

  • : 左右移动
  • : 旋转
  • : 加速下落
  • 空格 : 直接落到底部
  • R : 重新开始
  • Q : 退出游戏

运行方式

python tetromino.py

确保已安装 pygame:pip install pygame

首发原创文章,作者:王坚‌,如若转载,请注明出处:https://idctop.com/article/484379.html

赞 (0)
Curese Python是什么?Python新手入门教程
上一篇 2026年7月12日 01:57
cdn供应商哪家好,cdn供应商排名
下一篇 2026年7月12日 01:59

相关推荐

  • 服务器带宽200m速度快吗?200m带宽能带多少用户

    200M服务器带宽是中大型网站、高并发业务及流媒体平台跨越性能瓶颈、实现业务流畅运行的关键分水岭,其核心价值在于能够以极高的吞吐量解决数据传输拥堵问题,显著提升用户访问体验并降低跳出率,选择并优化200M带宽,不仅仅是购买流量,更是构建高性能网络架构的战略投资,直接决定了业务承载能力的上限,200M带宽的核心性……

    2026年4月8日
    12100
  • 个人免费云主机怎么用?有哪些稳定好用的免费云服务器推荐

    个人免费云主机是开发者入门、搭建博客或测试代码的理想选择,但需注意其性能限制和稳定性不如付费方案,在数字化转型的浪潮中,越来越多的个人开发者、学生以及小型创业者开始寻找低成本的服务器解决方案,对于预算有限但技术热情高涨的用户来说,拥有一台属于自己的云服务器意味着能够完全掌控自己的数据,体验从代码部署到域名解析的……

    2026年6月14日
    3800
  • 域名服务器填什么?新手如何正确配置DNS服务器地址?

    DNS服务器地址通常填路由器和电脑自动获取的运营商分配地址,绝大多数用户无需手动修改;只有当自动获取的DNS解析缓慢、打不开网页或需要特殊网络环境时,才建议手动填入公共DNS服务器地址,dns服务器地址填什么?先搞懂这3个关键场景很多新手第一次打开路由器后台,看到“DNS服务器”这一栏就懵了,网上教程众说纷纭……

    2026年9月8日
    1400
  • 服务器监管部门在哪里查|权威机构监管流程解析

    服务器监管部门是企业或组织中负责监控、维护和优化服务器系统的核心团队,确保数据安全、业务连续性和高效运营,在数字化时代,服务器作为信息基础设施的支柱,其稳定性和安全性直接影响整体业务表现,一个专业的服务器监管部门通过实时监控、预防性维护和应急响应,能有效降低宕机风险、抵御网络攻击,并满足合规要求,从而提升组织竞……

    2026年2月9日
    10830
  • 知乎服务器为什么这么慢?知乎官方回应来了!

    服务器知乎是一个专注于服务器技术和知识分享的在线平台,旨在为IT专业人士、开发者和企业提供权威、实用的信息交流空间,它结合了知乎的问答社区模式,聚焦于服务器硬件、软件、管理、优化和安全等核心领域,帮助用户解决实际问题,提升技术专长,通过专业内容共享,服务器知乎推动行业创新,降低技术门槛,让复杂概念变得通俗易懂……

    2026年2月9日
    14730
  • 漫威中的服务器有哪些

    漫威电影宇宙里的“服务器”,从来不是一台台冰冷的铁皮箱子,而是决定超级英雄能否翻盘的关键设施——从神盾局的空中要塞到瓦坎达的振金机柜,从贾维斯栖身的私人工室到佐拉博士的意识坟场,每一台服务器背后都有一套完整的“数字生存法则”,神盾局的服务器矩阵:最像现实互联网公司的高可用架构如果把漫威里的服务器当成一个IT系统……

    2026年8月24日
    800
  • 服务器日租靠谱吗,临时服务器日租哪个平台比较好?

    概念、优势与选择指南什么是服务器日租?服务器日租是指用户根据短期需求,以天为单位租赁服务器资源的一种计费模式,与传统的月租或年租模式不同,它打破了长期合约的限制,允许用户根据实际业务波峰或特定任务的需求,按需付费,用完即止,服务器日租的核心优势成本极低:仅需为实际使用的天数付费,能够显著降低短期项目的运营成本……

    2026年7月13日
    700
  • 服务器适合装什么显卡,哪款性价比最高?

    服务器选显卡,先看场景再谈型号:AI训练无脑上NVIDIA企业级计算卡,推理和渲染选L40S或RTX 6000 Ada,预算有限用RTX 4090改涡轮版,纯亮机任务一张几十元的亮机卡就够, 这个结论不是拍脑袋,而是基于近三年来国内IDC机房的真实上架数据统计得出的,服务器显卡和台式机显卡,差在哪很多人第一次接……

    2026年9月6日
    1200
  • 服务器安全系统有哪些类型,哪个服务器防护软件好用?

    服务器安全系统有哪些?简单说,它不是装一个杀毒软件就完事,而是把网络边界、主机系统、应用入口、数据存储、运维通道和物理机房逐层设防,常见组合包括防火墙、IDS/IPS、WAF、堡垒机、日志审计、漏洞扫描、终端防护、备份容灾和态势感知,服务器安全系统解决什么问题服务器一旦暴露公网,攻击面集中在几个位置:开放端口被……

    2026年9月18日
    500
  • 服务器搭在监控系统怎么操作?服务器监控系统搭建教程

    构建高效稳定的IT运维体系,核心在于实现从“被动响应”向“主动预防”的转变,服务器搭在监控系统不仅是技术堆叠,更是保障业务连续性的战略基石,一套成熟的监控系统能实时洞察硬件寿命、资源瓶颈与应用状态,将潜在故障扼杀在萌芽阶段,确保数据中心始终处于最佳运行效能, 核心价值:构建全链路可视化的防御体系监控系统的本质是……

    2026年3月10日
    12100

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注