C++ 如何修复我的非启动小游戏应用程序?
我必须为我的学校做一个小游戏,我被困在我的程序中。当我启动应用程序时,它工作正常,但当我想通过菜单栏开始一个新游戏时,它会说游戏正在启动,但事实并非如此。我认为这个程序被困在我的FenRPS::newGame()函数中,但我不知道如何修复它C++ 如何修复我的非启动小游戏应用程序?,c++,wxwidgets,C++,Wxwidgets,我必须为我的学校做一个小游戏,我被困在我的程序中。当我启动应用程序时,它工作正常,但当我想通过菜单栏开始一个新游戏时,它会说游戏正在启动,但事实并非如此。我认为这个程序被困在我的FenRPS::newGame()函数中,但我不知道如何修复它 FenRPS::FenRPS() : wxFrame( NULL, wxID_ANY, "Rock–paper–scissors", wxDefaultPosition, wxSize(607,650), wxCAPTION|wxCLOSE_BOX|wxC
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..." );
}
FenRPS::FenRPS():wxFrame(NULL,wxID_ANY,“石头-布-剪刀”,
wxDefaultPosition、wxSize(607650)、wxCAPTION | wxCLOSE | wxCLIP |儿童)
{
此->背景色(wxColor(240240));
这->设置图标(wxIcon(appps::Icone_xpm));
//===================菜单================
wxMenuItem*项目;
#定义项(菜单、fctEvenement、文本、助手)\
项目=菜单->附加(wxID_ANY、texte、aide)\
菜单->绑定(wxEVT_菜单,fctEvenement,this,item->GetId());
#定义分隔符(菜单)菜单->追加分隔符();
menuGame=新的wxMenu;
项目(menuGame、newGame、“&New Game”、“创建新游戏”);
分离器(menuGame);
项目(菜单名,退出,“退出”,“退出游戏”);
menuAbout=新的wxMenu;
项目(menuAbout、about、“&about”、“显示应用程序信息”);
菜单栏=新的wxMenuBar;
菜单栏->附加(菜单名“&Game”);
菜单栏->附加(菜单栏“&About”);
此->设置菜单栏(菜单栏);
//==============================布顿==============
rock_png=新的wxStaticBitmap(这个,wxID_ANY,wxBitmap(“img/rock.png”),
wxBITMAP_TYPE_PNG),wxPoint(54400),wxSize(128128));
创建(this,wxID_ANY,“roc K”,wxPoint(54538),wxSize(128,50));
buttonRock.Bind(wxEVT_按钮,playedRock,此);
paper_png=新wxStaticBitmap(此,wxID_任意,
wxBitmap(“img/paper.png”,wxBitmap_TYPE_png),wxPoint(236400),wxSize(128128));
创建(this,wxID_ANY,“papper”,wxPoint(236538),wxSize(128,50));
buttonPaper.Bind(wxEVT_按钮,播放纸,本);
剪刀png=新wxStaticBitmap(此,wxID_ANY,
wxBitmap(“img/scissors.png”、wxBitmap_TYPE_png)、wxPoint(418400)、wxSize(128128));
创建(this,wxID_ANY,“S C I S S O R S”,wxPoint(418538),wxSize(128,50));
纽扣剪刀。绑定(wxEVT_纽扣,玩剪刀,这个);
stTextBox=新的wxStaticText;
stTextBox->Create(这个,wxID_ANY,“\n欢迎参加石头剪刀游戏\n\n\n没有游戏在进行中”,wxPoint(10,10),wxSize(580364),wxALIGN_中心水平);
stTextBox->setbackgroundcolor(*wxLIGHT_灰);
stTextBox->SetFont(wxFont(wxFontInfo(12).FaceName(“Arial”).Bold());
if(已播放)
{
srand(时间(0));
choiceBot=(rand()%3)+1;
消息您需要将游戏代码移出wxFrame
构造函数,并移入newGame()
事件处理程序
当创建窗口(框架)时,wxFrame
构造函数被调用一次。此构造函数的主要目的是放置所有小部件并初始化它们
当用户从菜单中选择“新游戏”时,菜单对象将发送一个事件
方法设置为接收此事件。因此,您的程序中应该有此方法中的代码来创建和启动新游戏。调试器。使用调试器。调试器将帮助您单步执行程序,并允许您查看变量中的值。通常,使用调试器比发送到StackOverflow并等待某个事件快我可以帮你检查或调试你的程序。如果你对调试器过敏,你可能想实现一个日志文件。打开一个文件并在程序中的不同点打印到它(用窗口程序打印到控制台有点困难)。您可能希望在日志消息前面加上时间戳。我建议使用嵌套的switch
语句。IMHO,使用switch
处理菜单选择更具可读性。易于阅读的程序缺陷更少。当用户选择菜单项newGame
时,框架将调用FenRPS::newGame
。YournewGame()
方法设置标签中的文本并返回,其他情况不会发生。可能的