In turtle python, how do I make a hitbox?

Question:

Currently, the turtle and the enemy need to have the same coordinates and are limited to only one coordinate.

I want to make a round hitbox for both the turtle and the enemy that restarts the window when they touch.

I am new to python, so haven’t really tried many different methods (as I do not know them) but I had two variables, one was the coordinates of the enemy, while the other was the coordinates of the turtle, they changed as they moved, it seems that the coordinates need to be exactly equal to each other so it doesn’t really work.

Asked By: Happy Ameoba

||

Answers:

There are no formal hitboxes in the turtle module, but you can check an area around the turtle:

if abs(turtle.xcor() - enemy.xcor()) < 5 and abs(turtle.ycor() - enemy.ycor()) < 5:
    take_damage()

would check to see if the enemy turtle is within a square with sides length 10 around the player turtle before damaging.

You can also check in a radius (see turtle.distance) or any other shape that pleases you.

Otherwise, there is no built-in hitbox with the turtle module. If you explore the docs, which I implore you do, you won’t find a hitbox class or anything similar.

Answered By: aargon

i want to make a hit-box object not collision detection because i want to call the object instead of checking coordinates. example being; a box that follows the player (turtle) and can be used to check when it collides with another object. turtle touches banana and banana hides and moves but i want the hit-box to be an OBJECT not just having if(abs(player.xcor()-abs(banana.xcor() <= 10) because i want to call it not make that every time.

Answered By: AHNODWRAERWD
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.