I am working on a project for my c++ class, and we are asked to do some extra work on our moon lander clone. I would like to add an option to restart, or reset, the game when the user loses or wins the game. I understand that, as a student, I really need to do this on my own, but in my defence, they have not taught us anything about OpenGL. Any help is appreciated, even if I am redirected to another source for a hint.
The entire game is made up of classes, as this is an object-oriented college class I am taking.
I'm a little lost, you have a key while loop where you take user input and react to it right?
I'd have to know the general layout of your files and classes, but this should be a simple if statement with a pop-up window when the player loses... What are you using to open the window in the first place? (SDL2, SFML, QT, GTK, etc?) I'm not sure about SFML or Alegro, but SDL2, QT, and GTK have the capability to open up secondary windows in which you could ask for user input. This should be one instance where you don't have to program using openGL.
Thank you so much for replying! Sorry about the lack of information, but I suppose you are right! This is the provided code for the method that handles user input (I think):
void Game :: handleInput(const Interface & ui)
{
if (lander.isAlive() && !lander.isLanded())
{
if (ui.isDown())
{
lander.applyThrustBottom();
}
if (ui.isLeft())
{
lander.applyThrustLeft();
}
if (ui.isRight())
{
lander.applyThrustRight();
}
}
}
I suppose it would just be a matter of saying if the lander is dead or landed, if the user presses space, restart the game. My problem is, what would I do? Just call main()?
I've heard of somebody recursively calling main before, but I don't know what the results of that would be. Don't do that, the way I heard about it was a teacher mentioning it like it was a joke...
Since everything is object oriented in your code, you could create a new game object. (I'm again assuming things, but I'm pretty sure the object "Game" is your key object where you put all the other objects together into one component, correct?). Also remember to check that you reset any global non-constant variables.
A simple suggestion would be to create a method in your Game class that resets all the variables, as this will save memory rather than creating a separate new Game object each time. Otherwise there is a chance of actually running out of allotted memory (though not a huge concern with a simple game, this grows more worrisome as the object gains in complexity and size).
The next (and probably better) option is to create a ~deconstructor() method that cleanly deletes the object when you call it and then you could create it again with the same name, also preventing memory buildup/leaks.
If Game is not fully self contained you will have to track down all of the outsider variables and objects that you used that might affect Game and set them to a beginning state.
As a side thought;
If you did set the the Game into an array or a vector/list then you could set up separate save-states/profiles, the user could start a new game, or pause and let someone else play a separate profile then return later to their previous game... That's probably too much if this is coming due soon.
Thank you so much for all your help newbieg! I only got into programming a few months ago, and you are the first guy I have spoken to who makes sense!
Examining further, I don't think I can restart this game (well, not without some major reconstructing to the whole program). I would, however, like to try something else. I want to out to the screen the remaining "fuel" the user has as the points they earned. In the code, I have this:
if (lander.isLanded())
{
drawText(Point(), "You have successfully landed!");
}
The code for the drawText() method is this:
void drawText(const Point & topLeft, const char * text)
{
void *pFont = GLUT_BITMAP_HELVETICA_12; // also try _18
// prepare to draw the text from the top-left corner
glRasterPos2f(topLeft.getX(), topLeft.getY());
// loop through the text
for (const char *p = text; *p; p++)
glutBitmapCharacter(pFont, *p);
}
Obviously, I can't just pass lander.getFuel() in the char pointer parameter, as "fuel" is an integer, but is there a way to change it to a char? How would casting work with classes?
There are quite a few ways to do that, this is a simple enough operation that looking up "integer to string c++" in google will give you immediate answers. Oddly there isn't a function in the early c++ standard that will get it done, the later c++ standards (11 and later have the to_string() method), but depending on your compiler/IDE you may have to tell it what version of the c++ standard to use.
http://www.cplusplus.com/reference/string/to_string/
If the to_string method causes your IDE problems, then search how to add c++11 support to your IDE (this would only have to be done once per project, should take less than a minute or two research included, and shouldn't affect your previous code).
PS, Opengl is an advanced topic that usually isn't attempted until a person has a solid understanding of the basic language. While Glut and Glew can help to soften the curve, I am impressed that you are starting it in only a few months, hopefully you have some spare time to spend in building your basic c++ skills as well.