我一直在youtube上观看此视频-https://www.youtube.com/watch?v=horBQxH0M5A 会创建一个反弹的球列表。
我正在尝试更改代码,以使一个球变成红色,其余的变成绿色,当红色球“碰到”一个绿色球时,绿色球将颜色变为红色。这并不难,但是我还想确保当新的红色球碰到另一个绿色球时,该绿色球也将其颜色更改为绿色。
我所做的是创建一个红色球和一个绿色球列表:
@ViewChild('nameInput', { static: false }) nameInputRef: ElementRef;
有什么建议吗?
答案 0 :(得分:0)
您可以从绿色列表中删除绿色球,并将其附加到红色球列表中。那么您必须将红球附加到绿色列表中,并将其从红色列表中删除。
答案 1 :(得分:0)
以下是我对您的程序的简化,它执行了我认为您要尝试执行的操作。当你说:
我还想确保当新的红球碰到另一个时 绿球,绿球也将变为绿色。
我想你的意思是:
...还将颜色更改为红色。
以下代码不是创建显式列表,而是使用库自身的活动乌龟内部列表,并使用乌龟的颜色来确定碰撞时应发生的情况:
from turtle import Screen, Turtle
from random import randint
screen = Screen()
screen.title("Simulator")
screen.tracer(False)
for uninfected in range(10):
ball = Turtle()
ball.shape('circle')
ball.shapesize(0.5)
ball.color('green' if uninfected else 'red')
ball.penup()
ball.dy = randint(-2, 2)
ball.dx = randint(-2, 2)
x = randint(-180, 180)
y = randint(-180, 180)
ball.goto(x, y)
while True:
for ball in screen.turtles():
x, y = ball.position()
x += ball.dx
y += ball.dy
ball.setposition(x, y)
if x < -200:
ball.dx *= -1
elif x > 200:
ball.dx *= -1
if y < -200:
ball.dy *= -1
elif y > 200:
ball.dy *= -1
# change the color when distance is near
changed = True
while changed:
changed = False
for other in screen.turtles():
if ball == other or ball.pencolor() == other.pencolor():
continue
if ball.distance(other) <= 10:
ball.color('red')
other.color('red')
changed = True
screen.update()
screen.mainloop() # never reached
答案 2 :(得分:0)
# change the color when distance is near
for gball in gballlist:
if abs(rball.xcor() - gball.xcor()) < 4 and abs(rball.ycor() - gball.ycor()) < 4 :
if gball.color()[0] != 'red':
gball.color("red")
else:
gball.color("green")