14
2024
11

python编程学习-Dont-Crash

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

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

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

from xdf import *
import math

bgs = ["car-bg1@003h", "car-bg2@003i", "car-bg3@003j"]
fill(bgs[0])
car1 = stamp("car-1@003n")
car2 = stamp("car-2@003o")
carHit = stamp("car-hit@003p")
run = stamp("car-run@003q")
speed = 0.01
p1 = 0
p2 = 0
car1_path = 0
car2_path = 1

turn = 0
isOver = True
cx = 385
cy = 510
changed = False
turnTf = text("第1圈", 10, 40, 30, "red")

def getPathOnDis(p, d):
    p1 = []
    for i in range(0, len(p), 2):
        x = p[i]
        y = p[i+1]
        dx = x - cx
        dy = y - cy
        a = math.sqrt(dx * dx + dy * dy)
        dx = dx/a
        dy = dy/a  
        a = a + d
        p1.append(round(cx + dx * a))
        p1.append(round(cy + dy * a))
    return p1

def pLength(p):
    s = 0
    for i in range(0, len(p), 2):
        x1 = p[i]
        y1 = p[i+1]
        x2 = p[(i+2)%len(p)]
        y2 = p[(i+3)%len(p)]
        dx = x2 - x1
        dy = y2 - y1
        a = math.sqrt(dx * dx + dy * dy)
        s += a
    return s
def rad2ang(rad):
    return rad * 180 / math.pi
def ang2rad(ang):
    return ang * math.pi / 180
def getPatS(p, n, st):
    s = n * st
    if s == 0:
        return [p[0], p[1], 0]
    s0 = 0
    for i in range(0, len(p), 2):
        x1 = p[i]
        y1 = p[i+1]
        x2 = p[(i+2)%len(p)]
        y2 = p[(i+3)%len(p)]
        dx = x2 - x1
        dy = y2 - y1
        a = math.sqrt(dx * dx + dy * dy)
        if s0 <= s and s0 + a >= s:
            s = s - s0
            dx = dx / a
            dy = dy / a
            return [x1 + dx * s, y1 + dy * s, rad2ang(math.atan2(dy, dx))]
        s0 += a
    #print("error:", n, st)
    return [p[0], p[1], 0]

path = [221,511,221,493,221,475,221,457,221,439,221,421,221,403,221,385,221,367,221,350,221,332,222,314,224,296,229,279,235,262,242,246,252,231,263,217,275,204,288,192,303,181,318,173,335,166,352,161,370,159,388,158,405,159,423,162,440,168,456,175,472,184,486,195,499,207,511,221,521,235,530,251,537,267,543,284,547,302,549,319,549,337,549,355,549,373,549,391,549,409,549,427,549,445,549,463,549,480,549,498,549,516,549,534,549,552,549,570,549,588,549,606,549,624,549,641,549,659,549,677,549,695,548,713,545,730,540,747,533,764,525,780,515,795,504,809,491,821,478,833,463,843,447,851,430,857,413,861,395,863,377,863,359,861,342,857,325,851,309,843,294,834,280,822,267,810,256,796,246,781,238,765,231,749,226,731,223,714,221,696,221,678,221,660,221,642,221,624,221,606,221,589,221,571,221,553,221,535,222,516]
path1 = getPathOnDis(path, 20)
path2 = getPathOnDis(path, -20)
paths = [path1, path2]
s1 = pLength(path1)
s2 = pLength(path2)
ss = [s1, s2]

def updateCar():
    pp1 = getPatS(paths[car1_path], p1, ss[car1_path])
    pp2 = getPatS(paths[car2_path], (1-p2), ss[car2_path])
    car1.move(pp1[0], pp1[1])
    car2.move(pp2[0], pp2[1])
    car1.rotate(pp1[2] + 90)
    car2.rotate(pp2[2] - 90)
    pass
def updateTurn():
    turnTf.change("第" + str(turn+1) + "圈")
    pass
def reset():
    global p1,p2,isOver,car1_path,car2_path,turn,changed
    p1 = 0
    p2 = 0
    car1_path = 0
    car2_path = 1
    turn = 0
    isOver = False
    changed = False
    updateCar()
    updateTurn()
    carHit.hide()
    run.hide()
    carHit.hide()
    pass
def checkOver():
    if car1_path != car2_path:
        return
    global isOver
    d0 = abs(p1-(1-p2))
    if d0 > 0.5:
        d0 = 1 - d0
    if d0 < 60 / ss[car1_path]:
        run.show()
        carHit.show()
        carHit.move((car1.x + car2.x) / 2, (car1.y + car2.y) / 2)
        isOver = True
    pass
def tap():
    global isOver,car1_path
    if isOver:
        return
    car1_path = (car1_path + 1) % 2
    pass
def loop():
    global isOver,p1,p2,car2_path,turn,changed
    if isOver:
        return
    p1 += speed
    p2 += speed
    if p1 > 1:
        p1 -= 1
        turn += 1
        changed = False
        updateTurn()
    if p2 > 1:
        p2 -= 1
    if not changed and random(1000) > 950:
        d0 = abs(p1-(1-p2))
        if d0 > 0.2 and d0 < 0.8:
            changed = True
            car2_path = (car2_path + 1) % 2
        pass
    updateCar()
    checkOver()
    pass

def start():
    delay(reset, 100)
run.tap = start











也可以下载代码,本地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)

发表评论:

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