14
2024
11

python编程学习-woblox

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

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

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

from xdf import *

W = 768
H = 1024
CW = W // 6
m = 6
n = 6
ox = 0
oy = (H - n * CW) // 2
def xy2ij(x, y):
    return [(x-ox)//CW,(y-oy)//CW]
def ij2xy(i, j):
    return [ox+i*CW,oy+j*CW]
blox = [
    "blox-t@005z",
    "blox-w0@0064",
    "blox-w1@0065",
    "blox-w2@0066",
    "blox-w3@0067",
    "blox-w4@0068"
]
bloxSize = [
    [1, 1],
    [2, 1],
    [2, 1],
    [3, 1],
    [1, 2],
    [1, 3]
]
maps = [
    [0,5,2,1,0,2],
    [0,5,2,1,0,2,2,0,1,3,1,5,5,0,3,5,3,1],
    [0,5,2,1,1,2,2,0,0,3,3,5,4,4,2,5,0,1,2,4,4,5,3,0,5,5,0],
    [0,5,2,1,0,2,2,0,5,3,3,0,4,1,0,5,3,2,4,2,0,4,2,2,4,0,3,4,5,2,4,5,4,3,3,1],
    [0,5,2,1,0,2,2,2,0,4,1,0,5,3,1,4,2,1,4,4,0,4,5,0,4,5,4,2,4,3,2,3,4,2,1,3,2,2,5],
    [0,5,2,1,2,2,2,4,0,3,0,1,4,0,2,5,5,2,4,3,0,4,4,1,2,1,3,2,3,3,2,0,4,2,4,5],
    [0,5,2,1,0,2,2,2,0,3,3,1,4,1,0,5,2,1,4,3,2,4,0,4,4,5,4,2,4,0,2,4,3,3,2,5],
    [0,5,2,1,2,2,2,3,0,3,0,5,4,0,0,5,1,0,2,0,3,2,3,4,4,2,3,5,4,1],
    [0,5,2,1,2,2,2,1,0,3,3,4,4,1,2,2,1,1,2,4,0,2,0,5,4,3,0,4,4,1,4,2,4],
    [0,5,2,1,0,2,3,0,0,4,3,0,5,2,1,4,4,0,4,5,4,3,3,3,3,2,4,3,2,5,5,1,3],
    [0,5,2,1,3,2,2,0,1,4,0,3,2,2,1,2,4,4,2,4,5,4,2,2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2,2,2,1,3,1,0,4,0,0,5,3,2,2,0,3,2,4,3,4,4,0,4,2,2,3,0,4],
    [0,5,2,1,0,2,2,4,1,3,1,4,4,0,3,5,3,0,4,2,1,4,4,2,4,4,4,5,5,2],
    [0,5,2,1,3,2,2,1,0,3,0,3,4,1,1,5,0,0,2,4,4,4,2,1,4,4,0,4,5,0,4,5,2,4,3,3,4,2,4],
    [0,5,2,1,0,2,2,3,3,3,3,0,4,2,0,5,5,2,2,2,4,2,1,5,2,3,5,4,3,1,4,2,2,4,1,3],
    [0,5,2,1,0,2,2,0,5,3,1,0,4,0,3,5,5,0,2,4,4,4,2,1,4,3,1,4,4,0,4,4,2,4,3,4,3,1,3],
    [0,5,2,1,0,2,2,4,0,3,1,4,4,0,4,2,4,1,2,0,3,4,4,4,4,5,2,4,4,2,4,3,2,4,3,0,4,2,1],
    [0,5,2,1,2,2,2,1,3,3,2,0,4,0,0,5,1,0,2,0,5,2,3,5,4,0,2,4,5,0,4,5,2,4,2,4,5,4,1],
    [0,5,2,1,1,2,2,4,1,3,3,0,4,2,0,5,0,0,2,2,4,2,2,5,2,0,5,4,3,1,4,1,3,5,4,3,5,5,2],
    [0,5,2,1,1,2,2,1,1,3,0,0,4,0,1,5,4,0,2,2,5,2,4,5,4,3,0,4,5,0,4,2,3,4,1,4,2,0,3]
]
level = 0
mp = maps[level]
pool = []
arr = []
curDrag = [None, 0, 0, 0, 0, 0]
isWin = False
tarWd = None
heroWd = None
winTf = text("过关了,点击进入下一关", 50, "red", "center")
fill("blox-bg@0069")
levelTf = text("第1关", 10, 70, 50, "red")
def getWood(tp):
    for wd in pool:
        if wd[0]==tp:
            pool.remove(wd)
            return wd
    return [tp, stamp(blox[tp]), 0, 0, 0, 0]

def addWood(tp, i, j):
    wd = getWood(tp)
    sz = bloxSize[tp]
    xy = ij2xy(i + sz[0]/2, j + sz[1]/2)
    wd[2] = i
    wd[3] = j
    wd[4] = xy[0]
    wd[5] = xy[1]
    wd[1].move(xy[0], xy[1])
    wd[1].show()
    arr.append(wd)
    if tp == 0:
        global tarWd
        tarWd = wd
    elif tp == 1:
        global heroWd
        heroWd = wd
    pass
def clearWood():
    for wd in arr:
        wd[1].hide()
    pool.extend(arr)
    arr.clear()
    pass

def reStart():
    global mp, isWin
    winTf.hide()
    isWin = False
    mp = maps[level]
    clearWood()
    curDrag[0] = None
    levelTf.change("第"+str(level+1)+"关")
    for i in range(0, len(mp), 3):
        addWood(mp[i], mp[i+1], mp[i+2])
    pass

def nextLevel():
    global level
    if level < len(maps) - 1:
        level = level + 1
    reStart()
    pass

def getWoodUnder():
    for wd in arr:
        tp = wd[0]
        if tp == 0:
            continue
        sz = bloxSize[tp]
        ij = xy2ij(x, y)
        if ij[0] >= wd[2] and ij[0] < wd[2] + sz[0] and ij[1] >= wd[3] and ij[1] < wd[3] + sz[1]:
            return wd
    return None
def touch():
    if isWin:
        return
    global curDrag
    wd = getWoodUnder()
    if wd != None:
        curDrag = [wd, x, y, 0, 0, 0]
        sz = bloxSize[wd[0]]
        curDrag[5] = 1 if sz[0] > sz[1] else 2
    pass
def untouch():
    global curDrag
    if curDrag[0] != None:
        putWood(curDrag[0])
        checkWin()
    curDrag[0] = None
    pass
def touching():
    global curDrag
    if curDrag[0] != None:
        curDrag[3] = x - curDrag[1]
        curDrag[4] = y - curDrag[2]
        curDrag[1] = x
        curDrag[2] = y
        wd = curDrag[0]
        if curDrag[5] == 1:
            wd[4] = wd[4] + curDrag[3]
            if canMove(wd):
                wd[1].move(wd[4], wd[5])
            else:
                wd[4] = wd[4] - curDrag[3]
        elif curDrag[5] == 2:
            wd[5] = wd[5] + curDrag[4]
            if canMove(wd):
                wd[1].move(wd[4], wd[5])
            else:
                wd[5] = wd[5] - curDrag[4]
            pass
        pass
    pass
def loop():
    pass
def tap():
    if isWin:
        nextLevel()
    pass
def checkWin():
    global heroWd
    global tarWd
    global isWin
    if checkHit(heroWd, tarWd):
        isWin = True
        winTf.show()
        winTf.front()
    pass
def putWood(wd):
    tp = wd[0]
    sz = bloxSize[tp]
    ij = xy2ij(wd[4] - sz[0] * CW / 2 + CW / 2, wd[5] - sz[1] * CW / 2 + CW / 2)
    xy = ij2xy(ij[0] + sz[0]/2, ij[1] + sz[1]/2)
    wd[2] = ij[0]
    wd[3] = ij[1]
    wd[4] = xy[0]
    wd[5] = xy[1]
    wd[1].move(xy[0], xy[1], 100)
    pass
def canMove(wd):
    rect = getRect(wd, wd[4], wd[5])
    if rect[0] - rect[2] < ox or rect[0] + rect[2] > ox + CW * m or rect[1] - rect[3] < oy or rect[1] + rect[3] > oy + CW * n:
        return False
    for wd0 in arr:
        if wd0[0] == 0 or wd0 == wd:
            continue
        rect0 = getRect(wd0, wd0[4], wd0[5])
        if abs(rect[0] - rect0[0]) < rect[2] + rect0[2] and abs(rect[1] - rect0[1]) < rect[3] + rect0[3]:
            return False
    return True
def checkHit(wd0, wd1):
    rect0 = getRect(wd0, wd0[4], wd0[5])
    rect1 = getRect(wd1, wd1[4], wd1[5])
    return abs(rect0[0] - rect1[0]) < rect0[2] + rect1[2] and abs(rect0[1] - rect1[1]) < rect0[3] + rect1[3]
def getRect(wd, x0, y0):
    tp = wd[0]
    sz = bloxSize[tp]
    return [x0, y0, sz[0] * CW / 2, sz[1] * CW / 2]

reStart()


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



« 上一篇下一篇 »

相关文章:

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

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

python编程学习-别踩白块  (2024-11-14 16:38:59)

python编程学习-俄罗斯方块  (2024-11-14 16:38:19)

python编程学习-井字棋  (2024-11-14 16:34:9)

python编程学习-五子棋  (2024-11-14 16:33:23)

python编程学习-乒乓  (2024-11-14 16:32:46)

python编程学习-一笔画关卡生成  (2024-11-14 16:32:6)

python编程学习-一笔画  (2024-11-14 16:31:27)

python编程学习-where-my-ice-cream  (2024-11-14 16:29:57)

发表评论:

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