简单改进俄罗斯方块

news/2025/2/25 21:31:01/文章来源:https://www.cnblogs.com/cpdd1/p/18737284

对俄罗斯方块进行一部分优化:
参考地址:https://www.cnblogs.com/27dCnc/p/18568655
一、作业要求:从网上寻找一个项目或者小游戏,找出里面存在的不足之处,并且将它的不足之处优化。
二、运行环境:Windows
三、编译器:VSCODE
四、源代码*

点击查看代码 import sys import pygame from pygame.locals import * import random

class Block:
blk_color = [(255, 255, 255),(255, 255, 0),(255, 0, 255),(0, 255, 255),(255, 0, 0),(0, 255, 0),(0, 0, 255),(32,32,32)]
BLANK = 7
type_coord=[[[-1,0],[0,0],[1,0],[2,0]]
,[[-1,0],[0,0],[1,0],[0,1]]
,[[-1,0],[0,0],[-1,1],[0,1]]
,[[-1,0],[0,0],[0,1],[1,1]]
,[[0,0],[1,0],[-1,1],[0,1]]
,[[-1,0],[0,0],[1,0],[1,1]]
,[[-1,0],[0,0],[1,0],[-1,1]]]
type_rotate = []

def __init__(self,x,y,blk,angle):self.x = xself.y = yself.blk = blkself.angle = angle@staticmethod
def rotate(no):rt_all = []rt = Block.type_coord[no][:]cx,cy=0,0for b in range(4):rt[b][0],rt[b][1] = rt[b][0]*4,rt[b][1]*4cx += rt[b][0]cy += rt[b][1]cx = (cx)//8*2 if no !=6 else (cx+4)//8*2cy = (cy)//8*2 if no !=6 else (cy-4)//8*2rt_all.append(rt)for r in range(3):rt_new = []for b in range(4):rt_new.append([cx + (cy-rt[b][1]),cy-(cx-rt[b][0])])rt_all.append(rt_new)rt = rt_newfor r in range(4):for b in range(4):rt_all[r][b][0] //= 4rt_all[r][b][1] //= 4return rt_all
@staticmethod
def init_rotate():for r in range(7):Block.type_rotate.append(Block.rotate(r))

class TRS:
screen = None
map = [[Block.BLANK]*10 for i in range(20)]
STATUS = 0
cbk = None

def __init__(self,screen):TRS.screen = screen@staticmethod
def action(key_pressed):if(key_pressed[K_LEFT] and TRS.check_action(TRS.cbk.x-1,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle)):TRS.cbk.x -= 1elif (key_pressed[K_RIGHT] and TRS.check_action(TRS.cbk.x+1,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle)):TRS.cbk.x += 1elif (key_pressed[K_UP] and TRS.check_action(TRS.cbk.x,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle+1)):TRS.cbk.angle += 1elif (key_pressed[K_DOWN] and TRS.check_action(TRS.cbk.x,TRS.cbk.y+1,TRS.cbk.blk,TRS.cbk.angle)):TRS.cbk.y += 1@staticmethod
def new_blk():TRS.cbk = Block(5,0,random.randint(0,6),0)
@staticmethod
def check_action(x,y,blk,angle):tr = Block.type_rotate[blk][angle%4]for b in range(4):bx,by = x + tr[b][0],y + tr[b][1]if(bx<0 or bx>9 or by <0 or by>19 or TRS.map[by][bx]!=Block.BLANK):return Falsereturn True
@staticmethod
def check_drop():if TRS.check_action(TRS.cbk.x,TRS.cbk.y+1,TRS.cbk.blk,TRS.cbk.angle):TRS.cbk.y += 1else:TRS.STATUS = 2@staticmethod
def check_clear():blk = Block.type_rotate[TRS.cbk.blk][TRS.cbk.angle%4]row = list({TRS.cbk.y + blk[i][1] for i in range(4)})row.sort()row.reverse()for b in range(4):TRS.map[TRS.cbk.y + blk[b][1]][TRS.cbk.x + blk[b][0]] = TRS.cbk.blkdel_rows = 0for r in row:if not (Block.BLANK in TRS.map[r]):TRS.map.pop(r)del_rows += 1for d in range(del_rows):TRS.map.insert(0,[Block.BLANK for i in range(10)])@staticmethod
def print_game():TRS.screen.fill((0, 0, 0))for row in range(20):for col in range(10):pygame.draw.rect(TRS.screen, Block.blk_color[TRS.map[row][col]], ((col*21,row*21), (20, 20)), 0)blk = Block.type_rotate[TRS.cbk.blk][TRS.cbk.angle%4]for b in range(4):pygame.draw.rect(TRS.screen, Block.blk_color[TRS.cbk.blk], (((TRS.cbk.x+blk[b][0])*21,(TRS.cbk.y+blk[b][1])*21), (20, 20)), 0)

class App:
def init(self):
pygame.init()
screen = pygame.display.set_mode((300,430))
Block.init_rotate()
TRS(screen)

def main(self):clock = pygame.time.Clock()   # 创建游戏时钟count = 1# 进入游戏循环while True:# 设置刷新帧率clock.tick(15)# 事件检测for event in pygame.event.get():if event.type == pygame.QUIT:   # 退出事件sys.exit()if TRS.STATUS == 0:TRS.new_blk()if TRS.check_action(TRS.cbk.x,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle):TRS.STATUS = 1else:TRS.STATUS = 3print("GAME OVER")elif TRS.STATUS == 1:TRS.action(pygame.key.get_pressed())if count % 10 == 0:TRS.check_drop()elif TRS.STATUS == 2:TRS.check_clear()TRS.STATUS = 0TRS.print_game()pygame.display.update()   #刷新屏幕count += 1

App().main()


。 五、运行结果:[![](https://img2024.cnblogs.com/blog/3606924/202502/3606924-20250225212553066-687638030.png)]()![](https://img2024.cnblogs.com/blog/3606924/202502/3606924-20250225212916744-2088179227.png)

六、代码优化:由于源代码无法显示得分以及用户用时情况,故优化一个计时以及得分牌子,来反映用户实时情况;
代码:

点击查看代码
import sys
import pygame
from pygame.locals import *
import random
import timeclass Block:blk_color = [(255, 255, 255),(255, 255, 0),(255, 0, 255),(0, 255, 255),(255, 0, 0),(0, 255, 0),(0, 0, 255),(32,32,32)]BLANK = 7type_coord=[[[-1,0],[0,0],[1,0],[2,0]]\,[[-1,0],[0,0],[1,0],[0,1]]\,[[-1,0],[0,0],[-1,1],[0,1]]\,[[-1,0],[0,0],[0,1],[1,1]]\,[[0,0],[1,0],[-1,1],[0,1]]\,[[-1,0],[0,0],[1,0],[1,1]]\,[[-1,0],[0,0],[1,0],[-1,1]]]type_rotate = []def __init__(self,x,y,blk,angle):self.x = xself.y = yself.blk = blkself.angle = angle@staticmethoddef rotate(no):rt_all = []rt = Block.type_coord[no][:]cx,cy=0,0for b in range(4):rt[b][0],rt[b][1] = rt[b][0]*4,rt[b][1]*4cx += rt[b][0]cy += rt[b][1]cx = (cx)//8*2 if no !=6 else (cx+4)//8*2cy = (cy)//8*2 if no !=6 else (cy-4)//8*2rt_all.append(rt)for r in range(3):rt_new = []for b in range(4):rt_new.append([cx + (cy-rt[b][1]),cy-(cx-rt[b][0])])rt_all.append(rt_new)rt = rt_newfor r in range(4):for b in range(4):rt_all[r][b][0] //= 4rt_all[r][b][1] //= 4return rt_all@staticmethoddef init_rotate():for r in range(7):Block.type_rotate.append(Block.rotate(r))class TRS:screen = Nonemap = [[Block.BLANK]*10 for i in range(20)]STATUS = 0cbk = Nonescore = 0  # Added score variablestart_time = time.time()  # Added timer variabledef __init__(self,screen):TRS.screen = screen@staticmethoddef action(key_pressed):if(key_pressed[K_LEFT] and TRS.check_action(TRS.cbk.x-1,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle)):TRS.cbk.x -= 1elif (key_pressed[K_RIGHT] and TRS.check_action(TRS.cbk.x+1,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle)):TRS.cbk.x += 1elif (key_pressed[K_UP] and TRS.check_action(TRS.cbk.x,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle+1)):TRS.cbk.angle += 1elif (key_pressed[K_DOWN] and TRS.check_action(TRS.cbk.x,TRS.cbk.y+1,TRS.cbk.blk,TRS.cbk.angle)):TRS.cbk.y += 1@staticmethoddef new_blk():TRS.cbk = Block(5,0,random.randint(0,6),0)@staticmethoddef check_action(x,y,blk,angle):tr = Block.type_rotate[blk][angle%4]for b in range(4):bx,by = x + tr[b][0],y + tr[b][1]if(bx<0 or bx>9 or by <0 or by>19 or TRS.map[by][bx]!=Block.BLANK):return Falsereturn True@staticmethoddef check_drop():if TRS.check_action(TRS.cbk.x,TRS.cbk.y+1,TRS.cbk.blk,TRS.cbk.angle):TRS.cbk.y += 1else:TRS.STATUS = 2@staticmethoddef check_clear():blk = Block.type_rotate[TRS.cbk.blk][TRS.cbk.angle%4]row = list({TRS.cbk.y + blk[i][1] for i in range(4)})row.sort()row.reverse()for b in range(4):TRS.map[TRS.cbk.y + blk[b][1]][TRS.cbk.x + blk[b][0]] = TRS.cbk.blkdel_rows = 0for r in row:if not (Block.BLANK in TRS.map[r]):TRS.map.pop(r)del_rows += 1for d in range(del_rows):TRS.map.insert(0,[Block.BLANK for i in range(10)])# Update score based on the number of rows clearedif del_rows > 0:TRS.score += del_rows * 100  # 100 points per cleared row@staticmethoddef print_game():TRS.screen.fill((0, 0, 0))for row in range(20):for col in range(10):pygame.draw.rect(TRS.screen, Block.blk_color[TRS.map[row][col]], ((col*21,row*21), (20, 20)), 0)blk = Block.type_rotate[TRS.cbk.blk][TRS.cbk.angle%4]for b in range(4):pygame.draw.rect(TRS.screen, Block.blk_color[TRS.cbk.blk]], (((TRS.cbk.x+blk[b][0])*21,(TRS.cbk.y+blk[b][1])*21), (20, 20)), 0)# Display score and timerfont = pygame.font.SysFont("Arial", 24)score_text = font.render(f"Score: {TRS.score}", True, (255, 255, 255))elapsed_time = int(time.time() - TRS.start_time)timer_text = font.render(f"Time: {elapsed_time}s", True, (255, 255, 255))TRS.screen.blit(score_text, (10, 430 - 50))TRS.screen.blit(timer_text, (10, 430 - 30))class App:def __init__(self):pygame.init()screen = pygame.display.set_mode((300,430))Block.init_rotate()TRS(screen)def main(self):clock = pygame.time.Clock()   # 创建游戏时钟count = 1# 进入游戏循环while True:# 设置刷新帧率clock.tick(15)# 事件检测for event in pygame.event.get():if event.type == pygame.QUIT:   # 退出事件sys.exit()if TRS.STATUS == 0:TRS.new_blk()if TRS.check_action(TRS.cbk.x,TRS.cbk.y,TRS.cbk.blk,TRS.cbk.angle):TRS.STATUS = 1else:TRS.STATUS = 3print("GAME OVER")elif TRS.STATUS == 1:TRS.action(pygame.key.get_pressed())if count % 10 == 0:TRS.check_drop()elif TRS.STATUS == 2:TRS.check_clear()TRS.STATUS = 0TRS.print_game()pygame.display.update()   #刷新屏幕count += 1App().main()
运行结果:![](https://img2024.cnblogs.com/blog/3606924/202502/3606924-20250225211536297-362718450.png) 注意事项:得分和计时器显示在屏幕底部。 当新方块无法放置在网格顶部时,游戏结束,并在控制台打印“游戏结束”。 你可以截取运行中的游戏画面以查看视觉效果。 七、心得体会:对博客园有了一个初步认识,并且简单了解了一下部分小游戏的代码,并且体验了一把开发游戏权限的部分乐趣。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/889767.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

SemanticKernel之Chat

去年写过几过几篇关于SemanticKernel的文章,由于正式发布的版本与之前的版本变化较大,加上前的东京《生成式AI应用开发》活动,想把演示的Demo逐一分享出来,的以再次开启SemanticKernel系统。下面是一个Chat的例子,用户提问,如果本地有固定数据能对应,直接返回,如果没有…

7、添加特效

去除画面logo 复制一份 拖动模糊特效到复制的片段中 右键分离音频或者快捷键【ctrl+shift+s】 删除音频 添加原创特效,特效随机 有音乐的去除音乐 适当拖动放大 调整透明度 开幕特效 三秒 调整参数后,复制双份 画面特效和人物特效二选一 画面特效需要修改参数

Prompt升级

前两篇关于Prompt的文章分别从提示词规则建议和具体框架角度说明了一下Prompt的使用技巧,接下来可以说是对框架式提示词的的进一步升级——结构化提示词。 结构化定义: 对信息进行组织,使其遵循特定的模式和规则,从而方便有效理解信息。 结构化提示词语法:这个结构支持 Mark…

Spring复习-事务

事务概述 Spring事务编程概述 事务是开发中必不可少的东西,使用JDBC开发时,我们使用connnection对事务进行控制,使用MvBatis时,我们使用SqlSession对事务进行控制,缺点显而易见,当我们切换数据库访问技术时,事务控制的方式总会变化,Spring 就将这些技术基础上,提供了统…

Prompt进阶

在Prompt入门里,我们分享了OpenAI官方给出的提示词建议,但这些建议基本上是指导性的,方向性的,概念性的,虽然对我们编写提示词有很大帮助,但究竟我们的提示词好还是不好,效率怎么性,很大程度上要看每个人的理解,和提示词输出。那么有没有一个可操作性的,方法论的,谁…

P1174 打砖块

链接 https://www.luogu.com.cn/problem/P1174 思路刚开始的思路:设置dp[i][j]:前i列使用了j颗子弹,那么递推dpi,j=max(dpi,j,dpi-1,k+maxj-k),然后统计在第i列使用了j-k颗子弹会多出来多少颗,把这些遍历加到前面,见代码。喜提70pts。但是搞不懂哪里错了。 看了评论区的dp:70…

redis - [06] redis-benchmark性能测试

题记部分 001 || 参数含义 002 || 测试100个并发,100000个请求 启动redis-server redis-server /etc/redis.conf 进行性能测试 redis-benchmark -h localhost -p 6379 -c 100 -n 100000

百万架构师第四十三课:Nginx:Nginx 应用实战|JavaGuide

百万架构师系列文章阅读体验感更佳 原文链接:https://javaguide.net 公众号:不止极客 课程目标:Nginx 反向代理功能配置 Nginx 负载均衡实战 Nginx 动静分离配置 Nginx 配置文件分析 Nginx 多进程模型原理 Nginx 高可用集群实战反向代理​ 我们把请求发送到 proxy (代理服务…

大三下每日打卡003

今天配置了python的虚拟环境anaconda想尝试一下yolov8来实现识别

需求评审

需求评审是产品经理日常会议的形式之一,也是一个“公开处刑”的时刻。这篇文章,我们看看作者分享的如何做好一次需求评审的经验,供大家参考。前段时间有小伙伴留言,想聊一下关于需求评审面向不同角色如何处理,以及产品不同生命周期产品工作上有什么区别。我结合自己工作经…

牛客题解 | 对称的二叉树_1

牛客题库题解题目 题目链接 题目的主要信息:判断一棵二叉树是否是镜像,即判断二叉树是否是轴对称图形轴对称:非轴对称:举一反三: 学习完本题的思路你可以解决如下题目: BM28. 二叉树的最大深度 BM29. 二叉树中和为某一值的路径(一) BM32. 合并二叉树 BM33. 二叉树的镜像…

牛客题解 | 字符串变形

牛客题库题解题目 题目链接 题目主要信息:将字符串大小写反转 将整个字符串的所有单词位置反转举一反三: 学会了本题的思路,你将可以解决类似的字符串问题: BM84. 最长公共前缀 BM85. 验证IP地址 方法一:双逆转(推荐使用) 思路: 将单词位置的反转,那肯定前后都是逆序,…