C語(yǔ)言實(shí)現(xiàn)打飛機(jī)游戲
本文實(shí)例為大家分享了C語(yǔ)言實(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ī)的移動(dòng) void CreateEnemyAircraftModel(); //創(chuàng)建敵機(jī)的模型 結(jié)構(gòu)體數(shù)組 void CreateEnemyAircraft(); //創(chuàng)建一架敵方飛機(jī) void EnemyAircraftMove(); //敵機(jī)的移動(dòng) void CreateBulletModel(); //創(chuàng)建子彈的模型 void Shoot(); //發(fā)射子彈 void Bulletmove(); //子彈的移動(dòng) int GetBulletStatus(); //獲取是否有子彈存在 void JudgeCollide(); //判定是否擊中敵機(jī) void JudgeGameOver(); //判定是否與敵機(jī)相撞 //為了防止在運(yùn)行別的函數(shù)時(shí)這個(gè)函數(shù)無(wú)法運(yùn)行,所以講起嵌入每一個(gè)循環(huán)的函數(shù)中,保證不遺漏任何情況. void Pause(); //暫停 void ControlGame(); //控制游戲主程序 void GameStart(); //游戲開(kāi)的的預(yù)先運(yùn)行函數(shù)集合 void EndGame(); //結(jié)束游戲 /* ■ 飛機(jī)的基本模型 ■■■ 第一行為機(jī)頭, 敵機(jī)的設(shè)定只需用到機(jī)頭,機(jī)身僅僅只是打印出來(lái),并沒(méi)有實(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("特別說(shuō)明:"); 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ù)字用的稍大. { //每一個(gè)值表示一架飛機(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ù),對(duì)其格子. { 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++) //將每一個(gè)敵機(jī)機(jī)頭向下移動(dòng)一位 { 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++) //每行最多存在一個(gè)子彈,子彈的最大數(shù)值為地圖上下邊框所確定的值.所以一上下邊框確定一個(gè)最大的鏈表. { 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(); }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(202.快樂(lè)數(shù))
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(202.快樂(lè)數(shù)),本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08C++ OpenCV實(shí)戰(zhàn)之標(biāo)記點(diǎn)檢測(cè)的實(shí)現(xiàn)
這篇文章主要介紹了如何利用C++ OpenCV實(shí)現(xiàn)關(guān)鍵點(diǎn)的檢測(cè),文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)OpenCV有一定幫助,感興趣的小伙伴可以了解一下2022-03-03帶你了解C語(yǔ)言的數(shù)據(jù)的存儲(chǔ)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言的數(shù)據(jù)的存儲(chǔ),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助2021-08-08OpenCV提取圖像中圓線(xiàn)上的數(shù)據(jù)具體流程
在對(duì)圖像進(jìn)行處理時(shí),經(jīng)常會(huì)要提取出圖像中某條直線(xiàn)、圓線(xiàn)或者ROI區(qū)域內(nèi)的感興趣數(shù)據(jù),進(jìn)行重點(diǎn)關(guān)注。本文主要介紹了利用OpenCV獲取圖像中圓線(xiàn)上的數(shù)據(jù),需要的可以參考一下2021-11-11用C語(yǔ)言實(shí)現(xiàn)單鏈表的各種操作(一)
本篇文章是對(duì)用C語(yǔ)言實(shí)現(xiàn)單鏈表的各種操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05簡(jiǎn)單介紹C語(yǔ)言中的umask()函數(shù)和truncate()函數(shù)
這篇文章主要介紹了簡(jiǎn)單介紹C語(yǔ)言中的umask()函數(shù)和truncate()函數(shù),是C語(yǔ)言入門(mén)學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09詳解如何將Spire.Doc for C++集成到C++程序中
Spire.Doc for C++是一個(gè)專(zhuān)業(yè)的Word庫(kù),供開(kāi)發(fā)人員在任何類(lèi)型的C++應(yīng)用程序中閱讀、創(chuàng)建、編輯、比較和轉(zhuǎn)換 Word 文檔,本文演示了如何以?xún)煞N不同的方式將 Spire.Doc for C++ 集成到您的 C++ 應(yīng)用程序中,希望對(duì)大家有所幫助2023-05-05