14
2024
11

python编程学习-Dark-Light-Swap

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

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

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

from xdf import *
import time
setFrameRate(10)
W = 768
H = 1024
CW = 64
m = 11
n = 16
ox = (W - m * CW) // 2
oy = (H - n * CW) // 2
def xy2ij(x, y):
    return [(x-ox)//CW,(y-oy)//CW]
def ij2xy(i, j):
    return [ox+i*CW+CW/2,oy+j*CW+CW/2]

names = [
    #
    "DLS-hro-b1@006I",
    "DLS-hro-b2@006J",
    "DLS-hro-w1@006K",
    "DLS-hro-w2@006L",
    #
    "DLS-cub-b@006E",
    "DLS-cub-w@006H",
    "DLS-cub-g@006G",
    "DLS-cub-u@006F",
    "DLS-tar@006X",
    "DLS-swt@006f",
    #
    "DLS-key-b@006M",
    "DLS-key-w@006N",
    "DLS-lok-b@006O",
    "DLS-lok-w@006P"
]
maps = [
  [45,146,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
  [145,29,0,0,0,1,1,1,1,1,1,1,1,0,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0,1,0,1,1,1,1,1,1,0,1,1,1,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,1,1,0,1,0,0,0,0,0,0,0,0,1,0,1,0,4,1,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0,1,0,1,0,1,1,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,3,0,3,0,1,0,1,3,1,3,1,1,1]
]

level = 0
data = []
data2 = []
pool = []
arr = []
hero1 = None
hero2 = None
tf = text("过关了,点击进入下一关", 50, "red", "center")
levelTf = text("第1关", 0, 60, 50, "red")
restartTf = text("重新开始", W, 60, 50, "red", "right")
swapTf = text("切换", 0, H-10, 50, "red")
isSuccess = False
isSleep = False
class Cube:
    def __init__(self, name, name2 = None):
        self.name = name
        self.sp = stamp(name)
        self.sp2 = None
        if name2 != None:
            self.sp2 = stamp(name2)
        self.i = 0
        self.j = 0
        self.x = 0
        self.y = 0
        self.complete = False
        self.active = False
        pass
    def setPos(self, i, j):
        self.i = i
        self.j = j
        xy = ij2xy(i, j)
        self.x = xy[0]
        self.y = xy[1]
        self.sp.move(self.x, self.y)
        if self.sp2:
            self.sp2.move(self.x, self.y)
        pass
    pass
def removeCube(cube):
    pool.append(cube)
    arr.remove(cube)
    cube.sp.hide()
    pass
def addCube(name, i, j, name2 = None):
    cb = getCube(name, name2)
    arr.append(cb)
    cb.sp.show()
    cb.sp.front()
    if cb.sp2:
        cb.sp2.front()
        cb.sp2.show()
    cb.setPos(i, j)
    return cb

def getCube(name, name2 = None):
    for cb in pool:
        if cb.name == name:
            pool.remove(cb)
            return cb
    return Cube(name, name2)
def nextLevel():
    global level
    if level < len(maps) - 1:
        level += 1
    reStart()
    pass
def reStart():
    global data,data2,hero1,hero2,isSuccess
    data.clear()
    data2.clear()
    mp = maps[level].copy()
    for n in range(2, 178):
        cb = addCube(names[mp[n]+4], (n-2)//16, (n-2)%16)
        data.append(cb)
    for n in range(178, len(mp), 2):
        cb = addCube(names[mp[n+1]+10], mp[n]//16, mp[n]%16)
        data2.append(cb)
    hero1 = addCube(names[2], mp[0]//16, mp[0]%16, names[3])
    hero2 = addCube(names[0], mp[1]//16, mp[1]%16, names[1])
    hero1.complete = False
    hero2.complete = False
    hero1.active = True
    hero2.active = False
    updateHero(hero1)
    updateHero(hero2)
    tf.hide()
    isSuccess = False
    levelTf.change("第"+str(level+1)+"关")
    levelTf.front()
    restartTf.front()
    swapTf.front()
    pass

def updateHero(hero):
    if hero.complete:
        hero.sp.hide()
        hero.sp2.hide()
    else:
        if hero.active:
            hero.sp.hide()
            hero.sp2.show()
        else:
            hero.sp.show()
            hero.sp2.hide()
    pass
def swapHero():
    global hero1,hero2
    if hero2.complete:
        return
    tmp = hero1
    hero1 = hero2
    hero2 = tmp
    hero1.active = True
    hero2.active = False
    updateHero(hero1)
    updateHero(hero2)
    
def checkSuccess():
    global isSuccess, tf
    if hero1.complete and hero2.complete:
        isSuccess = True
        tf.front()
        tf.show()
def sleep(t):
    global isSleep
    isSleep = True
    time.sleep(t)
    isSleep = False
def getFromData2(i, j):
    for cb in data2:
        if cb.i == i and cb.j == j and not cb.sp.hidden:
            return cb
    return None
def move(di, dj):
    i = hero1.i
    j = hero1.j
    n0 = i * n + j
    i1 = i + di
    j1 = j + dj
    n1 =  i1 * n + j1
    #
    cb4 = getFromData2(i1, j1)
    if cb4 != None:
        if cb4.name == names[12] or cb4.name == names[13]: #锁
            return
    #
    i2 = i + di * 2
    j2 = j + dj * 2
    n2 = i2 * n + j2
    i3 = hero2.i
    j3 = hero2.j
    n3 = i3 * n + j3
    cb3 = data[n3]
    if (not hero2.complete) and n1 == n3:
        return
    flag1 = i1 < 0 or i1 >= m or j1 < 0 or j1 >= n
    flag2 = i2 < 0 or i2 >= m or j2 < 0 or j2 >= n
    if flag1:
        return
    cb0 = data[n0]
    cb1 = data[n1]
    cb2 = None if flag2 else data[n2]
    swapFlag = False
    if cb1.name == names[7]: # Blue
        return
    elif cb1.name == names[8]: # target
        hero1.setPos(i1, j1)
        #sleep(0.5)
        hero1.complete = True
        updateHero(hero1)
        swapHero()
        checkSuccess()
    elif cb1.name == names[9]: # Switch
        hero1.setPos(i1, j1)
        if not hero2.complete and cb3.name == cb1.name:
            #sleep(0.5)
            hero1.setPos(i3, j3)
            hero2.setPos(i1, j1)
    elif cb1.name == names[6]: # Gray
        hero1.setPos(i1, j1)
    elif cb1.name == names[5]: # White
        if hero1.name == names[0]: # Black
            hero1.setPos(i1, j1)
        elif cb2 and (cb2.name == names[4] or cb2.name == names[6]) and (hero2.complete or n2 != n3):
            swapFlag = True
        pass
    elif cb1.name == names[4]: # Black
        if hero1.name == names[2]: # White
            hero1.setPos(i1, j1)
        elif cb2 and (cb2.name == names[5] or cb2.name == names[6]) and (hero2.complete or n2 != n3):
            swapFlag = True
    if swapFlag:
        hero1.setPos(i1, j1)
        tmp = data[n2]
        data[n2] = data[n1]
        data[n1] = tmp
        data[n1].setPos(i1, j1)
        data[n2].setPos(i2, j2)
        cb5 = getFromData2(i2, j2)
        if cb5 != None:
            checkLocks()
            pass
    pass
def hideLock(name):
    for cb in data2:
        if cb.name == name and not cb.sp.hidden:
            cb.sp.hide()
def checkLocks():
    checkLock(names[10], names[4], names[12])
    checkLock(names[11], names[5], names[13])
def checkLock(name1, name2, name3):
    count = 0
    for cb in data2:
        if cb.name == name1:
            count += 1
            n0 = cb.i * n + cb.j
            cb0 = data[n0]
            if cb0.name != name2:
                return
    if count > 0:
        hideLock(name3)
def tap():
    global isSuccess, tf, isSleep
    if isSleep:
        return
    if isSuccess:
        nextLevel()
def keyup(code,key):
    global isSuccess, isSleep
    if isSuccess or isSleep:
        return
    if code == 88: # x
        swapHero()
    elif code == 82: # r
        reStart()
    elif code == 37: # ArrowLeft
        move(-1, 0)
    elif code == 38: # ArrowUp
        move(0, -1)
    elif code == 39: # ArrowRight
        move(1, 0)
    elif code == 40: # ArrowDown
        move(0, 1)
    elif code == 66: # b
        pass
    elif code == 78 and key == "N": # n/N
        nextLevel()
        pass

def loop():
    pass

reStart()
restartTf.tap = reStart
swapTf.tap = swapHero

class MyKeyBoard:
    def __init__(self, x, y, w):
        hw = w / 2
        hhw = w * 0.75
        self.arr = [
            box(x, y - w, w, w, "transparent", "red"),
            text("↑", x + hw, y - w + hhw, w, "center", "red"),
            box(x, y, w, w, "transparent", "red"),
            text("↓", x + hw, y + hhw, w, "center", "red"),
            box(x - w, y, w, w, "transparent", "red"),
            text("←", x - hw, y + hhw, w, "center", "red"),
            box(x + w, y, w, w, "transparent", "red"),
            text("→", x + w + hw, y + hhw, w, "center", "red"),
        ]
        self.arr[0].tap=self.clickUp
        self.arr[2].tap=self.clickDown
        self.arr[4].tap=self.clickLeft
        self.arr[6].tap=self.clickRight
        delay(self.loop, 100)
        
    def loop(self):
        for i in range(len(self.arr)):
            self.arr[i].front()
        delay(self.loop, 100)
    
    def onKeydown(self, keyCode, key):
        try:
            keyup(keyCode, key)
        except e:
            pass
    def clickUp(self):
        self.onKeydown(38, "ArrowLeft")
    def clickDown(self):
        self.onKeydown(40, "ArrowDown")
    def clickLeft(self):
        self.onKeydown(37, "ArrowLeft")
    def clickRight(self):
        self.onKeydown(39, "ArrowRight")
MyKeyBoard(768-160, 1024-80, 80)




也可以下载代码,本地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编程学习-woblox  (2024-11-14 16:30:47)

发表评论:

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