代码拉取完成,页面将自动刷新
import pgzrun
from pgzero.actor import Actor
from pgzero.screen import Screen
from pgzero.loaders import sounds
import random
import sys
from time import time
# 类型标注
screen:Screen
SIZE = 200;
WIDTH = SIZE*3;
HEIGHT = SIZE*3;
pul_actors = [];
# 游戏结束标志位
finished = False;
# 完成拼图,填充最后一张图片
end_pul = Actor('2022hu_8');
end_pul.left = 2 * SIZE;
end_pul.top = 2 * SIZE;
# 添加时间概念
start = time();
end = None;
for i in range(8):
pl = Actor("2022hu_"+str(i)+".png");
# 增加索引标识位置,gameOver判定
pl.index = i;
pul_actors.append(pl);
# 洗牌
random.shuffle(pul_actors);
for i in range(len(pul_actors)):
# 巧妙的获取列数和行数
pul_actors[i].left = i % 3 * SIZE;
pul_actors[i].top = i // 3 * SIZE;
def draw():
screen.fill((255,255,255));
for pul in pul_actors:
pul.draw();
# 拼图完成
if finished:
end_pul.draw();
screen.draw.text("拼图完成",center=(WIDTH//2,HEIGHT//2),fontsize=50,color="green",fontname="simhei")
screen.draw.text(f"完成时间{round(end,2)}秒",center=(WIDTH//2,HEIGHT//2+100),fontsize=40,color="green",fontname="simhei")
else :
screen.draw.text(f'时间:{round(time()-start)}s',center=(WIDTH//2,20),fontsize=40,color="green",fontname="simhei");
# 抽离函数
def get_pul(grid_x, grid_y):
for pul in pul_actors:
# 当前位置和所有的图片位置对比
if grid_x == pul.x // SIZE and grid_y == pul.y // SIZE:
return pul;
return None;
def gameFinished():
global finished;
global end;
for i in range(8):
pul = get_pul(i%3,i//3);
if pul == None or pul.index != i:
return;
finished = True;
sounds.gameover.play();
end = (time() - start);
# 事件处理
def on_mouse_down(pos):
# 游戏结束了就不能再移动了
if not finished:
# 行和列
grid_x = pos[0] // SIZE;
grid_y = pos[1] // SIZE;
# 当前是哪一张图片,
thispul = get_pul(grid_x, grid_y);
# 图片四个方向,上有空位
if grid_y > 0 and get_pul(grid_x, grid_y - 1) == None:
thispul.y -= SIZE;
return;
elif grid_y < 2 and get_pul(grid_x, grid_y+1) == None:
thispul.y += SIZE;
return;
elif grid_x > 0 and get_pul(grid_x-1, grid_y) == None:
thispul.x -= SIZE;
return;
elif grid_x < 2 and get_pul(grid_x+1, grid_y) == None:
thispul.x += SIZE;
return;
def update():
if not finished:
gameFinished();
pgzrun.go();
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。