15
2024
11

python编程学习-拼图

学习python编程,在新东方少儿编程官网的自由创作平台做的一些作品。

可以在线运行,效果如下(在新页面中打开链接):

代码如下(用到平台自定义的xdf库):

from xdf import *

arr = [1,2,3,4,5,6,7,8,9,10,11,12]
w = 256
isPlaying = True
cubeArr = []
index12 = 11
for i in range(12):
    cube = box(1, 1, w-10, w-10, "#ccc")
    tf = text(str(i+1), w / 2, w, w/2, "red", "center")
    cubeArr.append((cube, tf))
txt = text("恭喜,拼图完成", 768/2, 1024/2, 100, "red", "center")
txt2 = text("按空格键开始", 768/2, 1024/2 + 120, 100, "red", "center")

def updateCube():
    for i in range(12):
        ind = arr[i] - 1
        m = i // 3
        n = i % 3
        cubeArr[ind][0].move(w * (n+0.5), w * (m+0.5))
        cubeArr[ind][1].move(w * (n+0.5), w * (m+0.75))

def swap(i,j):
    t=arr[i]
    arr[i]=arr[j]
    arr[j]=t

def upsetArr():
    for i in range(10): #交换偶数次,能保证逆序数是偶数
        ii = random(0, 10)
        while ii==i:
            ii = random(0,10)
        swap(i,ii);
    #print(getInverseNum()) #逆序数,逆序数是偶数,拼图才有解

def reset():
    upsetArr()
    index12 = 11
    txt.hide()
    txt2.hide()
    updateCube()
    isPlaying = True
    cubeArr[11][0].hide()
    cubeArr[11][1].hide()

def getInverseNum():
    inverseNum=0;
    for i in range(11):
        c=arr[i]
        for j in range(11-1-i):
            if c>arr[j+i+1]:
                inverseNum+=1
    return inverseNum

def isWin():
    for i in range(11):
        if arr[i] > arr[i+1]:
            return False
    return True

def keydown(keyCode, key):
    global isPlaying
    if isPlaying:
        if keyCode == 37:
            moveLeft()
            checkWin()
        elif keyCode == 38:
            moveUp()
            checkWin()
        elif keyCode == 39:
            moveRight()
            checkWin()
        elif keyCode == 40:
            moveDown()
            checkWin()
    else:
        if keyCode == 32:
            isPlaying = True
            reset()

def loop():
    pass

def checkWin():
    global isPlaying
    if isWin():
        isPlaying = False
        txt.show()
        txt2.show()

def moveLeft():
    global index12
    n = index12 % 3
    targetN = n + 1
    if targetN < 3:
        swap(index12, index12 + 1)
        index12 = index12 + 1
        updateCube()

def moveRight():
    global index12
    n = index12 % 3
    targetN = n - 1
    if targetN >= 0:
        swap(index12, index12 - 1)
        index12 = index12 - 1
        updateCube()

def moveUp():
    global index12
    m = index12 // 3
    targetM = m + 1
    if targetM < 4:
        swap(index12, index12 + 3)
        index12 = index12 + 3
        updateCube()

def moveDown():
    global index12
    m = index12 // 3
    targetM = m - 1
    if targetM >= 0:
        swap(index12, index12 - 3)
        index12 = index12 - 3
        updateCube()

reset()



也可以下载代码,本地python环境运行(用pygame封装了xdf库)。



« 上一篇下一篇 »

相关文章:

python编程学习-推箱子  (2024-11-15 11:34:1)

python编程学习-推箱子  (2024-11-15 11:34:1)

python编程学习-扫雷  (2024-11-15 11:32:58)

python编程学习-小学数学加减分解题  (2024-11-15 11:32:29)

python编程学习-对角棋  (2024-11-15 11:32:8)

python编程学习-密室逃生  (2024-11-15 11:31:35)

python编程学习-围住神经猫  (2024-11-15 11:31:6)

python编程学习-四子棋  (2024-11-15 11:30:32)

python编程学习-华容道  (2024-11-14 16:43:25)

python编程学习-动画  (2024-11-14 16:39:37)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。