Question
I am currently trying to create a game like Frogger in C++ and Openframeworks. I am unsure of how to set up the collision detection
I am currently trying to create a game like Frogger in C++ and Openframeworks.
I am unsure of how to set up the collision detection loop and keep track of the number of lives. The player gets 3 at attemps get the Frog to the otherside before ending the game.
This is what my OfApp.cpp file looks like. My professor does not wanting to use any game engines. Just the ofRectangle class and the method intersects. The way I am doing it now, I feel like it is redundant but it works. Please let me know if theres an easier way to simplify the loop.
void ofApp::update()
{
if (game_state == "start")
{
}
if(game_state == "play")
{
player.update();
testCar.update();
testCar2.update();
testCar3.update();
testCar4.update();
turtle.update();
croc.update();
int Lives;
bool collision;
bool collision2;
bool collision3;
bool collision4;
collision = player.intersects(testCar);
collision2 = player.intersects(testCar2);
collision3 = player.intersects(testCar3);
collision4 = player.intersects(testCar4);
Lives = 3;
if(player.intersects(testCar))
{ collision = true;
Lives = Lives - 1;
}
if (collision == true) game_state = "end";
// ------------------------------------------------------------------------------------//
if(player.intersects(testCar2))
{
collision2 = true;
Lives = Lives - 1;
}
if (collision2 == true) game_state = "end";
// ------------------------------------------------------------------------------------//
if(player.intersects(testCar3))
{
collision3 = true;
Lives = Lives - 1;
}
if (collision3 == true) game_state = "end";
// ------------------------------------------------------------------------------------//
if(player.intersects(testCar4))
{
collision4 = true;
Lives = Lives - 1;
}
if (collision4 == true) game_state = "end";
else if (Lives <= 0) game_state = "end";
printf(" Num lives: %d", Lives);
}
if (game_state == "end")
{
player.setup();
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started