I was trying the code and I noticed that sometimes the check will make the game run into the game over state even if the circle does not touch the axe sometimes.
I noticed this when I added a small code snippet to restart the game after the game runs into the game over state.
if(collision_with_axe == true)
{
if(IsKeyDown(KEY_SPACE) || IsKeyDown(KEY_A) || IsKeyDown(KEY_D))
{
collision_with_axe = false;
circle_x = 0;
}
}
So sometimes it will reset my position without getting the Game Over message.
I know for a fact that this is not happening because of keeping the keys pressed down, since the game will stop executing and show the Game Over message and make you press down a key again to return to a new game.
Am I the only one who’s getting this issue?
This is my whole axe game cpp
#include "raylib.h"
int main()
{
int width = 400;
int height = 300;
InitWindow(width, height, "MyWindow");
//circle
int circle_x = 0;
int circle_y = height/2;
float circle_radius = 25;
//circle edges
int l_circle_x = circle_x - circle_radius;
int r_circle_x = circle_x + circle_radius;
int u_circle_y = circle_y - circle_radius;
int b_circle_y = circle_y + circle_radius;
//axe
int axe_x = width/2;
int axe_y = 0;
int axe_lenght = 50;
//axe edges
int l_axe_x = axe_x;
int r_axe_x = axe_x + axe_lenght;
int u_axe_y = axe_y;
int b_axe_y = axe_y + axe_lenght;
//physics
int direction = 10;
//collision
bool collision_with_axe =
(b_axe_y >= u_circle_y) &&
(u_axe_y <= b_circle_y) &&
(l_axe_x <= r_circle_x) &&
(r_axe_x >= l_circle_x);
SetTargetFPS(60);
while(WindowShouldClose() == false)
{
BeginDrawing();
ClearBackground(WHITE);
if(collision_with_axe)
{
DrawText("GAME OVER\nTry Again?", width/2, height/2, 20 , RED);
} else
{
//Game logic
//update edges
l_circle_x = circle_x - circle_radius;
r_circle_x = circle_x + circle_radius;
u_circle_y = circle_x - circle_radius;
b_circle_y = circle_x + circle_radius;
l_axe_x = axe_x;
r_axe_x = axe_x + axe_lenght;
u_axe_y = axe_y;
b_axe_y = axe_y + axe_lenght;
//update collision
collision_with_axe =
(b_axe_y >= u_circle_y) &&
(u_axe_y <= b_circle_y) &&
(l_axe_x <= r_circle_x) &&
(r_axe_x >= l_circle_x);
//draw stuff
DrawCircle(circle_x, circle_y, circle_radius, BLUE);
DrawRectangle(axe_x, axe_y, axe_lenght, axe_lenght, RED);
axe_y += direction;
if (axe_y > height || axe_y < 0)
{
direction = -direction;
}
if (IsKeyDown(KEY_D) && circle_x < width)
{
circle_x += 7;
}
if (IsKeyDown(KEY_A) && circle_x > 0)
{
circle_x -= 7;
}
}
if(collision_with_axe == true){
if(IsKeyDown(KEY_SPACE) || IsKeyDown(KEY_A) || IsKeyDown(KEY_D)){
collision_with_axe = false;
circle_x = 0;
}
}
EndDrawing();
}
}