我需要同时运行Wait
,Begin
和Float
,其中
Wait
是计时器,Begin
设置气泡,Float
使气泡上升
V用于启动计时器
W用于使气泡上升
X是生成位置
Y用于左右移动
代码:
import turtle
import random
from random import randint
turtle.bgcolor("black")
turtle.setup(500,500)
Await = randint(1,4)/2
Bwait = randint(1,4)/2 + Await
Cwait = randint(1,4)/2 + Bwait
Dwait = randint(1,4)/2 + Cwait
Ewait = randint(1,4)/2 + Dwait
Fwait = randint(1,4)/2 + Ewait
Gwait = randint(1,4)/2 + Fwait
Hwait = randint(1,4)/2 + Gwait
Iwait = randint(1,4)/2 + Hwait
Jwait = randint(1,4)/2 + Iwait
Kwait = randint(1,4)/2 + Jwait
Lwait = randint(1,4)/2 + Kwait
Mwait = randint(1,4)/2 + Lwait
Nwait = randint(1,4)/2 + Mwait
Owait = randint(1,4)/2 + Nwait
A = turtle.Turtle()
B = turtle.Turtle()
C = turtle.Turtle()
D = turtle.Turtle()
E = turtle.Turtle()
F = turtle.Turtle()
G = turtle.Turtle()
H = turtle.Turtle()
I = turtle.Turtle()
J = turtle.Turtle()
K = turtle.Turtle()
L = turtle.Turtle()
M = turtle.Turtle()
N = turtle.Turtle()
O = turtle.Turtle()
A.shapesize(.5)
B.shapesize(.5)
C.shapesize(.5)
D.shapesize(.5)
E.shapesize(.5)
F.shapesize(.5)
G.shapesize(.5)
H.shapesize(.5)
I.shapesize(.5)
J.shapesize(.5)
K.shapesize(.5)
L.shapesize(.5)
M.shapesize(.5)
N.shapesize(.5)
O.shapesize(.5)
def Float(z):
w = 1
while w < 520:
z.forward(1)
z.left(90)
y = randint(-1,1)
z.forward(y)
z.right(90)
w = w + 1
def Begin(z):
x = randint(-250,250)
z.shape('circle')
z.color('black')
z.penup()
z.right(90)
z.forward(260)
z.left(90)
z.forward(x)
z.left(90)
z.color('red')
z.speed(.5)
Float(z)
def Wait(v):
if v == Await:
Begin(A)
Wait(Await + .1)
if v == Bwait:
Begin(B)
Wait(Bwait + .1)
if v == Cwait:
Begin(C)
Wait(Cwait + .1)
if v == Dwait:
Begin(D)
Wait(Dwait + .1)
if v == Ewait:
Begin(E)
Wait(Ewait + .1)
if v == Fwait:
Begin(F)
Wait(Fwait + .1)
if v == Gwait:
Begin(G)
Wait(Gwait + .1)
if v == Hwait:
Begin(H)
Wait(Hwait + .1)
if v == Iwait:
Begin(I)
Wait(Iwait + .1)
if v == Jwait:
Begin(J)
Wait(Jwait + .1)
if v == Kwait:
Begin(K)
Wait(Kwait + .1)
if v == Lwait:
Begin(L)
Wait(Lwait + .1)
if v == Mwait:
Begin(M)
Wait(Mwait + .1)
if v == Nwait:
Begin(N)
Wait(Nwait + .1)
if v == Owait:
Begin(O)
v = v + .1
Wait(0)
答案 0 :(得分:0)
每次将函数 Wait()称为一次时,v就会增加1。看起来像您要循环播放?很难理解。这样可以使代码实际运行,但是我不知道这是否就是您想要的。
def Wait(v):
while True:
if v == Await:
Begin(A)
Wait(Await + .1)
if v == Bwait:
Begin(B)
Wait(Bwait + .1)
if v == Cwait:
Begin(C)
Wait(Cwait + .1)
if v == Dwait:
Begin(D)
Wait(Dwait + .1)
if v == Ewait:
Begin(E)
Wait(Ewait + .1)
if v == Fwait:
Begin(F)
Wait(Fwait + .1)
if v == Gwait:
Begin(G)
Wait(Gwait + .1)
if v == Hwait:
Begin(H)
Wait(Hwait + .1)
if v == Iwait:
Begin(I)
Wait(Iwait + .1)
if v == Jwait:
Begin(J)
Wait(Jwait + .1)
if v == Kwait:
Begin(K)
Wait(Kwait + .1)
if v == Lwait:
Begin(L)
Wait(Lwait + .1)
if v == Mwait:
Begin(M)
Wait(Mwait + .1)
if v == Nwait:
Begin(N)
Wait(Nwait + .1)
if v == Owait:
Begin(O)
v = v + .1
无论您要做什么。看课。您正在创建大量重复数据。类就像数据模板一样,您可以轻松使用它们。我为您重写了代码:
class Turtle:
def __init__(self, wait, turtle):
self.wait = wait
self.turtle = turtle
self.turtle.shapesize(.5)
self.v = 0
def Wait(self, time):
if self.v == 2:
return
self.Begin()
self.v += 1
self.Wait(self.v)
def Begin(self):
x = randint(-250,250)
self.turtle.shape('circle')
self.turtle.color('black')
self.turtle.penup()
self.turtle.right(90)
self.turtle.forward(260)
self.turtle.left(90)
self.turtle.forward(x)
self.turtle.left(90)
self.turtle.color('red')
self.turtle.speed(.5)
self.Float()
def Float(self):
w = 1
while w < 520:
self.turtle.forward(1)
self.turtle.left(90)
y = randint(-1,1)
self.turtle.forward(y)
self.turtle.right(90)
w = w + 1
turtles = []
prev_wait = 0
for x in range(20):
wait = randint(1,4)/2 + prev_wait
turtles.append(Turtle(wait, turtle.Turtle()))
prev_wait = wait
for turtle in turtles:
turtle.Wait(0)
答案 1 :(得分:0)
以下是我认为您的代码尝试执行的操作的实现。这可能比您准备要复杂的多,但我希望它为您提供了有关如何重新编写自己的代码的想法。特别是在冒泡原型和克隆以避免大量冗余代码的概念中:
from turtle import Screen, Turtle
from random import randint
BUBBLE_COUNT = 9
BUBBLE_DIAMETER = 10
WIDTH, HEIGHT = 500, 500
CURSOR_SIZE = 20
def bubbles_rise():
for start_time, bubble in bubbles:
if time > start_time and bubble.ycor() < HEIGHT/2 + BUBBLE_DIAMETER:
bubble.forward(1)
dx = randint(-1, 1)
if dx != 0:
bubble.setx(bubble.xcor() + dx)
if any(bubble.ycor() < HEIGHT/2 + BUBBLE_DIAMETER for _, bubble in bubbles):
screen.ontimer(bubbles_rise, 10)
else:
print("Finished rising!")
def bubbles_launch():
global time
for start_time, bubble in bubbles:
if time >= start_time:
bubble.showturtle()
if any(start_time > time for start_time, _ in bubbles):
time += 0.1
screen.ontimer(bubbles_launch, 100)
else:
print("Finished launching!")
screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.bgcolor("black")
bubble_prototype = Turtle('circle', visible=False)
bubble_prototype.penup()
bubble_prototype.sety(-HEIGHT/2 - BUBBLE_DIAMETER) # just out of sight
bubble_prototype.color('red')
bubble_prototype.shapesize(BUBBLE_DIAMETER / CURSOR_SIZE)
bubble_prototype.setheading(90)
bubble_prototype.speed('fastest')
bubbles = []
start_time = 0
for _ in range(BUBBLE_COUNT):
start_time += randint(1, 4) / 2
new_bubble = bubble_prototype.clone()
new_bubble.setx(randint(BUBBLE_DIAMETER - WIDTH/2, WIDTH/2 - BUBBLE_DIAMETER)) # just inside
bubbles.append((start_time, new_bubble))
time = 0
bubbles_rise()
bubbles_launch()
screen.mainloop()