欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C語言實現(xiàn)打飛機游戲

 更新時間:2020年03月04日 09:14:26   作者:qq_42577465  
這篇文章主要為大家詳細介紹了C語言實現(xiàn)打飛機游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了C語言實現(xiàn)打飛機游戲的具體代碼,供大家參考,具體內容如下

#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  //己方飛機的節(jié)點
 {
   int x;
   int y;
   struct node *next;
 }node;
 
typedef struct Bullet
 {
   int x;
   int y;
   int status;  //0 1 表示此處是否有子彈
   struct Bullet *next;
 }Bullet;   //子彈模型節(jié)點
 
typedef struct Enemy
 {
   int x;
   int y;
   int status;   //0 1 表示此節(jié)點是否存在飛機
 }Enemy;   //敵機節(jié)點
 
// 全局變量//
 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();     //固定光標
 void CreateMap();    //創(chuàng)建游戲界面
 void CreateAircraft();    //創(chuàng)建自己的飛機
 void printAircraft();    //打印自己的飛機
 void Aircraftmove();   //自己飛機的移動
 void CreateEnemyAircraftModel();  //創(chuàng)建敵機的模型 結構體數(shù)組
 void CreateEnemyAircraft();   //創(chuàng)建一架敵方飛機
 void EnemyAircraftMove();  //敵機的移動
 void CreateBulletModel();   //創(chuàng)建子彈的模型
 void Shoot();   //發(fā)射子彈
 void Bulletmove();   //子彈的移動
 int GetBulletStatus();    //獲取是否有子彈存在
 void JudgeCollide();     //判定是否擊中敵機
 void JudgeGameOver();   //判定是否與敵機相撞 
 //為了防止在運行別的函數(shù)時這個函數(shù)無法運行,所以講起嵌入每一個循環(huán)的函數(shù)中,保證不遺漏任何情況.
 void Pause();    //暫停
 void ControlGame();  //控制游戲主程序
 void GameStart();   //游戲開的的預先運行函數(shù)集合
 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:發(fā)射子彈");
   Pos(70,22);
   printf("ESC:退出游戲");
   Pos(70,24);
   printf("Enter:暫停");
   Pos(70,26);
   printf("用↑.↓.←.→控制飛機");
   Pos(70,28);
   printf("F1:加速敵機的產(chǎn)生");
   Pos(70,30);
   printf("F2:減緩敵機的產(chǎn)生");
   Pos(80,40);
   printf("特別說明:");
   Pos(70,42);
   printf("敵機從地圖下飛出不結束游戲");
 }
 
void CreateAircraft()
 {
   node *q;
   int i;
   head=(node*)malloc(sizeof(node));//鏈表形式創(chuàng)建飛機,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("■");
       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++)    //結構體數(shù)組表示敵軍飛機最多同時存在30架,也許不可能有那么多,數(shù)字用的稍大.
   {        //每一個值表示一架飛機,敵機只需知道其機頭坐標
     Ehead[j].x=-1;
     Ehead[j].y=1;
     Ehead[j].status=0;
   }
 }
 
void CreateEnemyAircraft()//創(chuàng)建出一架新的飛機
 {
   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為偶數(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()       //模擬敵機向下飛行的函數(shù)
 {
   Sleep(100);
   if(s>=15)     //控制敵機飛機的出現(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++)      //將每一個敵機機頭向下移動一位
   {
     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()     //子彈的飛行
 {         //與敵軍飛機飛行同理
   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,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() //判斷是否撞機
 {
   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();
 }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。 

相關文章

  • Qt編譯OpenCV的實現(xiàn)步驟

    Qt編譯OpenCV的實現(xiàn)步驟

    本文主要介紹了Qt編譯OpenCV的實現(xiàn)步驟,通過詳細的步驟和說明,幫助開發(fā)者在Qt環(huán)境中成功集成并編譯OpenCV,從而為各類計算機視覺項目提供強大的支持,感興趣的可以了解一下
    2024-01-01
  • C++實現(xiàn)LeetCode(202.快樂數(shù))

    C++實現(xiàn)LeetCode(202.快樂數(shù))

    這篇文章主要介紹了C++實現(xiàn)LeetCode(202.快樂數(shù)),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • 詳解C++11中的右值引用與移動語義

    詳解C++11中的右值引用與移動語義

    本篇文章主要介紹了詳解C++11中的右值引用與移動語義,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • C++ OpenCV實戰(zhàn)之標記點檢測的實現(xiàn)

    C++ OpenCV實戰(zhàn)之標記點檢測的實現(xiàn)

    這篇文章主要介紹了如何利用C++ OpenCV實現(xiàn)關鍵點的檢測,文中的示例代碼講解詳細,對我們學習OpenCV有一定幫助,感興趣的小伙伴可以了解一下
    2022-03-03
  • 帶你了解C語言的數(shù)據(jù)的存儲

    帶你了解C語言的數(shù)據(jù)的存儲

    這篇文章主要為大家詳細介紹了C語言的數(shù)據(jù)的存儲,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助
    2021-08-08
  • OpenCV提取圖像中圓線上的數(shù)據(jù)具體流程

    OpenCV提取圖像中圓線上的數(shù)據(jù)具體流程

    在對圖像進行處理時,經(jīng)常會要提取出圖像中某條直線、圓線或者ROI區(qū)域內的感興趣數(shù)據(jù),進行重點關注。本文主要介紹了利用OpenCV獲取圖像中圓線上的數(shù)據(jù),需要的可以參考一下
    2021-11-11
  • 用C語言實現(xiàn)單鏈表的各種操作(一)

    用C語言實現(xiàn)單鏈表的各種操作(一)

    本篇文章是對用C語言實現(xiàn)單鏈表的各種操作進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • 簡單介紹C語言中的umask()函數(shù)和truncate()函數(shù)

    簡單介紹C語言中的umask()函數(shù)和truncate()函數(shù)

    這篇文章主要介紹了簡單介紹C語言中的umask()函數(shù)和truncate()函數(shù),是C語言入門學習中的基礎知識,需要的朋友可以參考下
    2015-09-09
  • 詳解如何將Spire.Doc for C++集成到C++程序中

    詳解如何將Spire.Doc for C++集成到C++程序中

    Spire.Doc for C++是一個專業(yè)的Word庫,供開發(fā)人員在任何類型的C++應用程序中閱讀、創(chuàng)建、編輯、比較和轉換 Word 文檔,本文演示了如何以兩種不同的方式將 Spire.Doc for C++ 集成到您的 C++ 應用程序中,希望對大家有所幫助
    2023-05-05
  • C語言零基礎講解指針和數(shù)組

    C語言零基礎講解指針和數(shù)組

    由于數(shù)據(jù)的表現(xiàn)形式多種多樣,還有字符型和其它的數(shù)值類型,因此僅有基本數(shù)據(jù)類型是不夠的。是否可以通過基本數(shù)據(jù)類型的組合抽象構造其它的數(shù)據(jù)類型呢?下面是小編為大家?guī)淼腃語言數(shù)組與指針詳解的知識
    2022-04-04

最新評論