与另一个物体接触时改变物体颜色
Change object color upon contact with another object
我一直在 youtube 上观看这个视频--https://www.youtube.com/watch?v=horBQxH0M5A
这会创建一个弹跳球列表。
我正在尝试更改代码,使一个球变为红色,其余球变为绿色,当红球 "touches" 变为绿球时,绿球将颜色变为红色。这并不难,但我还想确保当新的红球接触到另一个绿球时,那个绿球也会将其颜色变为绿色。
我所做的是创建一个红球和一个绿球列表:
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.
以下代码没有创建明确的列表,而是使用库自己的内部活动海龟列表,并使用海龟的颜色来确定它们碰撞时应该发生什么:
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
将代码的结尾部分更改为:
# 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")
我一直在 youtube 上观看这个视频--https://www.youtube.com/watch?v=horBQxH0M5A 这会创建一个弹跳球列表。
我正在尝试更改代码,使一个球变为红色,其余球变为绿色,当红球 "touches" 变为绿球时,绿球将颜色变为红色。这并不难,但我还想确保当新的红球接触到另一个绿球时,那个绿球也会将其颜色变为绿色。
我所做的是创建一个红球和一个绿球列表:
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.
以下代码没有创建明确的列表,而是使用库自己的内部活动海龟列表,并使用海龟的颜色来确定它们碰撞时应该发生什么:
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
将代码的结尾部分更改为:
# 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")