Change object color upon contact with another object

Robin Jiang

I have been watching this video on youtube--https://www.youtube.com/watch?v=horBQxH0M5A which creates a list of balls bouncing around.

I'm trying to alter the code to make one ball red, the rest green, and when the red ball "touches" a green ball, the green ball changes color to red. That wasn't hard, but I also want to make sure that when the new red ball touches another green ball, that green ball will also change its color to green.

What I did was create a single red ball and a list of green balls:

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

Any suggestions?

cdlane

Below is my simplification of your program that does what I believe you're trying to do. When you say:

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.

What I assume you meant was:

... will also change color to red.

Rather than create explicit lists, the following code uses the library's own internal list of active turtles and uses the color of the turtle to determine what should happen when they collide:

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

enter image description here

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Change object color with mouse drag

分類Dev

Image object color change using kineticjs

分類Dev

Creating a new object seems change another object previously created (???)

分類Dev

How to change the color of material of an object individually if the material is shared

分類Dev

converting json object in to another object

分類Dev

How to change the value of the key form an object with the value of some another key of the same object?

分類Dev

SWIFT - How to get connection of an object in table cell to change attribute of it from another object in table cell

分類Dev

How to cast a object into another?

分類Dev

jasmine testing service angular - could not find an object to spy upon for getBaseUrl()

分類Dev

How to change the structure of the object

分類Dev

Change object names using if()

分類Dev

How can i change the color-offset of an object to CMYK in Actionscript 3?

分類Dev

Object reference is required for the non-static field, method, or property Label1 Color change

分類Dev

Accessing Properties of one Object from another Object

分類Dev

Stub an object which is inside another object constructor

分類Dev

Dynamic object creation inside another object

分類Dev

Is there a way to connect a model object with another object?

分類Dev

Rotate object to "look at" another object in 3 dimensions?

分類Dev

Passing an object from a controller to another

分類Dev

Accessing object properties in another function

分類Dev

Place object between another two

分類Dev

Add another object into existing JSON

分類Dev

pandas - change time object to a float?

分類Dev

Change the format of JavaScript Data object

分類Dev

Change CSS with properties defined in an object

分類Dev

Change PHP Object Instance to a Parent

分類Dev

Javascript change specific object properties

分類Dev

Bokeh Legend Object - passing assigned color to legend

分類Dev

Wand object Color creation very slow

Related 関連記事

  1. 1

    Change object color with mouse drag

  2. 2

    Image object color change using kineticjs

  3. 3

    Creating a new object seems change another object previously created (???)

  4. 4

    How to change the color of material of an object individually if the material is shared

  5. 5

    converting json object in to another object

  6. 6

    How to change the value of the key form an object with the value of some another key of the same object?

  7. 7

    SWIFT - How to get connection of an object in table cell to change attribute of it from another object in table cell

  8. 8

    How to cast a object into another?

  9. 9

    jasmine testing service angular - could not find an object to spy upon for getBaseUrl()

  10. 10

    How to change the structure of the object

  11. 11

    Change object names using if()

  12. 12

    How can i change the color-offset of an object to CMYK in Actionscript 3?

  13. 13

    Object reference is required for the non-static field, method, or property Label1 Color change

  14. 14

    Accessing Properties of one Object from another Object

  15. 15

    Stub an object which is inside another object constructor

  16. 16

    Dynamic object creation inside another object

  17. 17

    Is there a way to connect a model object with another object?

  18. 18

    Rotate object to "look at" another object in 3 dimensions?

  19. 19

    Passing an object from a controller to another

  20. 20

    Accessing object properties in another function

  21. 21

    Place object between another two

  22. 22

    Add another object into existing JSON

  23. 23

    pandas - change time object to a float?

  24. 24

    Change the format of JavaScript Data object

  25. 25

    Change CSS with properties defined in an object

  26. 26

    Change PHP Object Instance to a Parent

  27. 27

    Javascript change specific object properties

  28. 28

    Bokeh Legend Object - passing assigned color to legend

  29. 29

    Wand object Color creation very slow

ホットタグ

アーカイブ