15
2024
11

python编程学习-收不完的西瓜

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

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

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

import time
from xdf import *

W = 768
H = 1024
CW = 64
M = int(W / CW)
N = int(H / CW)
isRunning = False
mps = [
    [[4,8],[5, 8],[6,8],[7,8]],
    [[6,6],[6,7],[6,8],[6,9]],
    [[3,9], [3,8], [4,8], [5,8], [6,8], [7,8], [8,8], [9,8]],
    [[5,6], [6,6], [6,7], [6,8], [6,9], [6,10], [6,11], [6,12]],
    [[4,9],[5,9],[5,8],[6,8],[6,7],[7,7]],
    [[5,10],[5,9],[6,9],[6,8],[7,8],[7,7]],
    [[2,10],[2,9],[3,9],[4,9],[4,8],[5,8],[6,8],[6,7],[7,7],[8,7]],
    [[4,4],[5,4],[5,5],[5,6],[6,6],[6,7],[6,8],[7,8],[7,9],[7,10]],
    [[4,4],[4, 5],[4, 6],[4,7], [4, 8],[5, 8],[6,8],[7,8],[8,8]],
    [[3,5],[4, 5],[5, 5],[6,5], [7, 5],[7, 6],[7,7],[7,8],[7,9]],
]
nums = [3,3,4,4,4,4,5,5,6,6]
answers = [
    "RRR",
    "DDD",
    "URT1",
    "RDT1",
    "RTU0",
    "UTR0",
    "URRT0",
    "RDDT0",
    "D十0RT3",
    "R十0DT3",
]
mpId = 0
mp = []
cubes = []
heroPos = [0,0]

def ij2xy(i, j):
    return [i * CW + CW / 2, j * CW + CW / 2]

def xy2ij(x, y):
    return [int(x/CW + 0.5), int(y/CW + 0.5)]

baseUrl = "https://code-file.xdf.cn/files/dingdangweb/20230810/"
for i in range(M):
    a = []
    cubes.append(a)
    for j in range(N):
        xy = ij2xy(i, j)
        co = '1691645320008_385580.png'
        if (j & i) % 2 != (j | i) % 2:
            co = '1691645320015_029791.png'
        b = stamp(baseUrl + co, xy[0], xy[1])
        a.append(b)

target = stamp(baseUrl+"1691645320000_235392.png")

hero = [
    stamp(baseUrl + "1691645320028_632517.png"),
    stamp(baseUrl + "1691645320013_192993.png"),
    stamp(baseUrl + "1691645320030_177936.png"),
    stamp(baseUrl + "1691645320027_277487.png"),
    stamp(baseUrl + "1691645320026_494298.png"),
    stamp(baseUrl + "1691645320000_157226.png"),
    stamp(baseUrl + "1691645320026_731721.png"),
    stamp(baseUrl + "1691646391107_430018.png"),
]
heroId = 0
levelTf = text("第1关", 600, 50, 50, "red")
numTf = text("目标指令个数:4", 200, 50, 30, "red")
numTf2 = text("输入指令个数:4", 200, 100, 30, "red")
answerTf = text("答案:RRR", 200, 150, 30, "red")
loseTf = text("失败了,重新输入指令", 50, "red", "center")
winTf = text("过关了,进入下一关", 50, "red", "center")
runBtn = box(10, 10, 150, 50, '#cccccc', "black")
runBtnTf = text("输入指令", 85, 45, 30, "black", "center")
ansBtn = box(10, 70, 150, 50, '#cccccc', "black")
ansBtnTf = text("答案", 85, 105, 30, "black", "center")

def refreshMp():
    for i in range(M):
        for j in range(N):
            cubes[i][j].hide()
    for i in range(len(mp)):
        cubes[mp[i][0]][mp[i][1]].show()

def updateHeroPos(t):
    hxy = ij2xy(heroPos[0],heroPos[1])
    for sp in hero:
        sp.move(hxy[0], hxy[1], t)

def updateTargetPos():
    global mp
    n = len(mp) - 1
    hxy = ij2xy(mp[n][0], mp[n][1])
    target.move(hxy[0], hxy[1])
    
def reset():
    global heroPos, mp, mps, mpId,isRunning
    isRunning = False
    if mpId >= len(mps):
        mpId = 0
    levelTf.change("第"+str(mpId + 1)+"关")
    numTf.change("目标指令个数:" + str(nums[mpId]))
    numTf2.change("输入指令个数:0")
    answerTf.change("答案:" + answers[mpId])
    answerTf.hide()
    mp = mps[mpId]
    loseTf.hide()
    winTf.hide()
    heroPos = [mp[0][0], mp[0][1]]
    refreshMp()
    updateHeroPos(0)
    updateTargetPos()
    stand()

def stand():
    global heroId
    heroId = 0
    
def walk():
    global heroId
    if heroId == 0:
        heroId = 1
    
def moveHero(x,y):
    global heroPos
    heroPos[0] = heroPos[0] + x
    heroPos[1] = heroPos[1] + y
    updateHeroPos(1000 * 0.9)
    time.sleep(1)

def moveLeft():
    moveHero(-1, 0)

def moveRight():
    moveHero(1, 0)

def moveUp():
    moveHero(0, -1)

def moveDown():
    moveHero(0, 1)
    
def checkLose():
    global mp, heroPos
    for i in range(len(mp)):
        if heroPos[0] == mp[i][0] and heroPos[1] == mp[i][1]:
            return False
    lose()
    return True

def checkWin():
    global mp,heroPos
    n = len(mp) - 1
    if heroPos[0] == mp[n][0] and heroPos[1] == mp[n][1]:
        win()
        return True
    return False

def lose():
    stand()
    loseTf.show()
    delay(reset, 2000)

def win():
    stand()
    global mpId
    mpId = mpId + 1
    winTf.show()
    delay(reset, 2000)
    
def run(cmds):
    numTf2.change("输入指令个数:" + str(len(cmds)))
    walk()
    global isRunning
    isRunning = True
    #print('hello',cmds)
    n = len(cmds)
    i = 0
    c = 0
    zm = "零一二三四五六七八九十"
    while i < n and i >= 0:
        char = cmds[i]
        if char in "左←L":
            moveLeft()
        elif char in "右→R":
            moveRight()
        elif char in "上↑U":
            moveUp()
        elif char in "下↓D":
            moveDown()
        elif char in zm:
            if c == zm.index(char):
                i = i + 1
        elif char.isdigit():
            i = int(char) - 1
        elif char in "目标T":
            if checkWin():
                return
        elif char in "清C":
            c = -1
        c = c + 1
        if checkLose():
            return;
        i = i + 1
    if not checkWin():
        lose()

def onStart():
    global isInput, isRunning
    if isRunning:
        return
    isInput = True
    #print("onStart")

isInput = False
def loop():
    global isInput,cmds,heroId
    for sp in hero:
        sp.hide()
    hero[heroId].show()
    if heroId != 0:
        heroId = heroId + 1
        if heroId == len(hero):
            heroId = 1
    if isInput:
        isInput = False
        cmds = input("指令")
        run(cmds)

def showAnswer():
    answerTf.show()

runBtn.tap = onStart
ansBtnTf.tap = showAnswer
reset()


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



« 上一篇下一篇 »

相关文章:

python编程学习-显示帧频  (2024-11-15 11:35:26)

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

python编程学习-拼图  (2024-11-15 11:33:25)

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)

发表评论:

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