c语言字符串去重简单,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;i

{

for(j=0;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("\n");

}

printf("\n");

if((position_x==enemy_x)&&(position_y==enemy_y))

{

flag=1; //飞机撞毁 游戏结束

printf("得分: %d\n",score);

printf("游戏结束");

}

else

printf("得分: %d\n",score);

}

void withoutInpute() //与用户输入无关

{

if(bullet_x>0) //子弹上升效果

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_x

enemy_x++;

else

{

enemy_x=0;

enemy_y=2+rand()%width-2;

}

speed=0;

}

}

void withInpute() //与用户输入有关

{

char input;

if(kbhit()) //控制飞机方向

{

input=getch();

if((input=='w')&&position_x>1)

position_x--;

if((input=='s')&&position_x

position_x++;

if((input=='a')&&position_y>1)

position_y--;

if((input=='d')&&position_y

position_y++;

if(input==' ')

{

bullet_x=position_x-1;

bullet_y=position_y;

}

}

}

int main()

{

system("color 2f");

startup(); // 数据初始化

while(1) // 游戏循环执行

{

gotoxy(0,0);

show(); // 显示画面

withoutInpute(); // 与用户输入无关的更新

withInpute(); // 与用户输入有关的更新

}

}

作者的另一段代码: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;k

{

enemy_x[k]=rand()%2; //初始化敌机位置

enemy_y[k]=rand()%Width;

canvas[enemy_x[k]][enemy_y[k]]=3;

}

score=0; //得分初始化

HideCursor();

}

void show() //显示界面

{

int i,j;

gotoxy(0,0);

for(i=0;i<=High;i++)

{

for(j=0;j<=Width;j++)

{

if(canvas[i][j] == 1)

printf("*"); //输出飞机

else if(canvas[i][j]==2)

printf("|"); //输出子弹

else if(canvas[i][j]==3)

printf("@"); //输出敌机

else if(canvas[i][j]==4)

printf("#"); //输出边界#

else

printf(" "); //输出空格

}

printf("\n");

}

printf("得分:%d\n",score);

}

void updateWithoutInput() //无需用户输入的更新

{

int i,j,k;

for(i=0;i

{

for(j=0;j

{

if(canvas[i][j]==2)

{

for(k=0;k

{

if(i==enemy_x[k] && j==enemy_y[k]) //击中敌机

{

score++;

if(score==5||score==10) //得分达到标准子弹加宽

bulletwidth++;

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;

}

}

canvas[i][j]=0; //子弹自动上升

if(i>0)

canvas[i-1][j]=2;

}

}

}

for(k=0;k

{

if(enemy_x[k]>High) //生成新的敌机

{

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(speed

speed++;

if(speed==Speed)

{

for(k=0;k

{

canvas[enemy_x[k]][enemy_y[k]]=0; //敌机自动下落

enemy_x[k]++;

canvas[enemy_x[k]][enemy_y[k]]=3;

}

speed=0;

}

for(k=0;k

{

if(enemy_x[k]==position_x&&enemy_y[k]==position_y) //飞机撞毁

{

printf("游戏结束\n");

exit(0);

}

}

}

void updateWithInput() //需用户输入的更新

{

char input;

if(kbhit())

{

input=getch();

if(input=='w' && position_x>0) //控制飞机方向

{

canvas[position_x][position_y]=0;

position_x--;

canvas[position_x][position_y]=1;

}

else if(input=='s' && position_x

{

canvas[position_x][position_y]=0;

position_x++;

canvas[position_x][position_y]=1;

}

else if(input=='a' && position_y>0)

{

canvas[position_x][position_y]=0;

position_y--;

canvas[position_x][position_y]=1;

}

else if(input=='d' && position_y

{

canvas[position_x][position_y]=0;

position_y++;

canvas[position_x][position_y]=1;

}

else if(input=' ') //space发射子弹

{

int left,right;

int x;

left=position_y-bulletwidth;

if(left<0)

left=0;

right=position_y+bulletwidth;

if(right>Width-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(); //需用户输入的更新

}

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值