如何修复我的非入门小游戏应用程序?

时间:2019-05-08 17:28:35

标签: c++ wxwidgets

我必须为我的学校做一些游戏,但我陷入了自己的计划。当我启动该应用程序时,它运行良好,但是当我想通过菜单栏启动一个新游戏时,它表示该游戏正在启动,但不是。我认为该程序卡在了我的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..." );
}

1 个答案:

答案 0 :(得分:0)

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

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

当用户从菜单中选择“新游戏”时,菜单对象发出事件。您的newGame()方法已设置为接收此事件。因此,您的程序应在此方法中包含代码以创建和启动新游戏。