C語言實(shí)現(xiàn)打飛機(jī)游戲
更新時(shí)間:2020年03月04日 09:14:26 作者:qq_42577465
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)打飛機(jī)游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)打飛機(jī)游戲的具體代碼,供大家參考,具體內(nèi)容如下
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define U 1
#define D 2
#define L 3
#define R 4
typedef struct node //己方飛機(jī)的節(jié)點(diǎn)
{
int x;
int y;
struct node *next;
}node;
typedef struct Bullet
{
int x;
int y;
int status; //0 1 表示此處是否有子彈
struct Bullet *next;
}Bullet; //子彈模型節(jié)點(diǎn)
typedef struct Enemy
{
int x;
int y;
int status; //0 1 表示此節(jié)點(diǎn)是否存在飛機(jī)
}Enemy; //敵機(jī)節(jié)點(diǎn)
// 全局變量//
node *head,*p;
Enemy Ehead[30];
Bullet *HBullet,*q,*H;
int Status,score=0;
int BulletStatus,s=1;
int add_1=1,add_2=0;
//函數(shù)聲明//
void Pos(); //固定光標(biāo)
void CreateMap(); //創(chuàng)建游戲界面
void CreateAircraft(); //創(chuàng)建自己的飛機(jī)
void printAircraft(); //打印自己的飛機(jī)
void Aircraftmove(); //自己飛機(jī)的移動
void CreateEnemyAircraftModel(); //創(chuàng)建敵機(jī)的模型 結(jié)構(gòu)體數(shù)組
void CreateEnemyAircraft(); //創(chuàng)建一架敵方飛機(jī)
void EnemyAircraftMove(); //敵機(jī)的移動
void CreateBulletModel(); //創(chuàng)建子彈的模型
void Shoot(); //發(fā)射子彈
void Bulletmove(); //子彈的移動
int GetBulletStatus(); //獲取是否有子彈存在
void JudgeCollide(); //判定是否擊中敵機(jī)
void JudgeGameOver(); //判定是否與敵機(jī)相撞
//為了防止在運(yùn)行別的函數(shù)時(shí)這個函數(shù)無法運(yùn)行,所以講起嵌入每一個循環(huán)的函數(shù)中,保證不遺漏任何情況.
void Pause(); //暫停
void ControlGame(); //控制游戲主程序
void GameStart(); //游戲開的的預(yù)先運(yùn)行函數(shù)集合
void EndGame(); //結(jié)束游戲
/*
■ 飛機(jī)的基本模型
■■■ 第一行為機(jī)頭, 敵機(jī)的設(shè)定只需用到機(jī)頭,機(jī)身僅僅只是打印出來,并沒有實(shí)際內(nèi)容
■■■ 下兩行為機(jī)身
*/
void Pos(int x,int y)//設(shè)置光標(biāo)位置
{
COORD pos;
pos.X=x;
pos.Y=y;
HANDLE hOutPut;
hOutPut=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutPut,pos);
}
void CreateMap()
{
system("mode con cols=100 lines=55");
int i;
for(i=0;i<=67;i+=2)//上下邊
{
Pos(i,0);
printf("■");
Pos(i,50);
printf("■");
}
for(i=0;i<=50;i++)//左右邊
{
Pos(0,i);
printf("■");
Pos(66,i);
printf("■");
}
Pos(80,8);
printf("您的得分%d",score);
Pos(70,20);
printf("Space:發(fā)射子彈");
Pos(70,22);
printf("ESC:退出游戲");
Pos(70,24);
printf("Enter:暫停");
Pos(70,26);
printf("用↑.↓.←.→控制飛機(jī)");
Pos(70,28);
printf("F1:加速敵機(jī)的產(chǎn)生");
Pos(70,30);
printf("F2:減緩敵機(jī)的產(chǎn)生");
Pos(80,40);
printf("特別說明:");
Pos(70,42);
printf("敵機(jī)從地圖下飛出不結(jié)束游戲");
}
void CreateAircraft()
{
node *q;
int i;
head=(node*)malloc(sizeof(node));//鏈表形式創(chuàng)建飛機(jī),x,y分別為顯示的坐標(biāo)
head->x=32;
head->y=47;
q=head;
for(i=0;i<=4;i+=2)
{
p=(node*)malloc(sizeof(node));
head->next=p;
p->x=30+i;
p->y=48;
head=p;
}
for(i=0;i<=4;i+=2)
{
p=(node*)malloc(sizeof(node));
head->next=p;
p->x=30+i;
p->y=49;
head=p;
}
p->next=NULL;
head=q;
p=head;
}
void printAircraft()
{
while(p!=NULL)
{
Pos(p->x,p->y);
printf("■");
p=p->next;
}
p=head;
}
void Aircraftmove()
{
JudgeGameOver();
JudgeCollide();
Sleep(50);
if(Status==U) //向上飛行
{
while (p!=NULL)
{
p->y=p->y-1;
Pos(p->x,p->y);
printf("■");
p=p->next;
}
p=head;
Pos(p->x-2,p->y+3);
printf(" ");
Pos(p->x,p->y+3);
printf(" ");
Pos(p->x+2,p->y+3);
printf(" ");
}
if(Status==D) //向下飛行
{
while (p!=NULL)
{
p->y=p->y+1;
Pos(p->x,p->y);
printf("■");
p=p->next;
}
p=head;
Pos(p->x,p->y-1);
printf(" ");
Pos(p->x-2,p->y);
printf(" ");
Pos(p->x+2,p->y);
printf(" ");
}
if(Status==L) //向左飛行
{
while (p!=NULL)
{
p->x=p->x-2;
Pos(p->x,p->y);
printf("■");
p=p->next;
}
p=head;
Pos(p->x+2,p->y);
printf(" ");
Pos(p->x+4,p->y+1);
printf(" ");
Pos(p->x+4,p->y+2);
printf(" ");
}
if(Status==R) //向右飛行
{
while (p!=NULL)
{
p->x=p->x+2;
Pos(p->x,p->y);
printf("■");
p=p->next;
}
p=head;
Pos(p->x-2,p->y);
printf(" ");
Pos(p->x-4,p->y+1);
printf(" ");
Pos(p->x-4,p->y+2);
printf(" ");
}
}
void CreateEnemyAircraftModel()
{
int j;
for(j=1;j<=30;j++) //結(jié)構(gòu)體數(shù)組表示敵軍飛機(jī)最多同時(shí)存在30架,也許不可能有那么多,數(shù)字用的稍大.
{ //每一個值表示一架飛機(jī),敵機(jī)只需知道其機(jī)頭坐標(biāo)
Ehead[j].x=-1;
Ehead[j].y=1;
Ehead[j].status=0;
}
}
void CreateEnemyAircraft()//創(chuàng)建出一架新的飛機(jī)
{
JudgeCollide();
JudgeGameOver();
srand((unsigned)time(NULL));
int j;
for(j=1;j<=20;j++)
{
if(Ehead[j].status==0)
{
JudgeGameOver();
while(Ehead[j].x%2!=0) //保證機(jī)頭x為偶數(shù),對其格子.
{
Ehead[j].x=rand()%62+3;
}
Ehead[j].y=1;
Ehead[j].status=1;
Pos((Ehead[j].x),(Ehead[j].y));
printf("■");
break;
}
}
}
void EnemyAircraftMove() //模擬敵機(jī)向下飛行的函數(shù)
{
Sleep(100);
if(s>=15) //控制敵機(jī)飛機(jī)的出現(xiàn)頻率
{
CreateEnemyAircraft();
s=1;
}
s=s+add_1;
JudgeCollide();
JudgeGameOver();
int j;
for(j=1;j<=30;j++)
{
if(Ehead[j].status==1 && Ehead[j].y==1) //特殊情況
{
Pos(Ehead[j].x,Ehead[j].y);
printf(" ");
}
if(Ehead[j].status==1 && Ehead[j].y==2) //特殊情況
{
Pos(Ehead[j].x,Ehead[j].y);
printf(" ");
Pos(Ehead[j].x-2,Ehead[j].y-1);
printf(" ");
}
if(Ehead[j].status==1 && 3<=Ehead[j].y && Ehead[j].y<=49)
{
Pos(Ehead[j].x,Ehead[j].y);
printf(" ");
Pos(Ehead[j].x-2,Ehead[j].y-1);
printf(" ");
Pos(Ehead[j].x-2,Ehead[j].y-2);
printf(" ");
}
if(Ehead[j].status==1 && Ehead[j].y==50)//特殊情況
{
Pos(Ehead[j].x-2,Ehead[j].y-1);
printf(" ");
Pos(Ehead[j].x-2,Ehead[j].y-2);
printf(" ");
}
if(Ehead[j].status==1 && Ehead[j].y==51)//特殊情況
{
Pos(Ehead[j].x-2,Ehead[j].y-2);
printf(" ");
}
}
for(j=1;j<=20;j++) //將每一個敵機(jī)機(jī)頭向下移動一位
{
if(Ehead[j].status==1)
{
if(Ehead[j].y<=52)
{
Ehead[j].y=Ehead[j].y+1;
}
if(Ehead[j].y>=53)
{
Ehead[j].status=0;
}
}
}
for(j=1;j<=20;j++)
{
if(Ehead[j].status==1 && Ehead[j].y==2)//特殊情況
{
Pos(Ehead[j].x,Ehead[j].y);
printf("■");
Pos(Ehead[j].x-2,Ehead[j].y-1);
printf("■■■");
}
if(Ehead[j].status==1 && 3<=Ehead[j].y && Ehead[j].y<=50)
{
Pos(Ehead[j].x,Ehead[j].y);
printf("■");
Pos(Ehead[j].x-2,Ehead[j].y-1);
printf("■■■");
Pos(Ehead[j].x-2,Ehead[j].y-2);
printf("■■■");
}
if(Ehead[j].status==1 && Ehead[j].y==51)//特殊情況
{
Pos(Ehead[j].x-2,Ehead[j].y-1);
printf("■■■");
Pos(Ehead[j].x-2,Ehead[j].y-2);
printf("■■■");
}
if(Ehead[j].status==1 && Ehead[j].y==52)//特殊情況
{
Pos(Ehead[j].x-2,Ehead[j].y-2);
printf("■■■");
}
}
JudgeGameOver();
}
void CreateBulletModel() //創(chuàng)建子彈的模型
{
int i;
q=(Bullet*)malloc(sizeof(Bullet));
q->x=0;
q->y=1;
q->status=0;
H=q;
for(i=2;i<=46;i++) //每行最多存在一個子彈,子彈的最大數(shù)值為地圖上下邊框所確定的值.所以一上下邊框確定一個最大的鏈表.
{
HBullet=(Bullet*)malloc(sizeof(Bullet));
HBullet->y=i;
HBullet->x=-1;
HBullet->status=0;
q->next=HBullet;
q=HBullet;
}
q->next=H;
HBullet=H;
}
void Shoot() //發(fā)射子彈
{
JudgeGameOver();
JudgeCollide();
while(HBullet->next->y!=head->y)
{
HBullet=HBullet->next;
}
HBullet->x=head->x;
HBullet->status=1; //1表示此處有子彈
Pos(HBullet->x,HBullet->y);
printf("■");
HBullet=H;
}
void BulletMove() //子彈的飛行
{ //與敵軍飛機(jī)飛行同理
JudgeGameOver();
JudgeCollide();
while(HBullet!=q)
{
if(HBullet->status==1)
{
Pos(HBullet->x,HBullet->y);
printf(" ");
}
HBullet=HBullet->next;
}
HBullet=H->next;
while(HBullet!=q)
{
H->x=HBullet->x;
H->status=HBullet->status;
H=H->next;
HBullet=HBullet->next;
}
HBullet=HBullet->next;
H=HBullet;
while(HBullet!=q)
{
if(HBullet->status==1)
{
Pos(HBullet->x,HBullet->y);
printf("■");
}
HBullet=HBullet->next;
}
HBullet=H;
JudgeGameOver();
}
int GetBulletStatus() //獲取地圖中是否存在子彈,不存在子彈的時(shí)候BulletMove()將不運(yùn)行
{
JudgeGameOver();
JudgeCollide();
while(HBullet!=q)
{
if(HBullet->status==1)
{
BulletStatus=1;
break;
}
HBullet=HBullet->next;
}
HBullet=H;
return 0;
}
void JudgeCollide() //判斷是否集中敵機(jī)
{
int j;
while(HBullet!=q)
{
for(j=1;j<=30;j++)
{
JudgeGameOver();
if(HBullet->status==1 && Ehead[j].status==1)
{
if((HBullet->x==Ehead[j].x && HBullet->y==Ehead[j].y))
{
HBullet->status=0;
Ehead[j].status=0;
score=score+10+add_2;
Pos(80,8);
printf("您的得分%d",score);
Pos(HBullet->x,HBullet->y);
printf(" ");
Pos(HBullet->x-2,HBullet->y-1);
printf(" ");
Pos(HBullet->x-2,HBullet->y-2);
printf(" ");
}
if(HBullet->x+2==Ehead[j].x && HBullet->y==Ehead[j].y)
{
HBullet->status=0;
Ehead[j].status=0;
score=score+10+add_2;
Pos(80,8);
printf("您的得分%d",score);
Pos(HBullet->x,HBullet->y);
printf(" ");
Pos(HBullet->x+2,HBullet->y);
printf(" ");
Pos(HBullet->x,HBullet->y-1);
printf(" ");
Pos(HBullet->x,HBullet->y-2);
printf(" ");
}
if(HBullet->x-2==Ehead[j].x && HBullet->y==Ehead[j].y)
{
HBullet->status=0;
Ehead[j].status=0;
score=score+10+add_2;
Pos(80,8);
printf("您的得分%d",score);
Pos(HBullet->x,HBullet->y);
printf(" ");
Pos(HBullet->x-2,HBullet->y);
printf(" ");
Pos(HBullet->x-4,HBullet->y-1);
printf(" ");
Pos(HBullet->x-4,HBullet->y-2);
printf(" ");
}
}
}
HBullet=HBullet->next;
}
HBullet=H;
}
void JudgeGameOver() //判斷是否撞機(jī)
{
int j;
for(j=1;j<=30;j++)
{
while(p!=NULL)
{
if(Ehead[j].x==p->x && Ehead[j].y==p->y)
{
EndGame();
}
p=p->next;
}
}
p=head;
}
void Pause()//暫停
{
while(1)
{
Sleep(300);
if(GetAsyncKeyState(VK_RETURN))
{
break;
}
}
}
void ControlGame() // 控制主程序
{
while(1)
{
JudgeGameOver();
JudgeCollide();
if(GetAsyncKeyState(VK_UP) && head->y!=1)
{
Status=U;
Aircraftmove();
}
else if(GetAsyncKeyState(VK_DOWN) && head->y+2!=49)
{
Status=D;
Aircraftmove();
}
else if(GetAsyncKeyState(VK_LEFT) && head->x-2!=2)
{
Status=L;
Aircraftmove();
}
else if(GetAsyncKeyState(VK_RIGHT) && head->x+2!=64)
{
Status=R;
Aircraftmove();
}
if(GetAsyncKeyState(VK_F1) && head->y!=1)
{
if(add_1<5)
{
add_1=add_1+1;
add_2=add_2+2;
}
}
if(GetAsyncKeyState(VK_F2) && head->y!=1)
{
if(add_1>2 &&add_1 <=5)
{
add_1=add_1-1;
add_1=add_2-2;
}
}
else if(GetAsyncKeyState(VK_RETURN))
{
Pause();
}
else if(GetAsyncKeyState(VK_SPACE))
{
Shoot();
}
else if(GetAsyncKeyState(VK_ESCAPE))
{
EndGame();
}
GetBulletStatus();
if(BulletStatus==1)
{
BulletMove();
}
EnemyAircraftMove();
JudgeGameOver();
}
}
void GameStart()
{
CreateMap();
CreateAircraft();
printAircraft();
CreateEnemyAircraftModel();
CreateBulletModel();
}
void EndGame()
{
system("cls");
Pos(35,25);
printf("Game Over.");
Pos(1,50);
exit(0);
}
void main()
{
GameStart();
ControlGame();
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
學(xué)習(xí)二維動態(tài)數(shù)組指針做矩陣運(yùn)算的方法
這片文章介紹了如何利用二維動態(tài)數(shù)組指針做矩陣運(yùn)算,需要的朋友可以參考下2015-07-07
使用C++一步步實(shí)現(xiàn)俄羅斯方塊后續(xù)
本文主要給大家分享的是作者在使用C++制作俄羅斯方塊小游戲的時(shí)候所需要的常用的函數(shù),有需要的小伙伴可以借鑒下,希望大家能夠喜歡。2017-12-12
C++11中value category(值類別)及move semantics(移動語義)的介紹
這篇文章主要給大家介紹了C++11中value category(值類別)及move semantics(移動語義)的介紹,文中介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05

