C語言實現打飛機遊戲
阿新 • • 發佈:2020-03-05
本文例項為大家分享了C語言實現打飛機遊戲的具體程式碼,供大家參考,具體內容如下
#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 //己方飛機的節點 { int x; int y; struct node *next; }node; typedef struct Bullet { int x; int y; int status; //0 1 表示此處是否有子彈 struct Bullet *next; }Bullet; //子彈模型節點 typedef struct Enemy { int x; int y; int status; //0 1 表示此節點是否存在飛機 }Enemy; //敵機節點 // 全域性變數// 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; //函式宣告// void Pos(); //固定游標 void CreateMap(); //建立遊戲介面 void CreateAircraft(); //建立自己的飛機 void printAircraft(); //列印自己的飛機 void Aircraftmove(); //自己飛機的移動 void CreateEnemyAircraftModel(); //建立敵機的模型 結構體陣列 void CreateEnemyAircraft(); //建立一架敵方飛機 void EnemyAircraftMove(); //敵機的移動 void CreateBulletModel(); //建立子彈的模型 void Shoot(); //發射子彈 void Bulletmove(); //子彈的移動 int GetBulletStatus(); //獲取是否有子彈存在 void JudgeCollide(); //判定是否擊中敵機 void JudgeGameOver(); //判定是否與敵機相撞 //為了防止在執行別的函式時這個函式無法執行,所以講起嵌入每一個迴圈的函式中,保證不遺漏任何情況. void Pause(); //暫停 void ControlGame(); //控制遊戲主程式 void GameStart(); //遊戲開的的預先執行函式集合 void EndGame(); //結束遊戲 /* ■ 飛機的基本模型 ■■■ 第一行為機頭,敵機的設定只需用到機頭,機身僅僅只是打印出來,並沒有實際內容 ■■■ 下兩行為機身 */ void Pos(int x,int y)//設定游標位置 { 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:發射子彈"); Pos(70,22); printf("ESC:退出遊戲"); Pos(70,24); printf("Enter:暫停"); Pos(70,26); printf("用↑.↓.←.→控制飛機"); Pos(70,28); printf("F1:加速敵機的產生"); Pos(70,30); printf("F2:減緩敵機的產生"); Pos(80,40); printf("特別說明:"); Pos(70,42); printf("敵機從地圖下飛出不結束遊戲"); } void CreateAircraft() { node *q; int i; head=(node*)malloc(sizeof(node));//連結串列形式建立飛機,x,y分別為顯示的座標 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(" "); 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++) //結構體陣列表示敵軍飛機最多同時存在30架,也許不可能有那麼多,數字用的稍大. { //每一個值表示一架飛機,敵機只需知道其機頭座標 Ehead[j].x=-1; Ehead[j].y=1; Ehead[j].status=0; } } void CreateEnemyAircraft()//創建出一架新的飛機 { 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) //保證機頭x為偶數,對其格子. { 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() //模擬敵機向下飛行的函式 { Sleep(100); if(s>=15) //控制敵機飛機的出現頻率 { 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-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-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++) //將每一個敵機機頭向下移動一位 { 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-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("■■■"); } if(Ehead[j].status==1 && Ehead[j].y==52)//特殊情況 { Pos(Ehead[j].x-2,Ehead[j].y-2); printf("■■■"); } } JudgeGameOver(); } void CreateBulletModel() //建立子彈的模型 { int i; q=(Bullet*)malloc(sizeof(Bullet)); q->x=0; q->y=1; q->status=0; H=q; for(i=2;i<=46;i++) //每行最多存在一個子彈,子彈的最大數值為地圖上下邊框所確定的值.所以一上下邊框確定一個最大的連結串列. { 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() //發射子彈 { 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() //子彈的飛行 { //與敵軍飛機飛行同理 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() //獲取地圖中是否存在子彈,不存在子彈的時候BulletMove()將不執行 { JudgeGameOver(); JudgeCollide(); while(HBullet!=q) { if(HBullet->status==1) { BulletStatus=1; break; } HBullet=HBullet->next; } HBullet=H; return 0; } void JudgeCollide() //判斷是否集中敵機 { 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,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,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() //判斷是否撞機 { 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(); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。