我一直在 youtube 上观看这个视频-- https://www.youtube.com/watch?v=horBQxH0M5A 这会创建一个弹跳球列表。
我正在尝试更改代码,使一个球变为红色,其余球变为绿色,当红色球“接触”到绿色球时,绿色球的颜色变为红色。这并不难,但我还想确保当新的红球接触到另一个绿球时,那个绿球也会将其颜色变为绿色。
我所做的是创建一个红球和一个绿球列表:
import turtle
import random
wn = turtle.Screen()
wn.bgcolor("white")
wn.title("simulator")
wn.tracer(1, 1)
# red ball
rball = turtle.Turtle()
rball.shape("circle")
rball.color("red")
# rball.penup()
rball.speed(1)
x = random.randint(-10, 10)
y = random.randint(-12, 12)
rball.goto(x,y)
# green ball
gballlist = []
for _ in range(5):
gballlist.append(turtle.Turtle())
for gballpeople in gballlist:
gballpeople.shape("circle")
gballpeople.color("green")
gballpeople.speed(1)
xh = random.randint(-10, 10)
yh = random.randint(-12, 12)
gballpeople.goto(xh, yh)
while True:
wn.update()
# rball.dy += acclerate
rball.dy = random.randint(-2, 2)
rball.dx = random.randint(-2, 2)
rball.setx(rball.xcor() + rball.dx)
rball.sety(rball.ycor() + rball.dy)
# list = [-1, 1]
# angel = random.choice(list)
angel = -1
if rball.xcor() < -100:
rball.dx *= angel
if rball.xcor() > 100:
rball.dx *= angel
if rball.ycor() < -100:
rball.dy *= angel
if rball.ycor() > 100:
rball.dy *= angel
for gball in gballlist:
gball.dy = random.randint(-2, 2)
gball.dx = random.randint(-2, 2)
gball.setx(gball.xcor() + gball.dx)
gball.sety(gball.ycor() + gball.dy)
# list = [-1, 1]
# angel = random.choice(list)
angel = -1
if gball.xcor() < -100:
gball.dx *= angel
if gball.xcor() > 100:
gball.dx *= angel
if gball.ycor() < -100:
gball.dy *= angel
if gball.ycor() > 100:
gball.dy *= angel
# 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 :
gball.color("red")
有什么建议吗?
最佳答案
下面是我对您的程序的简化,我相信您正在尝试这样做。当你说:
I also want to make sure that when the new red ball touch the other green ball, the green ball will also change color to green.
我猜你的意思是:
... will also change color to red.
以下代码没有创建明确的列表,而是使用库自己的内部事件 turtle 列表,并使用 turtle 的颜色来确定它们碰撞时应该发生什么:
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
https://stackoverflow.com/questions/60952458/
相关文章:
oracle - 无法使用 confluent CLI : java. sql.SQLExcepti
python - Flask App 进行多次登录尝试的奇怪行为
php - 实时服务器上的 Laravel Coinbase API Http 异常错误但适用于 X
r - 将 ggpredict() 和 ggplot2() 与缩放的连续变量一起使用并尝试取消缩放它
perl - 为什么我的 perl Catalyst redirect_and_detach 替换插
haskell - 使用 Haskell 从麦克风捕获音频输入?
c# - TransactionScope 抛出异常 "This platform does not