【问题标题】:Change object color upon contact with another object与另一个物体接触时改变物体颜色
【发布时间】:2020-03-31 14:02:34
【问题描述】:

我一直在 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")

有什么建议吗?

【问题讨论】:

    标签: python turtle-graphics


    【解决方案1】:

    以下是我对您的程序的简化,我相信您正在尝试做的事情。当你说:

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

    我猜你的意思是:

    ... 也会将颜色变为红色。

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

    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】:

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

      【讨论】:

      • 我正在尝试将您的答案与 OP 提供的代码联系起来,并且正在画一个空白。可能对于原始代码的作者来说,您的评论是有道理的,但对于堆栈溢出,您应该提供更详细的答案,以使其对其他读者也有用。尝试提供实际的代码行,以及您建议的位置。
      【解决方案3】:

      将代码的结尾部分更改为:

      # 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")
      

      【讨论】:

        猜你喜欢
        • 2017-01-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多