Python 与其他对象接触时更改对象颜色

Python 与其他对象接触时更改对象颜色,python,turtle-graphics,Python,Turtle Graphics,我一直在youtube上看这个视频-- 这将创建一个四处弹跳的球列表 我试图修改代码,使一个球变为红色,其余的球变为绿色,当红色球“接触”绿色球时,绿色球的颜色变为红色。这并不难,但我还想确保当新的红色球碰到另一个绿色球时,绿色球也会变为绿色 我所做的是创建一个红色球和一个绿色球列表: import turtle import random wn = turtle.Screen() wn.bgcolor("white") wn.title("simulator") wn.tracer(1,

我一直在youtube上看这个视频-- 这将创建一个四处弹跳的球列表

我试图修改代码,使一个球变为红色,其余的球变为绿色,当红色球“接触”绿色球时,绿色球的颜色变为红色。这并不难,但我还想确保当新的红色球碰到另一个绿色球时,绿色球也会变为绿色

我所做的是创建一个红色球和一个绿色球列表:

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")
导入海龟
随机输入
wn=tutle.Screen()
wn.bgcolor(“白色”)
wn.名称(“模拟器”)
wn.示踪剂(1,1)
#红球
rball=turtle.turtle()
rball.shape(“圆”)
rball.color(“红色”)
#rball.penup()
rball.speed(1)
x=random.randint(-10,10)
y=random.randint(-12,12)
rball.goto(x,y)
#绿球
gballlist=[]
对于范围(5)内的uu:
gballlist.append(turtle.turtle())
对于gballlist中的gballpeople:
gballpeople.shape(“圆”)
gballpeople.color(“绿色”)
gballpeople.速度(1)
xh=random.randint(-10,10)
yh=random.randint(-12,12)
gballpeople.goto(xh,yh)
尽管如此:
wn.update()
#rball.dy+=加速
rball.dy=random.randint(-2,2)
rball.dx=random.randint(-2,2)
rball.setx(rball.xcor()+rball.dx)
rball.sety(rball.ycor()+rball.dy)
#列表=[-1,1]
#天使=随机。选择(列表)
天使=-1
如果rball.xcor()小于-100:
rball.dx*=天使
如果rball.xcor()大于100:
rball.dx*=天使
如果rball.ycor()小于-100:
rball.dy*=天使
如果rball.ycor()大于100:
rball.dy*=天使
对于gball列表中的gball:
gball.dy=random.randint(-2,2)
gball.dx=random.randint(-2,2)
gball.setx(gball.xcor()+gball.dx)
gball.sety(gball.ycor()+gball.dy)
#列表=[-1,1]
#天使=随机。选择(列表)
天使=-1
如果gball.xcor()小于-100:
gball.dx*=天使
如果gball.xcor()大于100:
gball.dx*=天使
如果gball.ycor()小于-100:
gball.dy*=天使
如果gball.ycor()大于100:
gball.dy*=天使
#距离近时更改颜色
对于gball列表中的gball:
如果abs(rball.xcor()-gball.xcor())小于4且abs(rball.ycor()-gball.ycor())小于4:
gball.color(“红色”)

有什么建议吗?

您可以从绿色列表中删除绿色球,并将其附加到红色球列表中。然后你必须将红色的球附加到绿色列表中,并将其从红色列表中删除。

下面是我对你的程序的简化,我相信你正在尝试这样做。当你说:

我还想确保当新的红球碰到另一个球时 绿色球,绿色球也会变成绿色

我想你的意思是:

。。。也会将颜色更改为红色

下面的代码不创建显式列表,而是使用库自己的内部活动海龟列表,并使用海龟的颜色来确定它们发生碰撞时应该发生什么:

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
从海龟导入屏幕,海龟
从随机导入randint
screen=screen()
屏幕标题(“模拟器”)
屏幕跟踪(假)
对于范围(10)内的未感染者:
球=乌龟()
球形(“圆”)
球形。形状大小(0.5)
球形。颜色(“绿色”如果未感染,则为“红色”)
ball.penup()
ball.dy=randint(-2,2)
ball.dx=randint(-2,2)
x=randint(-180180)
y=randint(-180180)
球。后藤(x,y)
尽管如此:
对于屏幕中的球。海龟()
x、 y=球位置()
x+=ball.dx
y+=ball.dy
球的设定位置(x,y)
如果x<-200:
ball.dx*=-1
elif x>200:
ball.dx*=-1
如果y<-200:
ball.dy*=-1
如果y>200:
ball.dy*=-1
#距离近时更改颜色
更改=真
更改时:
更改=错误
对于屏幕中的其他。turtles()
如果ball==other或ball.pencolor()==other.pencolor():
持续
如果ball.distance(其他)将代码的结尾部分更改为:
#距离近时更改颜色
对于gball列表中的gball:
如果abs(rball.xcor()-gball.xcor())小于4且abs(rball.ycor()-gball.ycor())小于4:
如果gball.color()[0]!='红色':
gball.color(“红色”)
其他:
gball.color(“绿色”)

我试图将您的答案与OP提供的代码联系起来,但我的答案是空白的。对于原始代码的编写者来说,注释是有意义的,但是对于堆栈溢出,您应该提供更详细的答案,以便对其他读者也有用。尝试提供实际的代码行,以及您建议它们应该放置的位置。
# 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")