简体   繁体   中英

How can I get the color that is at a set of coordinates in python turtle?

With python, I'm trying to make a function that gets the colour that the turtle is currently on top of. I've found that turtle.pos() gives me coordinates of its location, how can I use those coordinates to get colours?

我认为最好的方法是使用此处显示的步骤: https : //www.kite.com/python/answers/how-to-find-the-rgb-value-of-a-pixel-in- Python

One way to go about this is to append every turtle you create into a list, then in your main game loop, loop through the list of turtle to see if the turtle.distance() is smaller than a certain value:

import turtle

wn = turtle.Screen()
turtles = []

yertle = turtle.Turtle('turtle')

while True:
    wn.tracer(0)
    for t in turtles:
        if yertle.distance(t) < 20 and yertle != t:
            print(f'Touched {t.color()}')
    wn.update()

In the above code I used if yertle.distance(t) < 20 which is for when all the turtles are the default size: 20 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM