猫史档案馆


【Python作品分享】【去太空,趣编程】绘制火箭长征8号【作品秀】

用户:BKS乄MCproblemBKS乄MCproblem查看:1 回复:1 评论:1 创建时间:2021-05-05T18:38:51


【作品展示】

center_image

 

【作品介绍】

用turtle库绘制的长征八号运输火箭,代码纯原创,点一下就脱落一节,最后只留下运输仓。

 

【作品源代码】

import turtle
import random
import time


# 画笔
pen = turtle.Pen()
pen.speed(0)

# 背景
space = turtle.Screen()

space.bgcolor('black')
space.setup(2000, 800)

# 平滑移动函数


def moveto(x, y):

    pen.penup()
    pen.goto(x, y)
    pen.pendown()

# 长方形函数


def rectangel(width, height, pencolor="red", fillcolor="white", pensize=10):

    pen.pencolor(pencolor)
    pen.fillcolor(fillcolor)
    pen.pensize(pensize)
    pen.begin_fill()
    for i in [width, height, width, height]:
        pen.forward(i)
        pen.right(90)
    pen.end_fill()

# 星型函数


def star(lenth, pencolor, fillcolor, pensize):
    pen.pencolor(pencolor)
    pen.fillcolor(fillcolor)
    pen.pensize(pensize)
    pen.begin_fill()
    for i in range(5):
        pen.forward(lenth)
        pen.right(144)
    pen.end_fill()

# 圆形函数


def circle(angel, lenth, pencolor, fillcolor, pensize):
    pen.pencolor(pencolor)
    pen.fillcolor(fillcolor)
    pen.pensize(pensize)
    pen.begin_fill()
    for i in range(360//angel):
        pen.forward(lenth)
        pen.right(angel)
    pen.end_fill()

# 三角形函数


def triangle(a, b, c, pencolor, fillcolor, pensize):
    pen.pencolor(pencolor)
    pen.fillcolor(fillcolor)
    pen.pensize(pensize)
    pen.begin_fill()
    for point in [a, b, c]:
        pen.goto(point[0], point[1])
    pen.end_fill()


# 开始进入Python的世界
ignite = False
fall = 3


def main():
    # 星星
    global ignite, fall
    space.tracer(0)
    for _count in range(20):
        moveto(random.randint(-1000, 1000), random.randint(-400, 400))
        pen.setheading(random.randint(0, 360))
        star(random.randint(10, 50), "light yellow", "yellow", 1)
    pen.setheading(0)
    space.tracer(10)
    if fall >= 1:
        body()
    if fall >= 2:
        propeller()
    if fall >= 3:
        fire(ignite)
    ignite = not ignite
# 脱落


def fall_off(x, y):
    global fall
    if fall > 1:
        fall -= 1


# 载人舱


def body():

    # 火箭身
    moveto(150, 100)
    rectangel(400, 200, 'red', 'white', 10)
    moveto(550, 100)
    triangle((650, 0), (550, -100), (550, 100), "lightgray", "dark gray", 10)

    # 窗户
    moveto(400, 50)
    circle(5, 4, "light gray", "sky blue", 5)

    # 文字
    pen.pencolor('dark red')
    moveto(150, 0)
    pen.write("长征", align="left", font=("Arial", 50, "italic"))
    moveto(175, -100)
    pen.write("8号", align="left", font=("Arial", 50, "italic"))

# 推进器


def propeller():

    # 主推进
    moveto(-400, 50)
    rectangel(550, 100, "red", "white", 10)

    # 副推进①
    moveto(-400, 120)
    rectangel(240, 70, "dark blue", "cyan")

    # 副推进②
    moveto(-400, -50)
    rectangel(240, 70, "dark blue", "cyan")

    # 文字
    moveto(-170, -60)
    pen.pencolor("red")
    pen.write("CHINA", move=False, align="center", font=("Arial", 80, "bold"))

# 尾焰


def fire(if_ignite):
    if if_ignite:
        # 短尾焰①
        moveto(-500, 120)
        rectangel(100, 70, "orange", "red")
        # 长尾焰
        moveto(-550, 50)
        rectangel(150, 100, "orange", "red")
        # 短尾焰②
        moveto(-500, -50)
        rectangel(100, 70, "orange", "red")
    else:
        # 长尾焰①
        moveto(-550, 120)
        rectangel(150, 70, "orange", "red")
        # 短尾焰
        moveto(-500, 50)
        rectangel(100, 100, "orange", "red")
        # 长尾焰②
        moveto(-550, -50)
        rectangel(150, 70, "orange", "red")


while True:
    main()
    for _count in range(100):
        space.listen()
        space.onclick(fall_off)
        space.listen()
        time.sleep(0.00000001)
    space.clear()
    space.bgcolor('black')
    
    


turtle.done()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
我是曲奇饼干我是曲奇饼干

厉害!👍竞争对手+1

点赞0


评论