C语言实现简单飞机大战语言实现简单飞机大战
主要为大家详细介绍了C语言实现简单飞机大战,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣
的小伙伴们可以参考一下
本文实例为大家分享了C语言实现飞机大战的具体代码,供大家参考,具体内容如下
定义四个函数实现飞机大战
#include<stdio.h>
#include<windows.h>
#include<conio.h>
//定义全局变量
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;i<high;i++)
{
for(j=0;j<width;j++)
{
if(flag)
break;
else if((i==position_x)&&(j==position_y)) //飞机坐标
printf("*");
else if((i==enemy_x)&&(j==enemy_y)) //敌机坐标
printf("*");
else if((i==bullet_x)&&(j==bullet_y)) //子弹坐标
printf("|");
else if ((j==width-1)||(i==high-1)||(j==0)||(i==0)) //打印边界
printf("#");
else
printf(" ");
}
printf("");
}
printf("");
if((position_x==enemy_x)&&(position_y==enemy_y))
评论0