如何修复我的非启动小游戏应用程序?

How to fix my non-starting little game app?

我必须为我的学校做一个小游戏,我被困在我的程序中。当我启动该应用程序时它工作正常,但是当我想通过菜单栏开始新游戏时,它说游戏正在开始,但实际上并没有。我认为该程序卡在我的 FenRPS::newGame() 函数中,但我不知道如何修复它。

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..." );
}

您需要将游戏代码从 wxFrame 构造函数中移出并移至 newGame() 事件处理程序中。

wxFrame 构造函数在创建 window(框架)时被调用一次。此构造函数的主要目的是放置所有小部件并对其进行初始化。

当用户从 Menu 中选择 "New Game" 时,Menu 对象发出一个事件。您的 newGame() 方法已设置为接收此事件。因此,您的程序应该包含此方法中的代码来创建和开始新游戏。