sounds.bg_music.play(-1)
- def swap_Square(i,j): # 两个拼图的位置互换
- sounds.chick.play()
- # 略
Win_music=0
- if is_win:
- if Win_music==0:
- sounds.win_music.play()
- Win_music=1
- Is_Win=True
- try:
- txtFile=open("rank.txt",'r')
- score=txtFile.readline()
- except:
- txtFile=open("rank.txt",'w')
- score = "您是第一个玩家"
- txtFile.write(score)
- txtFile.close()
import datetime
- startTime=datetime.datetime.now()
- oldTime=int(score) if score.isdigit() else 9999
- newTime=0
- def update():
- global newTime,startTime
- if not Is_Win:
- endTime = datetime.datetime.now()
- newTime=(endTime-startTime).seconds
- # 略
- def on_mouse_down(pos,button): # 当鼠标被点击时
- # 略
- if is_win:
- # 略
- if newTime
- txtFile=open("rank.txt",'w')
- txtFile.write(str(newTime))
- txtFile.close()
- 将历史最好成绩级计时器显示在窗口上
- def draw():
- # 略
- screen.draw.text("游戏最佳记录: "+str(oldTime), (20, 10), fontsize=20,\
- fontname='s', color="blue")
- screen.draw.text("游戏运行时间: " + str(newTime), (WIDTH/2, 10), \
- fontsize=20, fontname='s', color="blue")
执行效果如下图所示:

完整代码如下:
- import pgzrun
- import random
- import datetime
-
- try:
- 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=0
-
- TITLE="pgzrun 拼图游戏"
- Square_size=125
- WIDTH=Square_size*4
- HEIGHT=Square_size*6
-
- click_time=0
- clickID_1=clickID_2=-1
- Is_Win=False
- Win_music=0
-
- sounds.bg_music.play(-1)
-
- Squares=[Actor("girl_01"),Actor("girl_02"),Actor("girl_03"),Actor("girl_04")
- ,Actor("girl_05"),Actor("girl_06"),Actor("girl_07"),Actor("girl_08")
- ,Actor("girl_09"),Actor("girl_10"),Actor("girl_11"),Actor("girl_12")
- ,Actor("girl_13"),Actor("girl_14"),Actor("girl_15"),Actor("girl_16")
- ,Actor("girl_17"),Actor("girl_18"),Actor("girl_19"),Actor("girl_20")
- ,Actor("girl_21"),Actor("girl_22"),Actor("girl_23"),Actor("girl_24")
- ]
-
- Gird=[]
- for i in range(6):
- for j in range(4):
- Square=Squares[i*4+j]
- Square.left=Square_size*j
- Square.top=Square_size*i
- Gird.append(Square)
-
- def swap_Square(i,j): # 两个拼图的位置互换
- sounds.chick.play()
- temp_pos=Gird[i].pos
- Gird[i].pos=Gird[j].pos
- Gird[j].pos=temp_pos
-
- for k in range(10): # 随机抽取10组拼图 进行位置互换
- i=random.randint(0,23)
- j=random.randint(0,23)
- swap_Square(i, j)
-
- def on_mouse_down(pos,button): # 当鼠标被点击时
- global click_time ,clickID_1 , clickID_2,Is_Win,Win_music
- for i in range(24):
- if Gird[i].collidepoint(pos): # 拼图对象被点击
- break
- if click_time%2==0 :
- clickID_1=i
- else:
- clickID_2=i
- swap_Square(clickID_1,clickID_2)
- click_time += 1
- # 成功判断
- is_win = True
- for 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 = False
- break
- if is_win:
- if Win_music==0:
- sounds.win_music.play()
- Win_music=1
- Is_Win=True
- if newTime
- 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), (20, 10), fontsize=20, fontname='s', color="blue")
- screen.draw.text("游戏运行时间: " + str(newTime), (WIDTH/2, 10), fontsize=20, fontname='s', color="blue")
- if Is_Win:
- screen.draw.text("游戏胜利!",(WIDTH/2-100,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
- if not Is_Win:
- endTime = datetime.datetime.now()
- newTime=(endTime-startTime).seconds
-
- pgzrun.go()
-
相关阅读:
常用API
【10】Spring源码-分析篇-AOP源码分析
Python数据结构:列表(list)、元组(tuple)、字典(dict)
ArduPilot开源代码之AP_RangeFinder
微信小程序 vue+uniapp书架小说阅读推荐系统
2023USNews美国加州系大学排名
starrocks新建clickhouse(jdbc)外部表 查询报错 JDBC result type is [java.math.BigInteger]
python jieba分词,一次性添加多个词
Leetcode 219. Contains Duplicate II (hashmap 和 sliding window)
MediaPlayer_Analyze-1-framework
-
原文地址:https://blog.csdn.net/lybc2019/article/details/132830975