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++實(shí)現(xiàn)四則運(yùn)算器(無(wú)括號(hào))
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)四則運(yùn)算器,無(wú)括號(hào)的計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
C++中g(shù)etline()、gets()等函數(shù)的用法詳解
這篇文章主要介紹了C++中g(shù)etline()、gets()等函數(shù)的用法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
C++高性能服務(wù)器框架之協(xié)程調(diào)度模塊
這篇文章主要介紹了C++高性能服務(wù)器框架中的協(xié)程調(diào)度模塊,文中通過(guò)代碼示例介紹的非常詳細(xì),對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-06-06
Linux搭建C++開(kāi)發(fā)調(diào)試環(huán)境的方法步驟
這篇文章主要介紹了Linux搭建C++開(kāi)發(fā)調(diào)試環(huán)境的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
C++實(shí)現(xiàn)打地鼠游戲設(shè)計(jì)
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)打地鼠游戲設(shè)計(jì),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
CMake 生成靜態(tài)庫(kù)與動(dòng)態(tài)庫(kù)的方法步驟
本文主要介紹了CMake 生成靜態(tài)庫(kù)與動(dòng)態(tài)庫(kù)的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
C++ Primer 標(biāo)準(zhǔn)庫(kù)vector示例詳解
該文章主要介紹了C++標(biāo)準(zhǔn)庫(kù)中的vector類型,包括其定義、初始化、成員函數(shù)以及常見(jiàn)操作,文章詳細(xì)解釋了如何使用vector來(lái)存儲(chǔ)和操作對(duì)象集合,并提供了代碼示例來(lái)說(shuō)明vector的使用方法,感興趣的朋友一起看看吧2025-03-03

