首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >制作烟花

制作烟花
EN

Stack Overflow用户
提问于 2021-08-20 23:27:17
回答 3查看 83关注 0票数 1

在我的循环结束时,它应该返回并显示start和burst,就像程序第一次运行时一样。

我试着把东西搬来搬去让它工作。我给我的乌龟添加了一个重置和清除,但它不能正常运行。

代码语言:javascript
复制
#import modules
import turtle
import random

#set screen size
win = turtle.Screen()
win.screensize (600,600)

#set turtle for firework
start = turtle.Turtle()
#set turtles for burst
burst1 = turtle.Turtle()

line = random.randint(10, 35)
line2 = random.randint(10, 15)

#background color


#start firework (base)(clear after each step?)


def base():
    for x in range(1):
        start.ht()
        start.up()
        start.goto(random.randint(-100, 100), -300)
        start.down()
        start.setheading(90)
        for x in range(line):
               start.forward(5)
               start.up()
               start.forward(5)
               start.down()
               start.clear()
        start.clear()

#burst
def burst():
    head = 45
    turtle.tracer(0,0)
    for x in range(20):
        burst1.ht()
        burst1.up()
        burst1.goto(start.xcor(),start.ycor())
        burst1.down()
        burst1.setheading(head)
        for x in range(line2):
            burst1.forward(5)
            burst1.up()
            burst1.forward(5)
            burst1.down()
            head = head + 15
            turtle.update()
        

#reset
while True:
    base()
    burst()
    start.reset()
    burst1.reset()
EN

回答 3

Stack Overflow用户

发布于 2021-08-21 05:09:15

现在你已经让它以你想要的方式工作了,让我们微调一下海龟的用法。这稍微改变了外观,但实际上是关于适当的技术。首先,不要使用screensize(),它不会做你想做的事情,使用setup()

接下来,不要在像turtle这样的事件驱动环境中使用while True:。我们可以使用ontimer()事件来代替。这允许up使用exitonclick()通过单击窗口来干净地结束模拟。

让我们避免清除屏幕,这会导致需要重做bgcolor("black"),而不是清除屏幕上的乌龟绘图。因为我们使用的是tracer(),所以我们可以使用额外的update()调用,并且我们不再需要speed()调用。下面是修改后的代码:

代码语言:javascript
复制
from turtle import Screen, Turtle
from random import randint, choice

COLORS = [
    '#FFFFFF', '#FF0000', '#00FF00', '#0000FF', '#FFFF00',
    '#00FFFF', '#FF00FF', '#C0C0C0', '#808080', '#800000',
    '#808000', '#008000', '#800080', '#008080', '#000080',
]

def launch():
    rocket.penup()
    rocket.goto(randint(-100, 100), -300)
    rocket.pendown()

    for _ in range(randint(5, 45)):
        rocket.forward(5)
        rocket.penup()
        rocket.forward(5)
        rocket.pendown()

        screen.update()
        rocket.clear()

def burst():
    angle = 15

    for _ in range(360 // angle):
        firework.penup()
        firework.goto(rocket.position())
        firework.pendown()
        firework.setheading(angle)

        for _ in range(5):
            firework.color(choice(COLORS))
            firework.forward(randint(3, 15))
            firework.penup()
            firework.forward(randint(3, 15))
            firework.pendown()

        angle += 15

    screen.update()

def display():
    firework.clear()
    launch()
    burst()

    screen.ontimer(display, randint(75, 750))

screen = Screen()
screen.setup(600, 600)
screen.bgcolor('black')
screen.tracer(False)

rocket = Turtle()
rocket.hideturtle()
rocket.color('white')
rocket.setheading(90)

firework = Turtle()
firework.hideturtle()
firework.pensize(3)

display()

screen.exitonclick()
票数 2
EN

Stack Overflow用户

发布于 2021-08-21 03:58:52

想明白了!多半是。很抱歉,第一篇文章的措辞很糟糕。

代码语言:javascript
复制
#import modules
import turtle
import random
import time
#set screen size
win = turtle.Screen()
win.screensize (600,600)

#set turtle for firework
start = turtle.Turtle()
#set turtles for burst
burst1 = turtle.Turtle()

burst1.ht()
colors = ["#FFFFFF","#FF0000","#00FF00","#0000FF","#FFFF00","#00FFFF","#FF00FF","#C0C0C0","#808080","#800000","#808000","#008000","#800080","#008080","#000080"]

#background color


#start firework (base)(clear after each step?)


def base():
    for x in range(1):
        start.ht()
        start.pencolor("White")
        start.speed(0)
        start.up()
        start.goto(random.randint(-100,100),-300)
        start.down()
        start.setheading(90)
        for x in range(random.randint(5, 45)):
               start.forward(5)
               start.up()
               start.forward(5)
               start.down()
               start.clear()

#burst
def burst():
    head = 15
    burst1.speed(0)
    burst1.pensize(3)
    for x in range(25):
        turtle.tracer(0,0)
        burst1.ht()
        burst1.up()
        burst1.goto(start.xcor(),start.ycor())
        burst1.down()
        burst1.setheading(head)
        for x in range(5):
            burst1.color(random.choice(colors))
            burst1.forward(random.randint(3, 15))
            burst1.up()
            burst1.forward(random.randint(3, 15))
            burst1.down()
            head = head + 15
            turtle.update()
        

#reset
while True:
    win.bgcolor("black")
    base()
    burst()
    turtle.clearscreen()
票数 1
EN

Stack Overflow用户

发布于 2021-08-21 16:14:49

@cdlane -这是我昨晚在弄清楚我之前发布的内容后想出来的。我将使用您的一些建议/示例来清理我的代码。非常感谢你的帮助/知识!请随时运行此程序,并让我知道您的想法。

代码语言:javascript
复制
#import modules
import turtle
import random
import time
#set screen size
win = turtle.Screen()
win.screensize (600,600)

#set turtle for firework
start = turtle.Turtle()
#set turtles for burst
burst1 = turtle.Turtle()
burst2 = turtle.Turtle()
build = turtle.Turtle()

burst1.ht()
colors = ["#FF0000","#00FF00","#0000FF","#FFFF00","#00FFFF","#FF00FF","#C0C0C0","#808080","#800000","#808000","#008000","#800080","#008080","#000080"]

#background color


#start firework (base)(clear after each step?)

def building():
    build.ht()
    build.pencolor("white")
    build.speed(10)
    build.up()
    build.goto(-350,-300)
    build.down()
    build.setheading(0)
    build.forward(10)
    for x in range(10):
        build.setheading(90)
        build1 = random.randint(10,50)
        build.forward(build1)
        build.setheading(0)
        build.forward(random.randint(10,20))
        build.setheading(270)
        build.forward(build1)
        build.setheading(0)
        build.forward(random.randint(1,20))
    build.pencolor("black")
    build.forward(25)
    build.pencolor("white")
    build.forward(10)
    for x in range(10):
        build.setheading(90)
        build1 = random.randint(10,50)
        build.forward(build1)
        build.setheading(0)
        build.forward(random.randint(10,20))
        build.setheading(270)
        build.forward(build1)
        build.setheading(0)
        build.forward(random.randint(1,20))
    


def base():
    for x in range(1):
        start.ht()
        start.pencolor("White")
        start.pensize(1)
        start.speed(0)
        start.up()
        start.goto(random.randint(-300,300),-300)
        start.down()
        start.setheading(90)
        for x in range(random.randint(5, 45)):
               start.forward(5)
               start.up()
               start.forward(5)
               start.down()
               start.clear()

#burst
def burst():
    head = 15
    burst1.speed(0)
    burst1.pensize(2)
    for x in range(25):
        turtle.tracer(10,0)
        burst1.ht()
        burst1.up()
        burst1.goto(start.xcor(),start.ycor())
        burst1.down()
        burst1.setheading(head)
        for x in range(5):
            burst1.color(random.choice(colors))
            burst1.forward(random.randint(3, 15))
            burst1.up()
            burst1.forward(random.randint(3, 15))
            burst1.down()
            head = head + 15
            turtle.update()

def small_burst():
    head = 15
    burst2.speed(0)
    burst2.pensize(1)
    for x in range(25):
        turtle.tracer(10,0)
        burst2.ht()
        burst2.up()
        burst2.goto(start.xcor(),start.ycor())
        burst2.down()
        burst2.setheading(head)
        for x in range(5):
            burst2.color(random.choice(colors))
            burst2.forward(random.randint(2, 5))
            burst2.up()
            burst2.forward(random.randint(2, 10))
            burst2.down()
            head = head + 15
            turtle.update()
        

#reset
while True:
    win.bgcolor("black")
    building()
    base()
    burst()
    base()
    small_burst()
    base()
    small_burst()
    turtle.clearscreen()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68868956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档