C++ c++;游戏帮助(简单代码)
我遇到了一些问题,我怀疑这是循环。我正在制作一个简单的游戏,我想我的两个while循环不知怎么地相互干扰了。这里是主要功能!提前谢谢!:C++ c++;游戏帮助(简单代码),c++,loops,C++,Loops,我遇到了一些问题,我怀疑这是循环。我正在制作一个简单的游戏,我想我的两个while循环不知怎么地相互干扰了。这里是主要功能!提前谢谢!: int main( int argc, char* argv[]) { SDL_Startup(); while(Playing == false && quit == false) { StartingScreen.input(); StartingScreen.render(); character.input()
int main( int argc, char* argv[])
{
SDL_Startup();
while(Playing == false && quit == false)
{
StartingScreen.input();
StartingScreen.render();
character.input();
SDL_Flip( screen );
SDL_Delay(1000/FPS);
}
while(Playing == true && quit == false)
{
CAMERAGUY.Camera();
character.input();
character.adjust();
SuperSnail.move();
SuperSnail.attack();
TheWall.boundaries();
TheWall.render();
SuperSnail.render();
character.render();
character.reset();
HUD.render();
SDL_Flip(screen);
SDL_Delay(1000/FPS);
cout << StartingScreen.x << endl;
}
if(Playing == false)
cout << "Playing == false" << endl;
if(quit == true)
return 0;
}
intmain(intargc,char*argv[])
{
SDL_启动();
while(播放==false&&quit==false)
{
StartingScreen.input();
StartingScreen.render();
字符输入();
SDL_翻转(屏幕);
SDL_延迟(1000/FPS);
}
同时(播放==true&&quit==false)
{
CAMERAGUY.Camera();
字符输入();
字符。调整();
SuperSnail.move();
SuperSnail.attack();
墙。边界();
TheWall.render();
SuperSnail.render();
character.render();
字符。重置();
渲染();
SDL_翻转(屏幕);
SDL_延迟(1000/FPS);
cout这非常简单。在当前的实现中,如果第二个循环完成,您将永远不会返回到第一个while循环,因为您将到达主例程的return
我想你应该在游戏中只使用一个主循环。我用注释编辑了你的代码,解释了它在做什么(以及为什么它没有按照你的预期运行):
intmain(intargc,char*argv[])
{
SDL_启动();
//将“Playing”设置为false
while(播放==false&&quit==false)
{
//当“Playing”为假时,做一些适用的事情
//
//在某个时刻,将“Playing”设置为true,以便
//循环结束
}
同时(播放==true&&quit==false)
{
//现在做一些当“玩”是真的时适用的事情
//
//在某个时刻,将“Playing”设置为false,以便
//循环结束
}
//这两个循环都结束了,就在那里
//没有返回到第一个循环的代码
//因此,执行过程如下:
//该行始终执行,因为“Playing”始终为false
//至此:
如果(播放==错误)
cout@alloyous没有帮助???哈哈,谢谢。但是在你的第三个技巧中,它说“以你自己的方式使用全局变量几乎肯定是个坏主意”。除了bools之外,我不显示任何全局变量。
int main( int argc, char* argv[])
{
SDL_Startup();
// Set `Playing` to false
while(Playing == false && quit == false)
{
// Do stuff which applies when `Playing` is false
//
// At some point, set `Playing` to true, so that
// the loop ends
}
while(Playing == true && quit == false)
{
// Now do stuff which applies when `Playing` is true
//
// At some point, set `Playing` to false, so that
// the loop ends
}
// Both loops have come to an end, and there
// is no code to return to the first loop
// so execution continues below:
// This line always executes because `Playing` is always false
// by this point:
if(Playing == false)
cout << "Playing == false" << endl;
// This conditional statement doesn't do what you think, because
// in the case that `quit == false`, the end of your `main` function
// is reached and returns 0 by default, meaning that the outcome
// is the same no matter what the value of `quit`
if(quit == true)
return 0;
}