嗨,我正在制作蛇游戲,它幾乎完成了,但我想添加重新開始游戲的重玩按鈕,我不知道有什么想法?
主檔案:
#include "SourceCode&Setup.h" //Game source code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <math.h>
void main()
{
setup();
while(GameOver!=1)
{
DrawBorder();
Input();
Movement();
}
}
uj5u.com熱心網友回復:
你可以簡單地將你的游戲玩法放入一個do {} while()回圈中:
#include "SourceCode&Setup.h" //Game source code
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <windows.h>
#include <ctype.h>
#include <math.h>
int main(void)
{
char again;
do
{
setup();
while(GameOver!=1)
{
DrawBorder();
Input();
Movement();
}
printf("Play again (y/n)?\n");
int ret = scanf(" %c", &again);
if (ret != 1)
again = 'n'; // don't play again in case of error.
} while (tolower(again)=='y');
}
根據setup具體情況,您可能需要對其進行調整以使其能夠被多次呼叫。
uj5u.com熱心網友回復:
在高層次上,我建議如下:
- 創建一個
initializeGame()函式,將游戲期間使用的所有變數設定為其默認值。例如將分數設定為 0 - 用于
getchar()完成游戲后等待按鍵。這會暫停程式,直到用戶按下控制臺中的某個鍵。如果輸入字符是'n'那么exit() - 在您的
main()函式中:創建另一個 while(true) 回圈,在每次播放后重置游戲狀態
uj5u.com熱心網友回復:
LABLE:
while(GameOver!=1)
{
DrawBorder();
Input();
Movement();
cout<<"Press one to play again:";
cin>>x;
if(x==1)
{
goto LABLE;
}
else
break;
}
uj5u.com熱心網友回復:
將您的游戲邏輯包裝在一個do-while回圈中:
int main( void )
{
do
{
setup();
while(GameOver!=1)
{
DrawBorder();
Input();
Movement();
}
} while ( playAnotherGame() );
}
playAnotherGame()類似的東西在哪里:
#include <ctype.h>
...
int playAnotherGame( void )
{
fputs( "Play another game? [Y/N]: ", stdout );
int c;
/**
* Read individual characters from standard input until
* we see a non-whitespace character
*/
while ( isspace( c = getchar() ) )
;
/**
* Return true if the input character is 'Y' or 'y',
* false otherwise.
*/
return tolower( c ) == 'y';
}
一些注意事項:
- 有兩個標準簽名
main:
和int main( void )
(或同等學歷)。如果您的代碼不采用命令列引數,請使用第一種形式。int main( int argc, char **argv )除非您的實作明確記錄
void main()為有效簽名,否則使用它將呼叫未定義的行為- 您的代碼可能會按預期作業,它可能會徹底崩潰,或者介于兩者之間。 .h檔案應該只包含宏定義、型別定義、函式宣告等。它們不應該包含任何函式定義或陳述句。將您的實作代碼放在單獨.c編譯和鏈接的單獨檔案中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/516686.html
標籤:C
上一篇:輸入無法在C中計算和列印
