import pgzrun
import random
import datetime
txtFile = open('rank.txt', 'r')
line = txtFile.readline()
oldTime = int(line)
txtFile.close()
start = datetime.datetime.now()
newTime = 0
TILE_SIZE = 100
WIDTH = 3*TILE_SIZE
HEIGHT = 3*TILE_SIZE +60
clickTime = 0
clickId1 = clickId2 = -1
allRight = False
tiles = [Actor('puzzle_pic0'), Actor('puzzle_pic1'), Actor('puzzle_pic2'),
Actor('puzzle_pic3'), Actor('puzzle_pic4'),Actor('puzzle_pic5'),
Actor('puzzle_pic6'),Actor('puzzle_pic7'),Actor('puzzle_pic8')]
grid = []
for i in range(3):
for j in range(3):
tile = tiles[i*3+j]
tile.left = j * TILE_SIZE
tile.top = i * TILE_SIZE
grid.append(tile)
def swapPosition(i, j):
tempPos = grid[i].pos
grid[i].pos = grid[j].pos
grid[j].pos = tempPos
for k in range(10):
i = random.randint(0, 8)
j = random.randint(0, 8)
swapPosition(i, j)
def draw():
screen.clear()
for tile in grid:
tile.draw()
screen.draw.text("Best record:"+str(oldTime)+'seconds', (60, 300), fontsize=25,
color='red')
screen.draw.text("Game running:"+str(newTime)+'seconds', (60, 330), fontsize=25,
color='red')
if allRight:
screen.draw.text("Game Wins", (40, HEIGHT/2-50),
fontsize=50, color='blue')
else:
for i in range(3):
screen.draw.line((0, i*TILE_SIZE), (WIDTH, i*TILE_SIZE), 'white')
screen.draw.line((i*TILE_SIZE, 0), (i*TILE_SIZE, 3*TILE_SIZE), 'white')
if clickId1 != -1:
screen.draw.rect(
Rect((grid[clickId1].left, grid[clickId1].top), (TILE_SIZE, TILE_SIZE)), 'red')
def update():
global newTime
if not allRight:
end = datetime.datetime.now()
newTime = (end - start).seconds
def on_mouse_down(pos, button):
global clickTime, clickId1, clickId2, allRight
for k in range(9):
if grid[k].collidepoint(pos):
break
if clickTime % 2 == 0:
clickId1 = k
clickTime += 1
elif clickTime % 2 == 1:
clickId2 = k
clickTime += 1
swapPosition(clickId1, clickId2)
allRight = True
for i in range(3):
for j in range(3):
tile = grid[i*3+j]
if tile.left != j * TILE_SIZE or tile.top != i * TILE_SIZE:
allRight = False
break
if allRight:
if newTime < oldTime:
txtFile = open('rank.txt', 'w')
txtFile.write(str(newTime))
txtFile.close()
pgzrun.go()
python拼图游戏_项目源码与素材,python少儿编程,经典教学作品,儿童益智游戏
版权申诉
5星 · 超过95%的资源 8 浏览量
2022-04-20
22:33:15
上传
评论
收藏 15KB ZIP 举报
向日葵少儿编程
- 粉丝: 9
- 资源: 500
最新资源
- 装修通用报价参考,基础施工项目+水电工程项目+瓦木项目,超级详细
- 三菱PLC例程源码Medocsequencegenerator
- 三菱PLC例程源码M1320磨头进出FX1s控制步进电机,有注释
- STRASSEN矩阵乘法算法(改进分治法·C语言)
- 前端.xmind前端.xmind前端.xmind前端.xmind前端.xmind
- 三菱PLC例程源码LOW-E玻璃镀膜线程序(三菱QPLC的)一万步带注释
- 三菱PLC例程源码LCD设备蚀刻机程序
- 三菱PLC例程源码LCD设备蚀刻机
- 全面前端开发指南:从基础到深入
- pvk2pfx 32位 Pvk2Pfx (Pvk2Pfx.exe) 是一种命令行工具,可将 .spc、.cer 和 .pvk 文
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈