简体   繁体   中英

In turtle python, how do I make a hitbox?

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.

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.

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