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_ID
Squares.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_music
if key == keys.SPACE:
Photo_ID += 1
if Photo_ID < len(Photos):
Is_Win = False
Win_music = 0
change_Photo()
- 修改时间的更新条件
def update():
global newTime,startTime,Photo_ID
if (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 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)
Photos=["girl_","boy_","cat_"]
Photo_ID=0
Squares=[]
Gird=[]
def swap_Square(i,j): # 两个拼图的位置互换
sounds.chick.play()
temp_pos=Gird[i].pos
Gird[i].pos=Gird[j].pos
Gird[j].pos=temp_pos
def change_Photo():
global Photo_ID
Squares.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*j
Square.top=Square_size*i
Gird.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_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<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_ID
if (not Is_Win) or (Photo_ID!=len(Photos)):
endTime = datetime.datetime.now()
newTime=(endTime-startTime).seconds
def on_key_down(key):
global Photo_ID,Is_Win,Win_music
if key == keys.SPACE:
Photo_ID += 1
if Photo_ID < len(Photos):
Is_Win = False
Win_music = 0
change_Photo()
pgzrun.go()
pgzrun拼图游戏素材包下载