ENDING THE GAME

hey i have an issue with my code
no issues are actually present - the code runs fine
however it doesn't do what i want it to
it should display monsters (green) and traps (red) on the screen
the character (blue) is able to move around by using keyboard keys w,s,a,d
aim is to walk around the screen until monsters have fallen into traps
i have failed to do this so i just put a message inside the if loop saying 'monster has broken trap' if anyone is able to make it so the monster is eliminated from the screen it would be a massive help
also when the character is caught by a monster or falls into trap the game should end
it displays the appripriate message at the moment but allows the user to continue playing
please can someone help end the game and make the screen just close down if the character either falls in trap or is eaten by monster ?

thanks in advance !!


// assignment 2 - survival game
#include <iostream>
#include <vector >
#include <cstdlib>
#include <ctime> // Interprets the value pointed by timer as a calendar time
#include <string> // enables use of strings (objects that represent sequences of characters)
#include <iomanip> //is part of the Input/output library
#include "console.h" //This file redirects the cin , cout channels to use a console window
#include "conio.h" // header file used to provide console input/output
using namespace std;
int random(int max) // function that will output random number between 1-max
{
return rand() % max + 1; // will return random number between 1-max
}
class Player
{
private:
int x = 0; // x axis
int y = 0; // y axis
char name = 'o'; // outputs 'o'
int px = x; // past x axis
int py = y; // past y axis
char movement = ' ';
public:
int getX()
{
return x; //to later be inputted into a function to check player position against monsters and traps position
}
int getY()
{
return y; //to later be inputted into a function to check player position against monsters and traps position
}
 
void move() // function which allows character to move with control of keyboard
{
px = x;
py = y;
movement = _getch(); //non-standard function to get a user inputted character
switch (movement) // allows user to control character by using keyboard (wsad)
{
case 'w': // letter 'W' on keyboard
y--; // moves up
break; //the break statement ends the switch statement that immediately encloses it.
case 's': // letter 'S' on keyboard
y++; // moves down
break;
case 'a': // letter 'A' on keyboard
x--; // moves left
break;
case 'd': // letter 'D' on keyboard
x++; // moves right
break;
}
}
void display() // display function that outputs position of player using console to set cursor position.
{
Console::setCursorPosition(py, px); // past x and y coordinates
cout << ' '; // set px and py to blank space ( to avoid a trail of o's from character movement)
Console::setColour(Console::BLUE); // set player to colour blue
Console::setCursorPosition(y, x); // set coordinates on grid
cout << name; // outputs 'o'
}
};
class Monsters
{
int x = 0; // x axis
int y = 0; // y axis
char name = '*'; // this is what the monsters will look like on the grid
public:
void setmonsterposition(int inX, int inY) // function that sets monster position onto the grid using x and y axis
{
x = inX; // x axis
y = inY; // y axis
}
void display() // display function that outputs position of monster using console to set cursor position.
{
Console::setCursorPosition(y, x); // set coordinates on grid
Console::setColour(Console::GREEN); // display's monster on grid in colour green
cout << name; // outputs '*'
}
int getX() //to later be inputted into a function to check player position against monsters current position
{
return x;
}
int getY() //to later be inputted into a function to check player position against monsters current position
{
return y;
}
bool checkForMonsters(Player &playr, vector<Monsters> game_monsters) // function that checks player position against monster position to see if player has been eaten
{
for (int check_position = 0; check_position < game_monsters.size(); check_position++) // for loop checks position against trap position
{
if (playr.getX() == game_monsters[check_position].getX() && playr.getY() == game_monsters[check_position].getY()) // if position equal to trap position execute statement
{
cout << " You have been eaten by a monster!! " << endl; // display message to user indicating that they have fallen into a trap

return true;
}
}
return false;
}
void movement(Player &playr) // movement function which enables monsters to move randomly ( but drawn to character)
{
int px = x;
int py = y;
if (playr.getX() > this->x)// allows you to access inner class member variable
/*it is a constant pointer that holds the memory address of the current object*/
/*makes constructor or function parameter and class data member readable and unambiguous to program*/
{
x++;
if (playr.getY() > this->y)
{
y++;
}
}
else if (playr.getX() < this->x)
{
x--;
if (playr.getY() < this->y)
{
y--;
}
}
Console::setCursorPosition(py, px); // past x and y coordinates
cout << ' ';
}
};
class Traps
{
int x = 0; // x axis
int y = 0; // y axis
char name = '@'; // this is what the traps will look like on the grid
public:
void setTrapPosition(int inX, int inY) // function that sets trap position onto the grid using x and y axis
{
x = inX; // x axis of trap
y = inY; // y axis of trap
 
}
void display() // display function that outputs position of trap using console to set cursor position.
{
Console::setCursorPosition(y, x); // set coordinates on grid
Console::setColour(Console::RED); // display's traps on grid in colour red
cout << name; // outputs '@'
}
int getX()
{
return x; //to later be inputted into a function to check player position against trap current position
}
int getY()
{
return y; //to later be inputted into a function to check player position against trap current position
}
bool checkForplayer(Player &playr, vector<Traps> game_traps) // function that checks for player ( checks if player coordinates equal trap)
{
for (int check_position = 0; check_position < game_traps.size(); check_position++) // for loop checks position against trap position
if (playr.getX() == game_traps[check_position].getX() && playr.getY() == game_traps[check_position].getY()) // if position equal to trap position execute statement
{
cout << " You have fallen into a trap!! " << endl; // display message to user indicating that they have fallen into a trap

return true;
}
return false;
}
void checkForMonster(vector<Monsters> game_monsters, vector<Traps> game_traps) // function that checks for monster ( checks if player coordinates equal trap)
{
for (int i = 0; i < game_traps.size(); i++)
{
for (int j = 0; j < game_monsters.size(); j++)
{
if (game_traps[i].getX() == game_monsters[i].getX() && game_traps[i].getY() == game_monsters[i].getY()) // if position equal to trap position execute statement
{
Cout << “ monster has broken the trap “ << endl;
}
}
}
}
};
 
int main()
{
srand(static_cast<unsigned int>(time(0)));
rand();
bool gameover = false;
char movement = ' ';
Monsters monster_obstacle;
Traps trap_obstacle;
vector<Traps> game_traps; // used vector for monsters - undefined size and ability to resize
vector<Monsters> game_monsters; // used vector for monsters - undefined size and ability to resize
Player game_player;
for (int monst_and_traps = 0; monst_and_traps < 5; monst_and_traps++)
{
game_traps.push_back(Traps());
game_monsters.push_back(Monsters());
game_monsters[monst_and_traps].setmonsterposition(random(20), random(20));
game_traps[monst_and_traps].setTrapPosition(random(20), random(20));
game_monsters[monst_and_traps].display();
game_traps[monst_and_traps].display();
}
while (movement != 'q' && !gameover) // while user does not type the letter 'q' into keyboard the game will continue
{
game_player.move();// calling movement function
game_player.display();
for (int i = 0; i < game_monsters.size(); i++)
{
game_monsters[i].movement(game_player);
game_monsters[i].display();
}
gameover = monster_obstacle.checkForMonsters(game_player, game_monsters); // calling checkForMonsters function
gameover = trap_obstacle.checkForplayer(game_player, game_traps); // calling checkForplayer function
trap_obstacle.checkForMonster(game_monsters, game_traps);
 
 
}
 
Console::pause("END OF GAME"); // displays message 'END OF GAME' when game ends
return 0;
}
These kind of things can be controled with a bool variable.

for example...

bool playerIsAlive = 1

Then if player gets caught by monster...

playerIsAlive = 0

Add in your loop to check if playerIsAlive == 1, if not then gameover.
i have inserted a bool variable into my code and it occasionally works but doesnt work all the time

would there be anything else to insert?
Pay attention to when it does not work. See if you can purposfully get it to not work. Then you might understand where in your code the problem lies.
yeah if you want to be able to kill the player from within any function by altering the variable of a bool, that's one of those rare situations where it would be completely appropriate to use a globally declared variable.
Topic archived. No new replies allowed.