14
2024
11

python编程学习-一笔画

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

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

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

from xdf import *
import math

def ang2rad(ang):
    return math.pi * ang / 180
def rad2ang(rad):
    return 180 * rad / math.pi
class factory:
    def __init__(self, tp, co):
        self.tp = tp
        self.co = co
        self.arr = []
        self.pool = []
        pass
    def get(self):
        if len(self.pool):
            return self.initC(self.pool.pop())
        return self.initC(self.create())
    def initC(self, c):
        self.arr.append(c)
        c.front()
        c.show()
        return c
    def remove(self, c):
        c.hide()
        self.pool.append(c)
        self.arr.remove(c)
        pass
    def removeAll(self):
        for c in self.arr:
            c.hide()
        self.pool.extend(self.arr)
        self.arr.clear()
        pass
    def create(self):
        if self.tp == 'circle':
            return circle(0, 0, 10, self.co)
        if self.tp == 'line':
            return line(0, 0, 100, 100, 5, self.co)
        return None
    pass

levels = [
    [[382,298,536,618,184,638],[0,2,4,0]],
    [[226,336,548,324,556,696,224,696],[0,2,4,6,0]],
    [[218,316,556,310,230,706,560,696],[0,2,4,6,0]],
    [[386,300,574,682,164,450,594,382,310,698],[0,2,4,6,8,0]],
    [[198,436,582,432,576,736,196,736,380,280],[0,2,4,6,0,8,2]],
    [[570,698,206,704,198,404,390,240,564,382],[0,2,4,6,8,4,0,8,2]],
    [[350,234,76,436,588,438,200,696,466,686],[0,2,4,0,6,8,0]],
    [[354,468,162,684,342,180,550,670],[0,2,4,0,6,4]],
    [[550,314,352,166,202,306,124,502,210,652,382,772,506,654,614,500,354,482],[0,2,4,6,8,10,12,14,0,12,16,4,8,16,0,4]],
    [[364,216,268,304,348,400,436,288,158,362,342,498,458,588,234,600,510,362],[0,2,4,0,6,4,8,10,12,14,10,16,4]]
]
level = 0
levelData = levels[level]
levelTf = text('', 0, 30, 30, 'red')
btn = text('重置', 768, 30, 30, 'red','right')
facC = factory('circle', 'red')
facL = factory('line', 'black')
facLU = factory('line', 'blue')
pu = [0, 0, None]
lu = []

def setLine(li, x0, y0, x1, y1):
    dx = x1 - x0
    dy = y1 - y0
    sz = math.sqrt(dx * dx + dy * dy)
    rot = math.atan2(dy, dx)
    li.move(x0, y0)
    li.rotate(rad2ang(rot) + 90)
    li.size(sz)
    pass
def refreshLevel():
    ps = levelData[0]
    ls = levelData[1]
    for i in range(1, len(ls)):
        x0 = ps[ls[i-1]]
        y0 = ps[ls[i-1]+1]
        x1 = ps[ls[i]]
        y1 = ps[ls[i]+1]
        l = facL.get()
        setLine(l, x0, y0, x1, y1)
        pass
    for i in range(0, len(ps), 2):
        x = ps[i]
        y = ps[i+1]
        c = facC.get()
        c.move(x, y)
    pass
def reset():
    clearULine()
    facC.removeAll()
    facL.removeAll()
    updateLevel()
    refreshLevel()
    pass
btn.tap = reset

def nextLevel():
    global level, levelData
    level += 1
    if level >= len(levels):
        level = 0
    levelData = levels[level]
    reset()
    pass

def updateLevel():
    levelTf.change('第' + str(level + 1) + '关')
    pass

def distance2(x0, y0, x1, y1):
    dx = x1 - x0
    dy = y1 - y0
    return dx * dx + dy * dy

def getP(x, y):
    ps = levelData[0]
    for i in range(0, len(ps), 2):
        xi = ps[i]
        yi = ps[i+1]
        if distance2(x, y, xi, yi) < 20 * 20:
            return i
        pass
    return -1
def getPat(i):
    ps = levelData[0]
    return [ps[i], ps[i+1]]
def clearULine():
    pu[2] = None
    lu.clear()
    facLU.removeAll()
    pass
def touch():
    clearULine()
    i = getP(x, y)
    if i != -1:
        pu[2] = facLU.get()
        lu.append(i)
        pu[0] = x
        pu[1] = y
        updateUline()
    pass
def updateUline():
    p = getPat(lu[-1])
    setLine(pu[2], p[0], p[1], pu[0], pu[1])
    pass
def getPat(i):
    ps = levelData[0]
    return [ps[i], ps[i+1]]
def touching():
    if pu[2]:
        i = getP(x, y)
        if i!= -1 and i != lu[len(lu)-1]:
            p = getPat(i)
            pu[0] = p[0]
            pu[1] = p[1]
            updateUline()
            pu[2] = facLU.get()
            lu.append(i)
        pu[0] = x
        pu[1] = y
        updateUline()
        pass
    pass

def untouch():
    if pu[2]:
        facLU.remove(pu[2])
        pu[2] = None
        if checkWin():
            delay(nextLevel, 1000)
        else:
            delay(reset, 1000)
        pass
    pass

def getS(ls):
    a = []
    for i in range(1, len(ls)):
        i1 = ls[i-1]
        i2 = ls[i]
        if i1 > i2:
            a.append('-'.join([str(i2),str(i1)]))
        else:
            a.append('-'.join([str(i1),str(i2)]))
        pass
    a.sort()
    s = ','.join(a)
    #print(s)
    return s
def checkWin():
    ls = levelData[1]
    return len(ls) == len(lu) and getS(ls) == getS(lu)

reset()



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

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

发表评论:

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