Skip to content Skip to sidebar Skip to footer

How To Detect If Turtle Is In The Radius Of X & Y And Then Do Something?

Currently, I'm trying to make a game and in the game I would like it so if the character is on top of an object, it picks it up. This is what I have so far: import turtle import ti

Solution 1:

Since I don't have your images, nor recognize what your game is about, below is an example of the functionality you describe. On the screen is a black circle and pink square. You can drag the circle and if you drag it onto the square, it will sprout a head and legs becoming a turtle. Dragging off the square, it reverts to being a circle:

from turtle import Screen, Turtle

def drag(x, y):
    default.ondrag(None)  # disable handler inside handler

    default.goto(x, y)

    if default.distance(scar) < 40:
        default.shape('turtle')
    elif default.shape() == 'turtle':
        default.shape('circle')

    default.ondrag(drag)

wn = Screen()
wn.setup(500, 500)

scar = Turtle('square', visible=False)
scar.shapesize(4)
scar.color('pink')
scar.penup()
scar.left(90)
scar.forward(50)
scar.showturtle()

default = Turtle('circle', visible=False)
default.shapesize(2)
default.speed('fastest')
default.penup()
default.left(90)
default.backward(50)
default.showturtle()

default.ondrag(drag)

wn.mainloop()

Solution 2:

I dont know the turtle-graphics, but in real world to determine the distance between two points (for 2D surfaces) we use Pythagorean theorem.

If some object is at (x1, y1) and another at (x2, y2), the distance is

dist=sqrt((x1-x2)^2 + (y1-y2)^2)

So, if dist <= R, turtle (or whatever) is in R radius from desired point

Post a Comment for "How To Detect If Turtle Is In The Radius Of X & Y And Then Do Something?"