C语言实现打飞机游戏

所属分类: 软件编程 / C 语言 阅读数: 140
收藏 0 赞 0 分享

本文实例为大家分享了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("■");
       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++)    //结构体数组表示敌军飞机最多同时存在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);
       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()      //创建子弹的模型
 {
   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,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();
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。 

更多精彩内容其他人还在看

用标准c++实现string与各种类型之间的转换

这个类在头文件中定义, < sstream>库定义了三种类:istringstream、ostringstream和stringstream,分别用来进行流的输入、输出和输入输出操作。另外,每个类都有一个对应的宽字符集版本
收藏 0 赞 0 分享

C++如何通过ostringstream实现任意类型转string

再使用整型转string的时候感觉有点棘手,因为itoa不是标准C里面的,而且即便是有itoa,其他类型转string不是很方便。后来去网上找了一下,发现有一个好方法
收藏 0 赞 0 分享

C/C++指针小结

要搞清一个指针需要搞清指针的四方面的内容:指针的类型,指针所指向的类型,指针的值或者叫指针所指向的内存区,还有指针本身所占据的内存区
收藏 0 赞 0 分享

C++ 类的静态成员深入解析

在C++中类的静态成员变量和静态成员函数是个容易出错的地方,本文先通过几个例子来总结静态成员变量和成员函数使用规则,再给出一个实例来加深印象
收藏 0 赞 0 分享

C++类的静态成员初始化详细讲解

通常静态数据成员在类声明中声明,在包含类方法的文件中初始化.初始化时使用作用域操作符来指出静态成员所属的类.但如果静态成员是整型或是枚举型const,则可以在类声明中初始化
收藏 0 赞 0 分享

C++类静态成员与类静态成员函数详解

静态成员不可在类体内进行赋值,因为它是被所有该类的对象所共享的。你在一个对象里给它赋值,其他对象里的该成员也会发生变化。为了避免混乱,所以不可在类体内进行赋值
收藏 0 赞 0 分享

C++中的friend友元函数详细解析

友元可以是一个函数,该函数被称为友元函数;友元也可以是一个类,该类被称为友元类。友元函数的特点是能够访问类中的私有成员的非成员函数。友元函数从语法上看,它与普通函数一样,即在定义上和调用上与普通函数一样
收藏 0 赞 0 分享

static全局变量与普通的全局变量的区别详细解析

以下是对static全局变量与普通的全局变量的区别进行了详细的分析介绍,需要的朋友可以过来参考下,希望对大家有所帮助
收藏 0 赞 0 分享

C++ explicit关键字的应用方法详细讲解

C++ explicit关键字用来修饰类的构造函数,表明该构造函数是显式的,既然有"显式"那么必然就有"隐式",那么什么是显示而什么又是隐式的呢?下面就让我们一起来看看这方面的知识吧
收藏 0 赞 0 分享

教你5分钟轻松搞定内存字节对齐

随便google一下,人家就可以跟你解释的,一大堆的道理,我们没怎么多时间,讨论为何要对齐.直入主题,怎么判断内存对齐规则,sizeof的结果怎么来的,请牢记以下3条原则
收藏 0 赞 0 分享
查看更多