C語(yǔ)言用封裝方法實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲
本文實(shí)例為大家分享了C語(yǔ)言用封裝方法實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲的具體代碼,供大家參考,具體內(nèi)容如下
這是上一次的飛機(jī)大戰(zhàn)游戲的項(xiàng)目。項(xiàng)目: 最簡(jiǎn)單的飛機(jī)大戰(zhàn)游戲
上次沒(méi)有用函數(shù)進(jìn)行的封裝。這次在上次的基礎(chǔ)上進(jìn)行封裝和一些功能的優(yōu)化。
一、項(xiàng)目描述和最終的成果展示
項(xiàng)目描述: 在上一次的基礎(chǔ)上用函數(shù)進(jìn)行了封裝,對(duì)于一些功能也進(jìn)行了一些優(yōu)化。
最終效果圖如下:
二、用函數(shù)進(jìn)行封裝
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h> //全局變量 int position_x,position_y;//飛機(jī)位置 int high,width;//游戲畫(huà)面尺寸 void startup()//數(shù)據(jù)的初始化 { ?? ?high = 20; ?? ?width = 30; ?? ?position_x = high/2;//飛機(jī)的上下位置 ?? ?position_y = width/2;//飛機(jī)的左右·位置 } void show()//顯示畫(huà)面 { ?? ?system("cls"); ?? ?int i,j; ?? ?for(i=0;i<high;i++) ?? ?{ ?? ??? ?for(j=0;j<width;j++) ?? ??? ?{ ?? ??? ??? ?if( (i == position_x) && (j== position_y))//輸出飛機(jī) ?? ??? ??? ??? ?printf("☆"); ?? ??? ??? ?else ?? ??? ??? ??? ?printf(" "); ?? ??? ?} ? ? ? ? printf("\n"); ?? ?} } void updateWithoutInput()//與用戶輸入無(wú)關(guān)的更新 { } void updateWithInput()//與用戶輸入有關(guān)的更新 { ?? ?char input; ?? ?if(kbhit())//判斷有無(wú)輸入 ?? ?{ ?? ??? ?input=getch(); ?? ??? ?if( input == 'a' || input == 'A') ?? ??? ??? ?position_y--;//左移 ?? ??? ?if( input == 'd' || input == 'D') ?? ??? ??? ?position_y++;//右移 ?? ??? ?if( input == 'w' || input == 'W') ?? ??? ??? ?position_x--;//上移 ?? ??? ?if( input == 's' || input == 'S') ?? ??? ??? ?position_x++;//下移 ?? ?} } int main(void) { ?? ?startup(); ?//數(shù)據(jù)的初始化 ?? ?while(1) ?? ?{ ?? ??? ?show();//顯示畫(huà)面 ?? ? ? ?updateWithoutInput();//與用戶輸入無(wú)關(guān)的更新 ?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新 ?? ?} ?? ?return 0; }
三、新型的發(fā)射子彈功能
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h> //全局變量 int position_x,position_y;//飛機(jī)位置 int high,width;//游戲畫(huà)面尺寸 int bullet_x,bullet_y;//子彈位置 //定義隱藏光標(biāo)函數(shù) void HideCursor() { ?? ?CONSOLE_CURSOR_INFO cursor; ? ? ?? ?cursor.bVisible = FALSE; ? ? ?? ?cursor.dwSize = sizeof(cursor); ? ? ?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); ? ? ?? ?SetConsoleCursorInfo(handle, &cursor); } void startup()//數(shù)據(jù)的初始化 { ?? ?high = 120; ?? ?width = 100; ?? ?position_x = high/2;//飛機(jī)的上下位置 ?? ?position_y = width/2;//飛機(jī)的左右·位置 ?? ?bullet_x = 0; ?? ?bullet_y = position_y; } void show()//顯示畫(huà)面 { ?? ?system("cls"); ?? ?int i,j; ?? ?for(i=0;i<high;i++) ?? ?{ ?? ??? ?for(j=0;j<width;j++) ?? ??? ?{ ?? ??? ??? ?if( (i == position_x) && (j== position_y))//輸出飛機(jī) ?? ??? ??? ??? ?printf("☆"); ?? ??? ??? ?else if( (i == bullet_x)&&(j == bullet_y)) ?? ??? ??? ??? ?printf("|");//輸出子彈 ?? ??? ??? ?else ?? ??? ??? ??? ?printf(" "); ?? ??? ?} ? ? ? ? printf("\n"); ?? ?} } void updateWithoutInput()//與用戶輸入無(wú)關(guān)的更新 { ?? ?if(bullet_x>-1) ?? ??? ?bullet_x--; } void updateWithInput()//與用戶輸入有關(guān)的更新 { ?? ?char input; ?? ?if(kbhit()) ?? ?{ ?? ??? ?input=getch(); ?? ??? ?if( input == 'a' || input == 'A') ?? ??? ??? ?position_y--;//左移 ?? ??? ?if( input == 'd' || input == 'D') ?? ??? ??? ?position_y++;//右移 ?? ??? ?if( input == 'w' || input == 'W') ?? ??? ??? ?position_x--;//上移 ?? ??? ?if( input == 's' || input == 'S') ?? ??? ??? ?position_x++;//下移 ?? ??? ?if( input == ' ') ?? ??? ?{ ?? ??? ??? ?bullet_x=position_x-1; ?? ??? ??? ?bullet_y=position_y; ?? ??? ?} ?? ?} } int main(void) { ?? ?startup();//數(shù)據(jù)的初始化 ?? ?while(1) ?? ?{ ?? ??? ?show();//顯示畫(huà)面 ?? ??? ?HideCursor();//隱藏光標(biāo),防止光標(biāo)亂閃。 ?? ? ? ?updateWithoutInput();//與用戶輸入無(wú)關(guān)的更新 ?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新 ?? ?} ?? ?return 0; }
效果圖如下:
發(fā)射子彈的功能和上次有了明顯的改進(jìn),有了一個(gè)動(dòng)態(tài)發(fā)射的一個(gè)效果。
四、實(shí)現(xiàn)移動(dòng)的敵機(jī)功能和更正屏幕閃爍,清除光標(biāo)功能
代碼如下;
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h> //全局變量 int position_x,position_y;//飛機(jī)位置 int high,width;//游戲畫(huà)面尺寸 int bullet_x,bullet_y;//子彈位置 int enemy_x,enemy_y;//敵機(jī)的位置 int score;//得分 //定義隱藏光標(biāo)函數(shù) void HideCursor() { ?? ?CONSOLE_CURSOR_INFO cursor; ? ? ?? ?cursor.bVisible = FALSE; ? ? ?? ?cursor.dwSize = sizeof(cursor); ? ? ?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); ? ? ?? ?SetConsoleCursorInfo(handle, &cursor); } void gotoxy(int x,int y)//將光標(biāo)移動(dòng)到(x,y)位置 { ?? ?HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE); ?? ?COORD pos; ?? ?pos.X = x; ?? ?pos.Y = y; ?? ?SetConsoleCursorPosition(handle,pos); } void startup()//數(shù)據(jù)的初始化 { ?? ?system("color 09"); ?? ?high = 30; ?? ?width =50; ?? ?position_x = high/2;//飛機(jī)的上下位置 ?? ?position_y = width/2;//飛機(jī)的左右位置 ?? ?bullet_x = 0; ?? ?bullet_y = position_y; ?? ?enemy_x=0; ?? ?enemy_y=position_y; ?? ?score=0; } void show()//顯示畫(huà)面 { ?? ?//system("cls"); ?? ?gotoxy(0,0); ?? ?int i,j; ?? ?for(i=0;i<high;i++) ?? ?{ ?? ??? ?for(j=0;j<width;j++) ?? ??? ?{ ?? ??? ??? ?if( (i == position_x) && (j== position_y))//輸出飛機(jī) ?? ??? ??? ??? ?printf("☆"); ?? ??? ??? ?else if( (i == bullet_x)&&(j == bullet_y)) ?? ??? ??? ??? ?printf("|");//輸出子彈 ?? ??? ??? ?else if( (i== enemy_x) && ( j==enemy_y)) ?? ??? ??? ??? ?printf("*");//輸出敵機(jī) ?? ??? ??? ?else ?? ??? ??? ??? ?printf(" ");//輸出空格 ?? ??? ?} ? ? ? ? printf("\n"); ?? ?} ?? ?printf("得分:%d\n",score); } void updateWithoutInput()//與用戶輸入無(wú)關(guān)的更新 { ?? ?static int speed=0; ?? ?if(bullet_x>-1) ?? ??? ?bullet_x--; ?? ? if( (bullet_x == enemy_x) && (bullet_y ==enemy_y) )//子彈擊中飛機(jī) ?? ? { ?? ??? ? score++;//分?jǐn)?shù)無(wú)效 ?? ??? ? enemy_x=-1;//產(chǎn)生新的敵機(jī) ?? ??? ? enemy_y=rand()%width; ?? ??? ? bullet_x=-2;//子彈無(wú)效 ?? ? } ?? ?// 用來(lái)控制敵機(jī)向下移動(dòng)的速度,每隔幾次循環(huán)才移動(dòng)一次敵機(jī) ?? ?// 這樣修改,雖然用戶按鍵的交互速度還是很快,但是NPC的移動(dòng)顯示可以降速 ?? ?if(speed<10) ?? ??? ?speed++; ?? ?if(speed == 10 ) ?? ?{ ?? ??? ?enemy_x++; ?? ??? ?speed = 0; ?? ?} } void updateWithInput()//與用戶輸入有關(guān)的更新 { ?? ?char input; ?? ?if(kbhit()) ?? ?{ ?? ??? ?input=getch(); ?? ??? ?if( input == 'a' || input == 'A') ?? ??? ??? ?position_y--;//左移 ?? ??? ?if( input == 'd' || input == 'D') ?? ??? ??? ?position_y++;//右移 ?? ??? ?if( input == 'w' || input == 'W') ?? ??? ??? ?position_x--;//上移 ?? ??? ?if( input == 's' || input == 'S') ?? ??? ??? ?position_x++;//下移 ?? ??? ?if( input == ' ') ?? ??? ?{ ?? ??? ??? ?bullet_x=position_x-1; ?? ??? ??? ?bullet_y=position_y; ?? ??? ?} ?? ?} } int main(void) { ?? ?startup();//數(shù)據(jù)的初始化 ?? ?while(1) ?? ?{ ?? ??? ?show();//顯示畫(huà)面 ?? ??? ?HideCursor();//隱藏光標(biāo),防止光標(biāo)亂閃。 ?? ? ? ?updateWithoutInput();//與用戶輸入無(wú)關(guān)的更新 ?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新 ?? ?} ?? ?return 0; }
效果圖如下:
五、訂正一些BUG和完成一些美化
我們的項(xiàng)目基本是已經(jīng)完成了。但是還有很多的漏洞。
比如: 飛機(jī)控制越界問(wèn)題,以及敵機(jī)越界問(wèn)題。
而且界面不夠好看我們要再美化一下。
以及增加游戲暫停功能。
游戲結(jié)束功能。
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h> //全局變量 int position_x,position_y;//飛機(jī)位置 int high,width;//游戲畫(huà)面尺寸 int bullet_x,bullet_y;//子彈位置 int enemy_x,enemy_y;//敵機(jī)的位置 int score;//得分 //定義隱藏光標(biāo)函數(shù) void HideCursor() { ?? ?CONSOLE_CURSOR_INFO cursor; ? ? ?? ?cursor.bVisible = FALSE; ? ? ?? ?cursor.dwSize = sizeof(cursor); ? ? ?? ?HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE); ? ? ?? ?SetConsoleCursorInfo(handle, &cursor); } void gotoxy(int x,int y)//將光標(biāo)移動(dòng)到(x,y)位置 { ?? ?HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE); ?? ?COORD pos; ?? ?pos.X = x; ?? ?pos.Y = y; ?? ?SetConsoleCursorPosition(handle,pos); } void startup()//數(shù)據(jù)的初始化 { ?? ?system("color 09"); ?? ?high = 30; ?? ?width =50; ?? ?position_x = high/2;//飛機(jī)的上下位置 ?? ?position_y = width/2;//飛機(jī)的左右位置 ?? ?bullet_x = 0; ?? ?bullet_y = position_y; ?? ?enemy_x=0; ?? ?enemy_y=position_y; ?? ?score=0; } void show()//顯示畫(huà)面 { ?? ?//system("cls"); ?? ?gotoxy(0,0); ?? ?int i,j; ?? ?for(i=0;i<high;i++) ?? ?{ ?? ??? ?for(j=0;j<width;j++) ?? ??? ?{ ?? ??? ??? ?if( (i == position_x) && (j== position_y))//輸出飛機(jī) ?? ??? ??? ??? ?printf("☆"); ?? ??? ??? ?else if( (i == bullet_x)&&(j == bullet_y)) ?? ??? ??? ??? ?printf("|");//輸出子彈 ?? ??? ??? ?else if( (i== enemy_x) && ( j==enemy_y)) ?? ??? ??? ??? ?printf("*");//輸出敵機(jī) ?? ??? ??? ?else if(j==width-1&&i==position_x) ?? ??? ??? ??? ?//飛機(jī)那一行,因?yàn)橛酗w機(jī)多占一格,所以要?jiǎng)h除前面的一個(gè)空格 ?? ??? ??? ??? ?printf("\b+"); ?? ??? ??? ?else if(j==width-1) ?? ??? ??? ??? ?printf("+"); ?? ??? ??? ?else if(i==high-1) ?? ??? ??? ??? ?printf("-"); ?? ??? ??? ?else ?? ??? ??? ??? ?printf(" ");//輸出空格 ?? ??? ?} ? ? ? ? printf("\n"); ?? ?} ?? ?printf("得分:%d\n",score); ?? ?printf("按1鍵游戲暫停\n"); } void updateWithoutInput()//與用戶輸入無(wú)關(guān)的更新 { ?? ?static int speed=0; ?? ?if(bullet_x>-1) ?? ??? ?bullet_x--; ?? ? if( (bullet_x == enemy_x) && (bullet_y ==enemy_y) )//子彈擊中飛機(jī) ?? ? { ?? ??? ? score++;//分?jǐn)?shù)無(wú)效 ?? ??? ? enemy_x=-1;//產(chǎn)生新的敵機(jī) ?? ??? ? enemy_y=rand()%width+1; ?? ??? ? bullet_x=-2;//子彈無(wú)效 ?? ? } ?? ?// 用來(lái)控制敵機(jī)向下移動(dòng)的速度,每隔幾次循環(huán)才移動(dòng)一次敵機(jī) ?? ?// 這樣修改,雖然用戶按鍵的交互速度還是很快,但是NPC的移動(dòng)顯示可以降速 ?? ?if(speed<6) ?? ??? ?speed++; ?? ?if(speed == 6 ) ?? ?{ ?? ??? ?enemy_x++; ?? ??? ?if(enemy_x==high-1)//如果飛機(jī)越界再次生成 ?? ??? ?{ ?? ??? ??? ?enemy_x=-1;//產(chǎn)生新的敵機(jī) ?? ??? ??? ?enemy_y=rand()%width+1; ?? ??? ?} ?? ??? ?if( enemy_x==position_x-1)//撞機(jī)了 游戲結(jié)束 ?? ??? ?{ ?? ??? ??? ?system("cls"); ?? ??? ??? ?printf("飛機(jī)墜毀了,游戲結(jié)束\n"); ?? ??? ??? ?printf("分?jǐn)?shù)為:%d\n",score); ?? ??? ??? ?printf("請(qǐng)重啟再開(kāi)始新的一局\n"); ?? ??? ??? ?while(1) ?? ??? ??? ?{ ?? ??? ??? ?} ?? ??? ?} ?? ??? ?speed = 0; ?? ?} } void updateWithInput()//與用戶輸入有關(guān)的更新 { ?? ?char input; ?? ?if(kbhit()) ?? ?{ ?? ??? ?input=getch(); ?? ??? ?if( input == 'a' || input == 'A') ?? ??? ?{ ?? ??? ??? ?position_y--;//左移 ?? ??? ??? ?if(position_y==0)//判斷是否越界 ?? ??? ??? ?{ ?? ??? ??? ??? ?position_y++; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if( input == 'd' || input == 'D') ?? ??? ?{ ?? ??? ??? ?position_y++;//右移 ?? ??? ??? ?if(position_y==width-2)//判斷是否越界 ?? ??? ??? ?{ ?? ??? ??? ??? ?position_y--; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if( input == 'w' || input == 'W') ?? ??? ?{ ?? ??? ??? ?position_x--;//上移 ?? ??? ??? ?if(position_x==1)//判斷是否越界 ?? ??? ??? ?{ ?? ??? ??? ??? ?position_x++; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if( input == 's' || input == 'S') ?? ??? ?{ ?? ??? ??? ?position_x++;//下移 ?? ??? ??? ?if(position_x==high-1)//判斷是否越界 ?? ??? ??? ?{ ?? ??? ??? ??? ?position_x--; ?? ??? ??? ?} ?? ??? ?} ?? ??? ?if( input == ' ')//發(fā)射子彈 ?? ??? ?{ ?? ??? ??? ?bullet_x=position_x-1; ?? ??? ??? ?bullet_y=position_y; ?? ??? ?} ?? ??? ?if( input == '1')//按1鍵游戲暫停 ?? ??? ?{ ?? ??? ??? ?while(1) ?? ??? ??? ?{ ?? ??? ??? ??? ?input=getch(); ?? ??? ??? ??? ?if(input == '1')//再按1鍵游戲繼續(xù) ?? ??? ??? ??? ??? ?break; ?? ??? ??? ?} ?? ??? ?} ?? ?} } int main(void) { ?? ?startup();//數(shù)據(jù)的初始化 ?? ?while(1) ?? ?{ ?? ??? ?show();//顯示畫(huà)面 ?? ??? ?HideCursor();//隱藏光標(biāo),防止光標(biāo)亂閃。 ?? ? ? ?updateWithoutInput();//與用戶輸入無(wú)關(guān)的更新 ?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新 ?? ?} ?? ?return 0; }
效果圖如下:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)
- C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)程序設(shè)計(jì)
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的飛機(jī)大戰(zhàn)游戲
- C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲完整代碼
- C語(yǔ)言控制臺(tái)實(shí)現(xiàn)字符飛機(jī)大戰(zhàn)
- C語(yǔ)言版飛機(jī)大戰(zhàn)游戲
- C語(yǔ)言代碼實(shí)現(xiàn)飛機(jī)大戰(zhàn)
- C語(yǔ)言之飛機(jī)大戰(zhàn)游戲
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單飛機(jī)大戰(zhàn)
- C語(yǔ)言實(shí)現(xiàn)飛機(jī)大戰(zhàn)小游戲
相關(guān)文章
C語(yǔ)言函數(shù)指針與回調(diào)函數(shù)的實(shí)現(xiàn)
本文主要介紹了C語(yǔ)言函數(shù)指針與回調(diào)函數(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù)詳解
bind是一組用于函數(shù)綁定的模板。在對(duì)某個(gè)函數(shù)進(jìn)行綁定時(shí),可以指定部分參數(shù)或全部參數(shù),也可以不指定任何參數(shù),還可以調(diào)整各個(gè)參數(shù)間的順序。這篇文章主要介紹了c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù) ,需要的朋友可以參考下2018-09-09C++利用opencv實(shí)現(xiàn)單目測(cè)距的實(shí)現(xiàn)示例
本文主要介紹了C++利用opencv實(shí)現(xiàn)單目測(cè)距的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11C語(yǔ)言內(nèi)嵌匯編API內(nèi)存搜索引擎實(shí)例
這篇文章主要介紹了C語(yǔ)言內(nèi)嵌匯編API內(nèi)存搜索引擎實(shí)例,涉及匯編語(yǔ)言與內(nèi)存相關(guān)操作,需要的朋友可以參考下2014-10-10解析c中stdout與stderr容易忽視的一些細(xì)節(jié)
本篇文章是對(duì)在c語(yǔ)言中stdout與stderr容易忽視的一些細(xì)節(jié)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C語(yǔ)言 90后懷舊游戲超級(jí)瑪麗的實(shí)現(xiàn)流程
90后最風(fēng)靡的游戲是什么?第一個(gè)聯(lián)想到的肯定是插卡游戲機(jī)或者VCD加光盤(pán)運(yùn)行在電視機(jī)上的超級(jí)瑪麗了,它的經(jīng)典絕對(duì)可以排在第一位,長(zhǎng)大后的我們今天來(lái)用C語(yǔ)言重溫一下2021-11-11C語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹(shù)實(shí)例詳解
這篇文章主要介紹了C語(yǔ)言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01