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

pgzrun 拼图游戏制作过程详解(10)

10. 拼图游戏继续升级——多关卡拼图

  • 初始化列表Photos用来储存拼图文件名,Photo_ID用来统计当下是第几张拼图,Squares储存当下拼图的24张小拼图的文件名,Gird储存当下窗口上显示的24个小拼图及坐标。
Photos=["girl_","boy_","cat_"]
Photo_ID=0
Squares=[]
Gird=[]
  • 建立change_Photo()函数通过Photo_ID来初始化新一轮的拼图
def change_Photo():global Photo_IDSquares.clear()Gird.clear()for i in range(1,25):# 初始化最新图片的文件名if i<10:s=Photos[Photo_ID]+'0'+str(i)else:s=Photos[Photo_ID]+str(i)Squares.append(Actor(s))# 略 Squares、Gird的初始化
  • 设定图片切换方法:上一张拼图胜利后按下空格键切换下一张拼图,再次将Is_Win设定为False
  • 修改游戏胜利条件:Photo_ID等于Photos的长度且Is_Win为True时才能迎来最终的胜利
def on_key_down(key):global Photo_ID,Is_Win,Win_musicif key == keys.SPACE:Photo_ID += 1if Photo_ID < len(Photos):Is_Win = FalseWin_music = 0change_Photo()
  • 修改时间的更新条件
def update():global newTime,startTime,Photo_IDif (not Is_Win) or (Photo_ID!=len(Photos)):endTime = datetime.datetime.now()newTime=(endTime-startTime).seconds
  • 再窗口上增加当下是第几张拼图的提示
def draw():# 略screen.draw.text("第" + str(Photo_ID+1)+"张图", (WIDTH-100, 10),\fontsize=20, fontname='s', color="blue")
  • 当最后一张拼图完成增加提示
def draw():# 略if Is_Win:# 略if Photo_ID == len(Photos) :screen.draw.text("已是最后一张图了!", (WIDTH / 2 - 170, HEIGHT / 2 + 50), \fontsize=50, fontname='s', color="blue")

执行效果如下图所示:

完整代码如下:

import pgzrun
import random
import time
import datetimetry:txtFile=open("rank.txt",'r')score=txtFile.readline()
except:txtFile=open("rank.txt",'w')score = "您是第一个玩家"txtFile.write(score)
txtFile.close()startTime=datetime.datetime.now()
oldTime=int(score) if score.isdigit() else 9999
newTime=0TITLE="pgzrun 拼图游戏"
Square_size=125
WIDTH=Square_size*4
HEIGHT=Square_size*6click_time=0
clickID_1=clickID_2=-1
Is_Win=False
Win_music=0sounds.bg_music.play(-1)Photos=["girl_","boy_","cat_"]
Photo_ID=0
Squares=[]
Gird=[]def swap_Square(i,j):  # 两个拼图的位置互换sounds.chick.play()temp_pos=Gird[i].posGird[i].pos=Gird[j].posGird[j].pos=temp_posdef change_Photo():global Photo_IDSquares.clear()Gird.clear()for i in range(1,25):if i<10:s=Photos[Photo_ID]+'0'+str(i)else:s=Photos[Photo_ID]+str(i)Squares.append(Actor(s))for i in range(6):for j in range(4):Square=Squares[i*4+j]Square.left=Square_size*jSquare.top=Square_size*iGird.append(Square)for k in range(10):  # 随机抽取10组拼图 进行位置互换i = random.randint(0, 23)j = random.randint(0, 23)swap_Square(i, j)change_Photo()def on_mouse_down(pos,button): # 当鼠标被点击时global click_time ,clickID_1 , clickID_2,Is_Win,Win_musicfor i in range(24):if Gird[i].collidepoint(pos): # 拼图对象被点击breakif click_time%2==0 :clickID_1=ielse:clickID_2=iswap_Square(clickID_1,clickID_2)click_time += 1# 成功判断is_win = Truefor i in range(6):for j in range(4):Square = Squares[i * 4 + j]if not (Square.left == Square_size * j and Square.top == Square_size * i) :is_win = Falsebreakif is_win:if Win_music==0:sounds.win_music.play()Win_music=1Is_Win=Trueif newTime<oldTime:txtFile=open("rank.txt",'w')txtFile.write(str(newTime))txtFile.close()def draw():screen.clear()for Square in Gird:Square.draw()screen.draw.text("游戏最佳记录: "+str(oldTime), (10, 10), fontsize=20, fontname='s', color="blue")screen.draw.text("第" + str(Photo_ID+1)+"张图", (WIDTH-100, 10), fontsize=20, fontname='s', color="blue")screen.draw.text("游戏运行时间: " + str(newTime), (10, 30), fontsize=20, fontname='s', color="blue")if Is_Win:screen.draw.text("游戏胜利!",(WIDTH/2-100,HEIGHT/2-50),fontsize=50,fontname='s',color="blue")if Photo_ID == len(Photos) :screen.draw.text("已是最后一张图了!", (WIDTH / 2 - 170, HEIGHT / 2 + 50), fontsize=50, fontname='s', color="blue")else :for i in range(5):screen.draw.line((i*Square_size,0),(i*Square_size,HEIGHT),"black")for i in range(7):screen.draw.line((0,i*Square_size),(WIDTH,i*Square_size),"black")if clickID_1!=-1:screen.draw.rect(Rect((Gird[clickID_1].left,Gird[clickID_1].top),(Square_size,Square_size)),"red")def update():global newTime,startTime,Photo_IDif (not Is_Win) or (Photo_ID!=len(Photos)):endTime = datetime.datetime.now()newTime=(endTime-startTime).secondsdef on_key_down(key):global Photo_ID,Is_Win,Win_musicif key == keys.SPACE:Photo_ID += 1if Photo_ID < len(Photos):Is_Win = FalseWin_music = 0change_Photo()pgzrun.go()

pgzrun拼图游戏素材包下载

http://www.lryc.cn/news/170766.html

相关文章:

  • glog与pugi::xml使用方法
  • windows下MySQL服务不见,服务无法启动,服务闪退,提示“本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止”
  • 剑指offer(C++)-JZ67:把字符串转换成整数atoi(算法-模拟)
  • 嵌入式笔试面试刷题(day15)
  • 【Docker】Dockerfile构建镜像
  • fota升级,可卸载apk也进行更新
  • ASP.NET dotnet 3.5 实验室信息管理系统LIMS源码
  • 2023!6招玩转 Appium 自动化测试
  • WireShark抓包分析TCP三次握手过程,TCP报文解析
  • 【C语言】指针和数组笔试题解析
  • Vue的模板语法(下)
  • Zookeeper客户端——I0Itec-zkClient
  • 火山引擎 ByteHouse:ClickHouse 如何保证海量数据一致性
  • hashmap使用
  • Centos7配置国内yum源
  • C#中async/await的线程ID变化情况
  • 网络安全—黑客技术—自学笔记
  • 功夫再高也怕菜刀。多年经验,会独立开发的机器视觉工程师,技术太强,但是找工作能力差劲
  • numpy的多项式函数: `poly1d`
  • Python灰帽编程——错误异常处理和面向对象
  • 【20230919】win11无法删除Chrome注册表项
  • TCP/IP客户端和服务器端建立通信过程
  • Python ---使用Fake库向clickhouse造数据小案例
  • 09MyBatisX插件
  • 使用 Messenger 跨进程通信
  • Spring Cloud Gateway
  • JVM 优化技术
  • 【MySQL系列】- MySQL自动备份详解
  • 指针笔试题讲解-----让指针简单易懂(2)
  • 使用windbg分析dump文件的方法