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

Python编写的俄罗斯方块小游戏

文章目录

  • 游戏页面
  • 实现代码

游戏页面

左右键移动方块位置,上键切换方块形态。
在这里插入图片描述

实现代码

import pygame
import random# 初始化 Pygame
pygame.init()# 定义颜色
colors = [(0, 0, 0),  # 黑色(255, 0, 0),  # 红色(0, 255, 0),  # 绿色(0, 0, 255),  # 蓝色(255, 255, 0),  # 黄色(255, 0, 255),  # 紫色(0, 255, 255)  # 青色
]# 俄罗斯方块形状
shapes = [[[1, 1, 1, 1]],[[1, 1],[1, 1]],[[0, 1, 1],[1, 1, 0]],[[1, 1, 0],[0, 1, 1]],[[1, 1, 1],[0, 1, 0]],[[1, 1, 1],[1, 0, 0]],[[1, 1, 1],[0, 0, 1]]
]# 设置游戏屏幕
screen_width = 300
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('俄罗斯方块')# 游戏网格
grid = [[0 for _ in range(10)] for _ in range(20)]# 初始化时钟
clock = pygame.time.Clock()# 定义方块类
class Shape:def __init__(self):self.shape = random.choice(shapes)self.color = random.randint(1, len(colors) - 1)self.x = 3self.y = 0def rotate(self):self.shape = [list(row) for row in zip(*self.shape[::-1])]def draw(self):for i, row in enumerate(self.shape):for j, val in enumerate(row):if val:pygame.draw.rect(screen, colors[self.color], (self.x * 30 + j * 30, self.y * 30 + i * 30, 30, 30))def check_collision(shape):for i, row in enumerate(shape.shape):for j, val in enumerate(row):if val:if shape.x + j < 0 or shape.x + j >= 10 or shape.y + i >= 20 or grid[shape.y + i][shape.x + j]:return Truereturn Falsedef merge_shape(shape):for i, row in enumerate(shape.shape):for j, val in enumerate(row):if val:grid[shape.y + i][shape.x + j] = shape.colordef remove_full_lines():global gridgrid = [row for row in grid if not all(row)]while len(grid) < 20:grid.insert(0, [0 for _ in range(10)])def draw_grid():for y in range(20):for x in range(10):pygame.draw.rect(screen, colors[grid[y][x]], (x * 30, y * 30, 30, 30))def main():running = Truecurrent_shape = Shape()fall_time = 0while running:screen.fill((0, 0, 0))draw_grid()current_shape.draw()for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseif event.type == pygame.KEYDOWN:if event.key == pygame.K_LEFT:current_shape.x -= 1if check_collision(current_shape):current_shape.x += 1if event.key == pygame.K_RIGHT:current_shape.x += 1if check_collision(current_shape):current_shape.x -= 1if event.key == pygame.K_DOWN:current_shape.y += 1if check_collision(current_shape):current_shape.y -= 1if event.key == pygame.K_UP:current_shape.rotate()if check_collision(current_shape):current_shape.rotate()current_shape.rotate()current_shape.rotate()fall_time += clock.get_rawtime()clock.tick()if fall_time / 1000 >= 0.5:fall_time = 0current_shape.y += 1if check_collision(current_shape):current_shape.y -= 1merge_shape(current_shape)remove_full_lines()current_shape = Shape()if check_collision(current_shape):running = Falsepygame.display.update()pygame.quit()if __name__ == "__main__":main()
http://www.lryc.cn/news/396379.html

相关文章:

  • 前端直连小票打印机,前端静默打印,js静默打印解决方案
  • python批量读取Excel数据写入word
  • Unity 常用取整方法
  • Apache Seata Mac下的Seata Demo环境搭建
  • 记录|C#安装+HslCommunication安装
  • Android 12系统源码_设备设置(一)Settings介绍
  • 如何查看GD32 Keil和IAR工程的map文件
  • 1Panel安装命令脚本大全,多Linux操作系统版本
  • 校园电动车安全监控和调度系统-计算机毕业设计源码13028
  • 【LLM之Agent】ReAct论文阅读笔记
  • LeetCode 125. 验证回文串
  • IT审计必看!对比旧版,CISA考试改版升级亮点和重点内容是什么?
  • 充电宝哪个牌子公认质量好?哪家充电宝好用?4款口碑好充电宝
  • Python实现图像添加水印的方法
  • MemFire Cloud: 一种全新定义后端即服务的解决方案
  • 职业教育软件测试实验实训室建设应用案例
  • 如何判断一个js对象为数组类型
  • Nacos2.X 配置中心源码分析:客户端如何拉取配置、服务端配置发布客户端监听机制
  • phpstudy框架,window平台,如何开端口给局域网访问?
  • 高性能Python网络框架实现网络应用详解
  • 万字学习——DCU编程实战
  • Neo4j 图数据库 高级操作
  • 《RWKV》论文笔记
  • 相机光学(二十九)——显色指数(Ra)
  • 【Swoole 的生命周期,文件描述符,协程数量,以及默认值】
  • “不要卷模型,要卷应用”之高考志愿填报智能体
  • k8s离线部署芋道源码后端
  • 图论·Day01
  • hutool ExcelUtil 导出导入excel
  • 打卡第7天-----哈希表