I have to do a little game for my school, and I'm stuck in my program. When I launch the app it's working fine, but when I want to start a new game by the menubar, it says that the game is starting, but it isn't. I think the program is stuck in my FenRPS::newGame() function, but I don't know how to fix it.
FenRPS::FenRPS() : wxFrame( NULL, wxID_ANY, "Rock–paper–scissors",
wxDefaultPosition, wxSize(607,650), wxCAPTION|wxCLOSE_BOX|wxCLIP_CHILDREN )
{
this->SetBackgroundColour( wxColor( 240,240,240 ));
this->SetIcon( wxIcon( AppRPS::Icone_xpm ));
//================ MENU ================
wxMenuItem* item;
#define Item( menu, fctEvenement, texte, aide ) \
item = menu->Append( wxID_ANY, texte, aide ); \
menu->Bind( wxEVT_MENU, fctEvenement, this, item->GetId() );
#define Separateur( menu ) menu->AppendSeparator();
menuGame = new wxMenu;
Item( menuGame, newGame, "&New Game", "Create a new game" );
Separateur( menuGame );
Item( menuGame, exit, "Exit", "Exit the game" );
menuAbout = new wxMenu;
Item( menuAbout, about, "&About", "Display app informations" );
menuBar = new wxMenuBar;
menuBar->Append( menuGame, "&Game" );
menuBar->Append( menuAbout, "&About" );
this->SetMenuBar( menuBar );
//=============== BOUTONS ==============
rock_png = new wxStaticBitmap(this, wxID_ANY, wxBitmap("img/rock.png",
wxBITMAP_TYPE_PNG), wxPoint(54,400), wxSize(128,128));
buttonRock.Create( this, wxID_ANY, "R O C K", wxPoint(54,538), wxSize(128,50));
buttonRock.Bind( wxEVT_BUTTON, playedRock, this );
paper_png = new wxStaticBitmap(this, wxID_ANY,
wxBitmap("img/paper.png", wxBITMAP_TYPE_PNG), wxPoint(236,400), wxSize(128,128));
buttonPaper.Create( this, wxID_ANY, "P A P E R", wxPoint(236,538), wxSize(128,50));
buttonPaper.Bind( wxEVT_BUTTON, playedPaper, this );
scissors_png = new wxStaticBitmap(this, wxID_ANY,
wxBitmap("img/scissors.png", wxBITMAP_TYPE_PNG), wxPoint(418,400), wxSize(128,128));
buttonScissors.Create( this, wxID_ANY, "S C I S S O R S", wxPoint(418,538), wxSize(128,50));
buttonScissors.Bind( wxEVT_BUTTON, playedScissors, this );
stTextBox = new wxStaticText;
stTextBox->Create( this, wxID_ANY, "\nWelcome in the Rock-Paper-Scissors game\n\n\n\nNo game is in progress", wxPoint(10,10), wxSize(580,364), wxALIGN_CENTRE_HORIZONTAL);
stTextBox->SetBackgroundColour( *wxLIGHT_GREY );
stTextBox->SetFont( wxFont( wxFontInfo(12).FaceName("Arial").Bold()));
if( hasPlayed )
{
srand(time(0));
choiceBot = (rand()%3)+1;
message << "Round n°" << nbrRound << "\n";
stTextBox->SetLabel( message );
if (choicePlayer == 1 && choiceBot == 1) message << message << "Equality\n\n\n";
else if (choicePlayer == 1 && choiceBot == 2)
{
message << message << "Round lost, the bot has made 'paper'\n\n\n";
scoreBot++;
}
else if (choicePlayer == 1 && choiceBot == 3)
{
message << message << "Round win, the bot had made 'scissors'\n\n\n";
scorePlayer++;
}
else if (choicePlayer == 2 && choiceBot == 1)
{
message << message << "Round win, the bot had made 'rock'\n\n\n";
scorePlayer++;
}
else if (choicePlayer == 2 && choiceBot == 2) message << message << "Equality\n\n\n";
else if (choicePlayer == 2 && choiceBot == 3)
{
message << message << "Round lost, the bot has made 'scissors'\n\n\n";
scoreBot++;
}
else if (choicePlayer == 3 && choiceBot == 1)
{
message << message << "Round lost, the bot has made 'rock'\n\n\n";
scoreBot++;
}
else if (choicePlayer == 3 && choiceBot == 2)
{
message << message << "Round win, the bot had made 'paper'\n\n\n";
scorePlayer++;
}
else if (choicePlayer == 3 && choiceBot == 3) message << message << "Equality\n\n\n";
stTextBox->SetLabel( message );
nbrRound++;
hasPlayed = false;
}
if( nbrRound > 5 )
{
message << "The game is over\n\n"
<< "Score :\n"
<< ">> Player : " << scorePlayer
<< "\n>> Computer : " << scoreBot;
if (scoreBot == scorePlayer)
message << message << "Equality. Try again\n";
else if (scoreBot > scorePlayer)
message << message << "You lost, you'll be luckier next time\n";
else if (scorePlayer > scoreBot)
message << message << "You won, congratulations !\n";
stTextBox->SetLabel( message );
wxSleep(2);
}
}
FenRPS::~FenRPS() {}
void FenRPS::playedRock( wxCommandEvent& ) { choicePlayer = 1; hasPlayed = true; }
void FenRPS::playedPaper( wxCommandEvent& ) { choicePlayer = 2; hasPlayed = true; }
void FenRPS::playedScissors( wxCommandEvent& ) { choicePlayer = 3; hasPlayed = true; }
void FenRPS::newGame( wxCommandEvent& )
{
stTextBox->SetLabel( "\nThe game is starting..." );
}
You need to move the game playing code out of the wxFrame
constructor and into the newGame()
event handler.
The wxFrame
constructor is called once, when creating the window (frame). The primary purpose of this constructor is to place all the widgets and initialize them.
When the User selects "New Game" from the Menu, the Menu object sends out an event. Your newGame()
method is set up to receive this event. Thus, your program should have code in this method to create and and start a new game.