Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Web Database Development Step By Step

Authors: Jim Buyens

1st Edition

0735609667, 978-0735609662

More Books

Students also viewed these Databases questions

Question

6. Vanguard

Answered: 1 week ago

Question

1. PricewaterhouseCoopers

Answered: 1 week ago