Answer the question
In order to leave comments, you need to log in
How to make collision between grenade and floor in pygame?
I want to make a grenade in the game but I don't know how to make it stay on the blocks.
using python with pygame library
Answer the question
In order to leave comments, you need to log in
The collision should not be between, but on the floor and on the grenade ..
Good day.
Let's imagine the situation: the image of the grenade overlapped the image of the earth:
Let the Rect of the earth = Rect(0, 90, 100, 10) with a window of 100x100px
Case 1.
Let the x and y of the grenade be stored separately.
Then you need to create a new Rect with the coordinates of the grenade and the size of the desired hitbox. If you turn on the rendering of such a Rect , you can see the following picture:
That is, this is the hitbox of the grenade.
Next, we need to take each hitbox point in the picture (this works well with small target and large checking hitboxes, if the ground was small and the grenade was huge, then the check would need to be done from the ground to the grenade and from the grenade to the ground) and compare overlap with ground hitbox:
groundRect.collidepoint(granadeRectPoint)
# True если перекрывается
granadeRect = granadeImage.get_rect()
groundRect.collidepoint(granadeRectPoint)
# True если перекрывается
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question