我创建了一个俄罗斯方块游戏,在游戏结束后可以重新开始。我使用goto语句实现了这个功能(请参见代码)。Game
类依赖析构函数,这些析构函数是否会在使用goto语句时被调用?这个goto语句有多糟糕?它是可以接受的吗?还是应该用其他方法代替?
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// initiate sdl
sdl_init();
// seed rng
srand(time(NULL));
newgame: // new game label
Game game(GAME_WIDTH, GAME_HEIGHT, 1, screen);
// keydowns
bool fastfall = false;
bool gamerunning = true;
Uint32 lastupdate = 0;
while (gamerunning && game.isalive()) {
// game running stuff here
}
// game over stuff here
while (gamerunning) {
if (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
gamerunning = false;
} else if (event.type == SDL_KEYDOWN) {
if (event.key.keysym.sym == SDLK_r) goto newgame; // yay a new game!
}
}
}
TTF_Quit();
SDL_Quit();
return 0;
}