在我的循环结束时,它应该返回并显示start和burst,就像程序第一次运行时一样。
我试着把东西搬来搬去让它工作。我给我的乌龟添加了一个重置和清除,但它不能正常运行。
#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()发布于 2021-08-21 05:09:15
现在你已经让它以你想要的方式工作了,让我们微调一下海龟的用法。这稍微改变了外观,但实际上是关于适当的技术。首先,不要使用screensize(),它不会做你想做的事情,使用setup()。
接下来,不要在像turtle这样的事件驱动环境中使用while True:。我们可以使用ontimer()事件来代替。这允许up使用exitonclick()通过单击窗口来干净地结束模拟。
让我们避免清除屏幕,这会导致需要重做bgcolor("black"),而不是清除屏幕上的乌龟绘图。因为我们使用的是tracer(),所以我们可以使用额外的update()调用,并且我们不再需要speed()调用。下面是修改后的代码:
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()发布于 2021-08-21 03:58:52
想明白了!多半是。很抱歉,第一篇文章的措辞很糟糕。
#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()发布于 2021-08-21 16:14:49
@cdlane -这是我昨晚在弄清楚我之前发布的内容后想出来的。我将使用您的一些建议/示例来清理我的代码。非常感谢你的帮助/知识!请随时运行此程序,并让我知道您的想法。
#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()https://stackoverflow.com/questions/68868956
复制相似问题