Hey guys. I was wondering if I could get help to get a game over screen when the enemy collides with the player.
If anyone wants to help comment on your ways to help.
Thank you.
You can create different update/draw functions for different game states and then set _update and _draw to those functions.
if player.dead then _update=gameoverupdate _draw=gameoverdraw end function gameoverupdate() ... function gameoverdraw() ... |
Now you can create a separate set of gameplay loops that takes over when you hit your game over condition.
Like this?
if ey==py then scene="menu" end if ex==px then scene="menu" end |
Sorry if I don't understand
To determine if an enemy has hit our player, we need to perform collision detection, in this case sometimes called "AABB" collision detection, as we have two "axis-aligned bounding boxes" a.k.a. two rectangles on x and y axes and we need to check if they overlap. They overlap if the following 4 conditions are true:
The left edge of rect2 is to the left of the right edge of rect1
The right edge of rect2 is to the right of the left edge of rect1
The top edge of rect2 is above the bottom edge of rect1
The bottom edge of rect2 is below the top edge of rect1
If all four of these things are true, the rectangles are overlapping on at least 1 pixel. Let's see what these conditions might look like (depending on your code):
(had to make an img as the bbs will eat angle bracket pairs as if they were html tags even in code blocks)
In the example code above, w and h stand for width and height. You may need to adjust the width and height by -1 as a sprite at 50,50 with a width of 8 would give you 58 for px+pw when in fact the last pixel of the sprite lies on column 57.
[Please log in to post a comment]