C语言实现简单飞机大战

本文实例为大家分享了C语言实现飞机大战的具体代码,供大家参考,具体内容如下

定义四个函数实现飞机大战

#include
#include
#include
//定义全局变量 
int high,width; //定义边界 
int position_x,position_y; //飞机位置 
int bullet_x,bullet_y; //子弹位置 
int enemy_x,enemy_y;
int score;
int flag; //飞机状态 
void gotoxy(int x,int y) //光标移动到(x,y)位置
{
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(handle,pos);
}
void HideCursor() // 用于隐藏光标
{
 CONSOLE_CURSOR_INFO cursor_info = {1, 0}; // 第二个值为0表示隐藏光标
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
 
void startup() //数据初始化 
{
 high=18;
 width=26;
 
 position_x=high-3; 
 position_y=width/2;
 
 bullet_x=0;
 bullet_y=position_y; 
 
 enemy_x=0;
 enemy_y=position_y;
 
 score=0;
 
 flag=0; //飞机完好 
 
 HideCursor();
}
void show() //显示界面 
{
 int i,j;
 for(i=0;i0) //子弹上升效果 
 bullet_x--;
 if((bullet_x==enemy_x)&&(bullet_y==enemy_y)) //子弹命中敌机 
 {
 score++;
 bullet_x=-1; 
 enemy_x=1;
 enemy_y=2+rand()%width-2;
 } 
 
 static int speed;
 if(speed<30) //减慢敌机速度,不影响飞机和子弹速度 
 speed++;
 if(speed==30)
 {
 if(enemy_x1)
 position_x--; 
 if((input=='s')&&position_x1)
 position_y--; 
 if((input=='d')&&position_y 
 

作者的另一段代码:C语言实现空战游戏,也很棒,分享给大家:

#include
#include
#include
#define High 27 //定义边界
#define Width 45
#define EnemyNum 5 //敌机数目 
//定义全局变量
int canvas[High][Width]={0}; //定义元素,0为空格,1为飞机,2为子弹,3为敌机,4为右下边界 
int position_x,position_y;  //飞机坐标
int enemy_x[EnemyNum],enemy_y[EnemyNum]; //敌机坐标
int score; //得分 
int Speed; //敌机速度
int bulletwidth; //子弹宽度 
void HideCursor()   //隐藏光标
{
 CONSOLE_CURSOR_INFO cursor_info = {1, 0};
 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void gotoxy(int x,int y) //光标移动到(x,y)位置
{
 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
 COORD pos;
 pos.X = x;
 pos.Y = y;
 SetConsoleCursorPosition(handle,pos);
}
void startup() //数据初始化
{
 position_x=High-2; //初始化飞机位置
 position_y=Width/2;
 canvas[position_x][position_y]=1;
 
 bulletwidth=0; //初始化子弹宽度 
 Speed=25; //敌机初始最小速度 
 int k;
 for(k=0;k0)
   canvas[i-1][j]=2;
  }
 }
 }
 for(k=0;kHigh) //生成新的敌机 
 {
 canvas[enemy_x[k]][enemy_y[k]]=0;
 enemy_x[k]=rand()%2;
 enemy_y[k]=rand()%Width;
 canvas[enemy_x[k]][enemy_y[k]]=3;
 } 
 }
 static int speed=0; 
 if(speed0)  //控制飞机方向
 {
  canvas[position_x][position_y]=0;
  position_x--;
  canvas[position_x][position_y]=1;
 }
 else if(input=='s' && position_x0)
 {
  canvas[position_x][position_y]=0;
  position_y--;
  canvas[position_x][position_y]=1;
 }
 else if(input=='d' && position_yWidth-1)
 right=0;
 for(x=left;x<=right;x++)
  canvas[position_x-1][x]=2;
 }
 }
}
int main()
{
 startup();
 system("color 2f");
 while(1)
 {
 show(); //显示界面
 updateWithoutInput(); //无需用户输入的更新
 updateWithInput(); //需用户输入的更新
 }
}

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

你可能感兴趣的:(C语言实现简单飞机大战)