I am coding the arcade game pong for an introduction to c++ project at university. The game has a one player practice mode where the ball simply bounces off the opposite side of the screen and a two player mode using the w and s keys for player 1's paddle movement and up and down keys for player 2's movement. I had originally written the game in one class but after consultation with my lecturer he had suggested to construct the game using three classes for extra merit.
Fl_Window
^
|
Game Interface
^ ^
| |
One Player Two Player
The diagram my lecturer sent me can be seen above.
I have a game interface class that contains methods common to both game modes (e.g. drawing the ball, collisions off the top and bottom of the screen and moving player 1's paddle) and two other classes for the one player and two player modes that contain methods specific to the mode (e.g. moving player 2's paddle, a scoring system and drawing player 2's paddle).
Initially I create an instance of the game interface class and run an initialise game menu function which creates a menu with choice of game mode. When either mode is selected a function within the game interface class wipes the menu widgets, draws player 1's paddle, draws the ball and sends the ball off in a random direction with constant speed.
What I then wish to happen is for an instance of the correct game mode class to be created and another specific initialise function (either one player or two player) from within that class to run.
I also want to be able to return from any game mode to the main menu and be able to select another mode.
This is my first ever c++ large scale project so forgive me for poor overall conceptual understanding!
How and where would I create these instances of the specific game mode classes? Within the base game interface class or in main?
I intialise the game menu like this:
//Main Function
int main()
{
GameInterface MyInterface(GameInterface::WindowSizeX,
GameInterface::WindowSizeY, "PONG");
MyInterface . InitialiseMenu();
return Fl::run();
}
Which runs the function:
void GameInterface :: InitialiseMenu()
{
begin();
MenuTitle = new Fl_Box (400, 50, 100, 50, "Welcome to PONG");
MenuTitle -> labelcolor (FL_WHITE);
MenuTitle -> labelsize (MenuTitleTextSize);
MenuTitle -> box (FL_NO_BOX);
MenuTitle -> show();
TwoPlayerMode = new Fl_Button (400, 300, 150, 50, "Two Player Mode");
TwoPlayerMode -> callback(TwoPlayerMode_cb, this);
TwoPlayerMode -> show();
show();
}
(Note: I'm currently just attempting to get the 2 player class to work with the game interface before I try and get all three classes working)
The callback function used is:
//Define two player mode button call back function
void GameInterface :: TwoPlayerMode_cb(Fl_Widget* w,void* data )
{
((GameInterface*) data) -> TwoPlayerMode_cb_i(w);
}
void GameInterface :: TwoPlayerMode_cb_i(Fl_Widget* w)
{
TwoPlayerMode -> hide();
MenuTitle -> hide();
InitialiseGameObjects();
}
The initialise game object function is:
//Define initialise game objects
void GameInterface :: InitialiseGameObjects()
{
Fl::add_timeout(0.01, GameInterfaceUpdate_cb, this);
begin();
Ball = new Fl_Box (400, 300, 10, 10);
Ball -> box(FL_FLAT_BOX);
Ball -> color (FL_WHITE);
Ball -> show();
Player1Paddle = new Fl_Box(0, 300, PaddleSizeX, PaddleSizeY);
Player1Paddle -> box(FL_FLAT_BOX);
Player1Paddle -> color(FL_WHITE);
Player1Paddle -> show();
InitialBallDirectionGenerator();
BallVelocityX = InitialBallDirectionX * InitialBallSpeed;
BallVelocityY = InitialBallDirectionY * InitialBallSpeed;
//TwoPlayerModeInitialise_i();
end();
}
At the point where it reads //TwoPlayerModeInitialise_i(); I wish for a method contained in the two player class to run that initialises the other objects, functions etc that are required for the two player game mode. I assume I need to make the instance of that class at this point? I then wish for the methods from both classes to control the game (e.g. player 1's paddle movement comes from the game interface class and player 2's paddle movement comes from the two player mode class).
My classes are made like this:
//Game Interface Class
class GameInterface : public Fl_Window
{
//Defining Public Members
public:
GameInterface(int width, int height, const char* title=0) : Fl_Window(width, height, title)
{
color(FL_BLACK);
show();
}
...
};
&
class TwoPlayerMode : public GameInterface
{
...
};
Any help would be greatly appreciated as I really have got stuck at where to go next! Thanks.