当前位置: 首页 > news >正文

2026年深圳首台(套)重大技术装备扶持计划申报指南

以下是基于 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))

http://my.tv.sohu.com/us/255415869/707082797.shtml
http://my.tv.sohu.com/us/255415869/707082865.shtml
http://my.tv.sohu.com/us/255415869/707082790.shtml
http://my.tv.sohu.com/us/255415869/707083010.shtml
http://my.tv.sohu.com/us/255415869/707082699.shtml
http://my.tv.sohu.com/us/255415869/707082847.shtml
http://my.tv.sohu.com/us/255415869/707082845.shtml
http://my.tv.sohu.com/us/255415869/707082597.shtml
http://my.tv.sohu.com/us/255415869/707082840.shtml
http://my.tv.sohu.com/us/255415869/707082766.shtml
http://my.tv.sohu.com/us/255415869/707082584.shtml
http://my.tv.sohu.com/us/255415869/707082763.shtml
http://my.tv.sohu.com/us/255415869/707082677.shtml
http://my.tv.sohu.com/us/255415869/707082829.shtml
http://my.tv.sohu.com/us/255415869/707082755.shtml
http://my.tv.sohu.com/us/255415869/707082752.shtml
http://my.tv.sohu.com/us/255415869/707082751.shtml
http://my.tv.sohu.com/us/255415869/707082824.shtml
http://my.tv.sohu.com/us/255415869/707082663.shtml
http://my.tv.sohu.com/us/255415869/707082662.shtml
http://my.tv.sohu.com/us/255415869/707082661.shtml
http://my.tv.sohu.com/us/255415869/707082817.shtml
http://my.tv.sohu.com/us/255415869/707082747.shtml
http://my.tv.sohu.com/us/255415869/707082654.shtml
http://my.tv.sohu.com/us/255415869/707082808.shtml
http://my.tv.sohu.com/us/255415869/707082548.shtml
http://my.tv.sohu.com/us/255415869/707082498.shtml
http://my.tv.sohu.com/us/255415869/707082738.shtml

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

def lock_tetromino(self):
"""将落地的方块锁定到网格中"""
for y, row in enumerate(self.current_tetromino.shape):
for x, cell in enumerate(row):
if cell:
grid_y = self.current_te

http://www.jsqmd.com/news/535808/

相关文章:

  • 2026年3月25日技术资讯洞察:开源芯片革命、Postgres文件系统与AI Agent安全新范式
  • StructBERT情感分类模型效果展示:招聘JD情感倾向与雇主品牌分析
  • Linux系统管理命令大全与实战技巧
  • 从‘丑’到‘美’:用自定义导航栏拯救你的微信小程序颜值(附完整代码与避坑点)
  • 2026开年贵阳装修指南:五家现代简约风设计实力派深度横评 - 2026年企业推荐榜
  • TensorRT性能调优实战指南:从问题诊断到优化落地
  • PyTorch 2.8镜像应用场景:电商企业自建商品视频生成私有化系统案例
  • STM32F429 FreeRTOS - 集成Cmbacktrace实现高效故障回溯
  • 轻量级容器化部署:llama.cpp推理服务的弹性扩展实践指南
  • DIY USB 3.0 HUB全流程:从GL3523芯片选型到PCB布线避坑指南
  • MiniCPM-V-2_6基础教程:Ubuntu20.04环境下的快速部署与配置指南
  • MacBook扩展屏新思路:把闲置的Windows台式机变成无线绘图板或演示监视器
  • 基于ChatTTS的自定义PT文件文字转语音实战指南
  • Python开发者开源入门全攻略:从环境配置到第一个PR的30天实战指南
  • Oracle 不支持的字符集 (在类路径中添加 orai18n.jar): ZHS16GBK
  • 深度学习的python基础2:从numpy到torch.tensor
  • 清音刻墨Qwen3智能字幕对齐:开箱即用的字幕生成工具
  • 终极macOS清理指南:使用开源脚本免费释放磁盘空间
  • 全球地理边界GeoJSON完全手册:开发者必备的地理数据解决方案
  • 从零构建PoseC3D数据集:数据格式解析与自定义骨骼提取实战
  • 文远知行启动1亿美元回购,依托稳健业务进展,传递资本市场积极信号
  • Stalwart Mail Server企业级部署:现代化邮件服务器的终极解决方案
  • 基于STM32的毕设实战:从传感器数据采集到低功耗通信的完整链路实现
  • 当代码遇见笔迹:HANDWRITTEN.js 如何让数字文字重获手写温度
  • 检测的毕设领域创新的技术实现路径:从选题到系统落地
  • 从零搭建你的第一个量化策略:以Python和Tushare为例,5步实现简单回测
  • 移动UI自动化测试架构选型:Maestro微内核架构与性能基准方法论
  • 2026医疗仪器适配开关优质推荐榜:地址开关/工业标签/弹片开关/拨动开关/拨码开关/指拨开关/控制面板贴纸/推拉开关/选择指南 - 优质品牌商家
  • 网络协议分析AI应用:使用PyTorch进行网络流量异常检测
  • 新手避坑指南:从立创EDA专业版导出3D模型,完美匹配AD23的完整流程