C語言用封裝方法實現(xiàn)飛機大戰(zhàn)游戲
本文實例為大家分享了C語言用封裝方法實現(xiàn)飛機大戰(zhàn)游戲的具體代碼,供大家參考,具體內(nèi)容如下
這是上一次的飛機大戰(zhàn)游戲的項目。項目: 最簡單的飛機大戰(zhàn)游戲
上次沒有用函數(shù)進行的封裝。這次在上次的基礎(chǔ)上進行封裝和一些功能的優(yōu)化。
一、項目描述和最終的成果展示
項目描述: 在上一次的基礎(chǔ)上用函數(shù)進行了封裝,對于一些功能也進行了一些優(yōu)化。
最終效果圖如下:
二、用函數(shù)進行封裝
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h> //全局變量 int position_x,position_y;//飛機位置 int high,width;//游戲畫面尺寸 void startup()//數(shù)據(jù)的初始化 { ?? ?high = 20; ?? ?width = 30; ?? ?position_x = high/2;//飛機的上下位置 ?? ?position_y = width/2;//飛機的左右·位置 } void show()//顯示畫面 { ?? ?system("cls"); ?? ?int i,j; ?? ?for(i=0;i<high;i++) ?? ?{ ?? ??? ?for(j=0;j<width;j++) ?? ??? ?{ ?? ??? ??? ?if( (i == position_x) && (j== position_y))//輸出飛機 ?? ??? ??? ??? ?printf("☆"); ?? ??? ??? ?else ?? ??? ??? ??? ?printf(" "); ?? ??? ?} ? ? ? ? printf("\n"); ?? ?} } void updateWithoutInput()//與用戶輸入無關(guān)的更新 { } 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++;//下移 ?? ?} } int main(void) { ?? ?startup(); ?//數(shù)據(jù)的初始化 ?? ?while(1) ?? ?{ ?? ??? ?show();//顯示畫面 ?? ? ? ?updateWithoutInput();//與用戶輸入無關(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;//飛機位置 int high,width;//游戲畫面尺寸 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;//飛機的上下位置 ?? ?position_y = width/2;//飛機的左右·位置 ?? ?bullet_x = 0; ?? ?bullet_y = position_y; } void show()//顯示畫面 { ?? ?system("cls"); ?? ?int i,j; ?? ?for(i=0;i<high;i++) ?? ?{ ?? ??? ?for(j=0;j<width;j++) ?? ??? ?{ ?? ??? ??? ?if( (i == position_x) && (j== position_y))//輸出飛機 ?? ??? ??? ??? ?printf("☆"); ?? ??? ??? ?else if( (i == bullet_x)&&(j == bullet_y)) ?? ??? ??? ??? ?printf("|");//輸出子彈 ?? ??? ??? ?else ?? ??? ??? ??? ?printf(" "); ?? ??? ?} ? ? ? ? printf("\n"); ?? ?} } void updateWithoutInput()//與用戶輸入無關(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();//顯示畫面 ?? ??? ?HideCursor();//隱藏光標(biāo),防止光標(biāo)亂閃。 ?? ? ? ?updateWithoutInput();//與用戶輸入無關(guān)的更新 ?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新 ?? ?} ?? ?return 0; }
效果圖如下:
發(fā)射子彈的功能和上次有了明顯的改進,有了一個動態(tài)發(fā)射的一個效果。
四、實現(xiàn)移動的敵機功能和更正屏幕閃爍,清除光標(biāo)功能
代碼如下;
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h> //全局變量 int position_x,position_y;//飛機位置 int high,width;//游戲畫面尺寸 int bullet_x,bullet_y;//子彈位置 int enemy_x,enemy_y;//敵機的位置 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)移動到(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;//飛機的上下位置 ?? ?position_y = width/2;//飛機的左右位置 ?? ?bullet_x = 0; ?? ?bullet_y = position_y; ?? ?enemy_x=0; ?? ?enemy_y=position_y; ?? ?score=0; } void show()//顯示畫面 { ?? ?//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))//輸出飛機 ?? ??? ??? ??? ?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()//與用戶輸入無關(guān)的更新 { ?? ?static int speed=0; ?? ?if(bullet_x>-1) ?? ??? ?bullet_x--; ?? ? if( (bullet_x == enemy_x) && (bullet_y ==enemy_y) )//子彈擊中飛機 ?? ? { ?? ??? ? score++;//分數(shù)無效 ?? ??? ? enemy_x=-1;//產(chǎn)生新的敵機 ?? ??? ? enemy_y=rand()%width; ?? ??? ? bullet_x=-2;//子彈無效 ?? ? } ?? ?// 用來控制敵機向下移動的速度,每隔幾次循環(huán)才移動一次敵機 ?? ?// 這樣修改,雖然用戶按鍵的交互速度還是很快,但是NPC的移動顯示可以降速 ?? ?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();//顯示畫面 ?? ??? ?HideCursor();//隱藏光標(biāo),防止光標(biāo)亂閃。 ?? ? ? ?updateWithoutInput();//與用戶輸入無關(guān)的更新 ?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新 ?? ?} ?? ?return 0; }
效果圖如下:
五、訂正一些BUG和完成一些美化
我們的項目基本是已經(jīng)完成了。但是還有很多的漏洞。
比如: 飛機控制越界問題,以及敵機越界問題。
而且界面不夠好看我們要再美化一下。
以及增加游戲暫停功能。
游戲結(jié)束功能。
代碼如下:
#include<stdio.h> #include<stdlib.h> #include<Windows.h> #include<conio.h> //全局變量 int position_x,position_y;//飛機位置 int high,width;//游戲畫面尺寸 int bullet_x,bullet_y;//子彈位置 int enemy_x,enemy_y;//敵機的位置 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)移動到(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;//飛機的上下位置 ?? ?position_y = width/2;//飛機的左右位置 ?? ?bullet_x = 0; ?? ?bullet_y = position_y; ?? ?enemy_x=0; ?? ?enemy_y=position_y; ?? ?score=0; } void show()//顯示畫面 { ?? ?//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))//輸出飛機 ?? ??? ??? ??? ?printf("☆"); ?? ??? ??? ?else if( (i == bullet_x)&&(j == bullet_y)) ?? ??? ??? ??? ?printf("|");//輸出子彈 ?? ??? ??? ?else if( (i== enemy_x) && ( j==enemy_y)) ?? ??? ??? ??? ?printf("*");//輸出敵機 ?? ??? ??? ?else if(j==width-1&&i==position_x) ?? ??? ??? ??? ?//飛機那一行,因為有飛機多占一格,所以要刪除前面的一個空格 ?? ??? ??? ??? ?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()//與用戶輸入無關(guān)的更新 { ?? ?static int speed=0; ?? ?if(bullet_x>-1) ?? ??? ?bullet_x--; ?? ? if( (bullet_x == enemy_x) && (bullet_y ==enemy_y) )//子彈擊中飛機 ?? ? { ?? ??? ? score++;//分數(shù)無效 ?? ??? ? enemy_x=-1;//產(chǎn)生新的敵機 ?? ??? ? enemy_y=rand()%width+1; ?? ??? ? bullet_x=-2;//子彈無效 ?? ? } ?? ?// 用來控制敵機向下移動的速度,每隔幾次循環(huán)才移動一次敵機 ?? ?// 這樣修改,雖然用戶按鍵的交互速度還是很快,但是NPC的移動顯示可以降速 ?? ?if(speed<6) ?? ??? ?speed++; ?? ?if(speed == 6 ) ?? ?{ ?? ??? ?enemy_x++; ?? ??? ?if(enemy_x==high-1)//如果飛機越界再次生成 ?? ??? ?{ ?? ??? ??? ?enemy_x=-1;//產(chǎn)生新的敵機 ?? ??? ??? ?enemy_y=rand()%width+1; ?? ??? ?} ?? ??? ?if( enemy_x==position_x-1)//撞機了 游戲結(jié)束 ?? ??? ?{ ?? ??? ??? ?system("cls"); ?? ??? ??? ?printf("飛機墜毀了,游戲結(jié)束\n"); ?? ??? ??? ?printf("分數(shù)為:%d\n",score); ?? ??? ??? ?printf("請重啟再開始新的一局\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();//顯示畫面 ?? ??? ?HideCursor();//隱藏光標(biāo),防止光標(biāo)亂閃。 ?? ? ? ?updateWithoutInput();//與用戶輸入無關(guān)的更新 ?? ??? ?updateWithInput();//與用戶輸入有關(guān)的更新 ?? ?} ?? ?return 0; }
效果圖如下:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言函數(shù)指針與回調(diào)函數(shù)的實現(xiàn)
本文主要介紹了C語言函數(shù)指針與回調(diào)函數(shù)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05c/c++ 標(biāo)準庫 bind 函數(shù)詳解
bind是一組用于函數(shù)綁定的模板。在對某個函數(shù)進行綁定時,可以指定部分參數(shù)或全部參數(shù),也可以不指定任何參數(shù),還可以調(diào)整各個參數(shù)間的順序。這篇文章主要介紹了c/c++ 標(biāo)準庫 bind 函數(shù) ,需要的朋友可以參考下2018-09-09C++利用opencv實現(xiàn)單目測距的實現(xiàn)示例
本文主要介紹了C++利用opencv實現(xiàn)單目測距的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11解析c中stdout與stderr容易忽視的一些細節(jié)
本篇文章是對在c語言中stdout與stderr容易忽視的一些細節(jié)進行了詳細的分析介紹,需要的朋友參考下2013-05-05C語言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹實例詳解
這篇文章主要介紹了C語言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹實例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01