【发布时间】:2019-09-26 12:08:52
【问题描述】:
我必须为我的学校做一个小游戏,但我被困在我的程序中。当我启动该应用程序时,它运行良好,但是当我想通过菜单栏开始一个新游戏时,它说游戏正在开始,但事实并非如此。我认为程序卡在我的 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..." );
}
【问题讨论】:
-
调试器。使用调试器。调试器将帮助您逐步完成程序并允许您观察变量中的值。通常,使用调试器比发布到 StackOverflow 并等待有人为您检查或调试程序要快。
-
如果您对调试器过敏,您可能需要实现一个日志文件。打开一个文件并在程序的各个点打印到它(使用窗口程序打印到控制台有点困难)。您可能希望在日志消息前加上时间戳。
-
我推荐使用嵌套的
switch语句。恕我直言,使用switch处理菜单选择更具可读性。易于阅读的程序具有较少的缺陷。 -
当用户选择菜单项
New Game时,框架调用FenRPS::newGame。您的newGame()方法在标签中设置文本并返回,没有其他事情发生。