相應(yīng)的游戲功能都需要放在 startup()、show()、updateWithoutInput()、updateWithInput()
幾個函數(shù)中實現(xiàn),主函數(shù)盡量保持以上形式,不要修改。我們利用函數(shù)封裝重構(gòu)飛機游戲,并實現(xiàn)新式子彈、敵機移動、擊中敵機效果和刷新和更好的清屏功能。
int main()
{startup(); //初始化
while(1) //游戲循環(huán)執(zhí)行
{show(); //顯示游戲畫面
updateWithoutInput(); //與用戶輸入無關(guān)的更新
updateWithInput(); //與用戶輸入有關(guān)的更新
}
return 0;
}
二、新式子彈用會動的子彈代替激光
#include#include#include//全局變量
int position_x,position_y; // 飛機位置
int high,width; // 游戲畫面尺寸
int bullet_x,bullet_y; // 子彈位置
void startup()// 數(shù)據(jù)初始化
{high=20;
width=30;
position_x=high/2;
position_y=width/2;
bullet_x=0;
bullet_y=position_y;
}
void show()
{system("cls");
int i,j;
for(i=0;ifor(j=0;j if((i==position_x)&&(j==position_y))
printf("*"); //輸出飛機*
else if((i==bullet_x)&&(j==bullet_y))
printf("|"); //輸出子彈
else
printf(" "); //輸出空格
}
printf("\n");
}
}
void updateWithoutInput()
{if(bullet_x>-1)
bullet_x--;
}
void updateWithInput() // 與用戶輸入有關(guān)的更新
{char input;
if(kbhit()) // 判斷是否有輸入
{input=getch(); //根據(jù)用戶的不同輸入來移動,不必輸入回車
if(input=='a')
position_y--; // 位置左移
if(input=='d')
position_y++; // 位置右移
if(input=='w')
position_x--; // 位置上移
if(input=='s')
position_x++; // 位置下移
if(input==' ')
bullet_x=position_x-1; // 發(fā)射子彈的初始位置在飛機的正上方
bullet_y=position_y;
}
}
int main()
{startup(); //初始化
while(1) //游戲循環(huán)執(zhí)行
{show(); //顯示游戲畫面
updateWithoutInput(); //與用戶輸入無關(guān)的更新
updateWithInput(); //與用戶輸入有關(guān)的更新
}
return 0;
}
三、加入敵機增加靜止的敵機@,坐標(biāo)(enemy_x,enemy_y),讓敵機自動向下移動(enemy_x++;)。為了在降低敵機移動速度的同時不影響用戶輸入響應(yīng)的頻率,代碼中用了一個小技巧,即在updateWithoutInput()函數(shù)中利用靜態(tài)變量speed,每執(zhí)行10次updateWithoutInput 函數(shù)敵機才移動一次。
四、積分功能增加判斷,當(dāng)子彈和敵機的位置相同時就是擊中敵機。增加變量score記錄游戲得分,擊中敵機后score++。敵機被擊中后會先消失,然后重新在隨機位置出現(xiàn),其中rand()函數(shù)產(chǎn)生一個隨機整數(shù),rand()%10即產(chǎn)生0~9的一個隨機整數(shù)。
#include#include#include//全局變量
int position_x,position_y;// 飛機位置
int high,width; // 游戲畫面尺寸
int bullet_x,bullet_y; // 子彈位置
int enemy_x,enemy_y; //敵人
int score; // 得分
void startup() // 數(shù)據(jù)初始化
{high=20;
width=30;
position_x=high/2;
position_y=width/2;
bullet_x=0;
bullet_y=position_y;
enemy_x=0;
enemy_y=position_y;
score =0;
}
void show()
{system("cls");
int i,j;
for(i=0;ifor(j=0;j if((i==position_x)&&(j==position_y))
printf("*"); //輸出飛機*
else if((i==bullet_x)&&(j==bullet_y))
printf("|"); //輸出子彈
else if((i==enemy_x)&&(j==enemy_y))
printf("@"); //輸出敵人@
else
printf(" "); //輸出空格
}
printf("\n");
}
printf("得分:%d\n",score);
}
void updateWithoutInput()
{if(bullet_x>-1)
bullet_x--;
if((bullet_x==enemy_x)&&(bullet_y==enemy_y))
{score++; // 分?jǐn)?shù)加1
enemy_x=-1; // 產(chǎn)生新的飛機
enemy_y=rand()%width;
bullet_x=-2; // 子彈無效
}
if(enemy_x>high) // 敵機跑出顯示屏幕
{enemy_x=-1; // 產(chǎn)生新的飛機
enemy_y=rand()%width;
}
// 用來控制敵機向下移動的速度。每隔10次循環(huán),才移動一次敵機
// 這樣修改的話,用戶按鍵交互速度還是保持很快,但我們NPC的移動顯示可以降速
static int speed=0;
if(speed<10)
speed++;
if(speed==10)
{enemy_x++;
speed=0;
}
}
void updateWithInput() // 與用戶輸入有關(guān)的更新
{char input;
if(kbhit()) // 判斷是否有輸入
{input=getch(); //根據(jù)用戶的不同輸入來移動,不必輸入回車
if(input=='a')
position_y--; // 位置左移
if(input=='d')
position_y++; // 位置右移
if(input=='w')
position_x--; // 位置上移
if(input=='s')
position_x++; // 位置下移
if(input==' ')
bullet_x=position_x-1; // 發(fā)射子彈的初始位置在飛機的正上方
bullet_y=position_y;
}
}
int main()
{startup(); //初始化
while(1) //游戲循環(huán)執(zhí)行
{show(); //顯示游戲畫面
updateWithoutInput(); //與用戶輸入無關(guān)的更新
updateWithInput(); //與用戶輸入有關(guān)的更新
}
return 0;
}
五、解決閃爍和閃動的光標(biāo)目前飛機游戲畫面的閃爍嚴(yán)重,第五步介紹新的清屏方法。利用 void gotoxy(int x,int y)函數(shù)(#include
清屏方法
#includevoid gotoxy(int x,int y) //光標(biāo)移動到(x,y)位置
{HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void show() // 顯示畫面
{gotoxy(0,0); // 光標(biāo)移動到原點位置,以下重畫清屏
、、、、
}
解決光標(biāo)閃爍
#include#includevoid HideCursor() // 用于隱藏光標(biāo)
{CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二個值為0表示隱藏光標(biāo)
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
int main()
{HideCursor();
return 0;
}
六、最終成品組合以上代碼
#include#include#include#include//全局變量
int position_x,position_y; // 飛機位置
int high,width; // 游戲畫面尺寸
int bullet_x,bullet_y; // 子彈位置
int enemy_x,enemy_y; //敵人
int score; // 得分
void startup() // 數(shù)據(jù)初始化
{high=20;
width=30;
position_x=high/2;
position_y=width/2;
bullet_x=0;
bullet_y=position_y;
enemy_x=0;
enemy_y=position_y;
score =0;
}
void gotoxy(int x,int y) //光標(biāo)移動到(x,y)位置
{HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(handle,pos);
}
void HideCursor() // 用于隱藏光標(biāo)
{CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二個值為0表示隱藏光標(biāo)
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void show()
{gotoxy(0,0); // 光標(biāo)移動到原點位置,以下重畫清屏
int i,j;
for(i=0;ifor(j=0;j if((i==position_x)&&(j==position_y))
printf("*"); //輸出飛機*
else if((i==bullet_x)&&(j==bullet_y))
printf("|"); //輸出子彈
else if((i==enemy_x)&&(j==enemy_y))
printf("@"); //輸出敵人@
else
printf(" "); //輸出空格
}
printf("\n");
}
printf("得分:%d\n",score);
}
void updateWithoutInput()
{if(bullet_x>-1)
bullet_x--;
if((bullet_x==enemy_x)&&(bullet_y==enemy_y))
{score++; // 分?jǐn)?shù)加1
enemy_x=-1; // 產(chǎn)生新的飛機
enemy_y=rand()%width;
bullet_x=-2; // 子彈無效
}
if(enemy_x>high) // 敵機跑出顯示屏幕
{enemy_x=-1; // 產(chǎn)生新的飛機
enemy_y=rand()%width;
}
// 用來控制敵機向下移動的速度。每隔10次循環(huán),才移動一次敵機
// 這樣修改的話,用戶按鍵交互速度還是保持很快,但我們NPC的移動顯示可以降速
static int speed=0;
if(speed<10)
speed++;
if(speed==10)
{enemy_x++;
speed=0;
}
}
void updateWithInput() // 與用戶輸入有關(guān)的更新
{char input;
if(kbhit()) // 判斷是否有輸入
{input=getch(); //根據(jù)用戶的不同輸入來移動,不必輸入回車
if(input=='a')
position_y--; // 位置左移
if(input=='d')
position_y++; // 位置右移
if(input=='w')
position_x--; // 位置上移
if(input=='s')
position_x++; // 位置下移
if(input==' ')
bullet_x=position_x-1; // 發(fā)射子彈的初始位置在飛機的正上方
bullet_y=position_y;
}
}
int main()
{startup(); //初始化
while(1) //游戲循環(huán)執(zhí)行
{show(); //顯示游戲畫面
updateWithoutInput(); //與用戶輸入無關(guān)的更新
updateWithInput(); //與用戶輸入有關(guān)的更新
HideCursor();
}
return 0;
}
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
分享標(biāo)題:【C語言游戲開發(fā)】進階飛機小游戲-創(chuàng)新互聯(lián)
URL標(biāo)題:http://muchs.cn/article16/dhsjgg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站策劃、域名注冊、網(wǎng)站收錄、小程序開發(fā)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容