C语言修改了的飞机小游戏

前段时间,和室友看了一些C语言的小游戏,打算自己也玩一个,在网上找了一个模板。然后自己修改了一些我们自己觉得需要修改的漏洞,和一些不太完善的地方。
游戏画面
在这里插入图片描述
然后是代码

#include<stdio.h> 
#include<stdlib.h>
#include<windows.h>
#include<string.h>
#include<conio.h>
#define MAX 100
long long int speed=0;
int position_x,position_y;
int high,width;
int bullet_x,bullet_y;
int enemy_x,enemy_y;
int map[MAX][MAX];
int score,n;
void starup()
{
 high=20;
 width=30;
 position_x=high/2;
 position_y=width/2;
 bullet_x=0;
 bullet_y=position_y;
 enemy_x=2;
 enemy_y=position_y-1;
 score=0;
}
void startMap()
{
 int i,j;
 for(i=1;i<=high-1;i++)
 {
  map[i][1]=4;
  for(j=2;j<=width-1;j++)
   map[i][j]=0;
  map[i][width]=4;
 }
 i=high;
 for(j=1;j<=width;j++)
  map[i][j]=3;
 map[bullet_x][bullet_y]=5;
 map[position_x-1][position_y]=1;
 i=position_x;
 for(j=position_y-2;j<=position_y+2;j++)
  map[i][j]=1;
 map[position_x+1][position_y-1]=1;
 map[position_x+1][position_y+1]=1;
 map[enemy_x][enemy_y]=2;
 map[enemy_x-1][enemy_y-1]=2;
 map[enemy_x-1][enemy_y+1]=2;
}
void HideCursor()
 {
  CONSOLE_CURSOR_INFO cursor_info={1,0};
  SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
 }
 void gotoxy(int x,int y)
 {
  HANDLE handle =GetStdHandle(STD_OUTPUT_HANDLE);
  COORD pos;
  pos.X=x;
  pos.Y=y;
  SetConsoleCursorPosition(handle,pos);
 }
 void updateWithoutInput()
 {
  if(bullet_x>0)
   bullet_x--;
  if((bullet_x==enemy_x)&&(bullet_y==enemy_y))
  {
   score++;
   enemy_x=0;
   enemy_y=rand()%width;
   bullet_x=0;
  }
  if((bullet_x==enemy_x-1)&&(bullet_y==enemy_y-1))
  {
   score++;
   enemy_x=0;
   enemy_y=rand()%width;
   bullet_x=0;
  }
  if((bullet_x==enemy_x-1)&&(bullet_y==enemy_y+1))
  {
   score++;
   enemy_x=0;
   enemy_y=rand()%width;
   bullet_x=0;
  }
  if((bullet_x==enemy_x+1)&&(bullet_y==enemy_y))
  {
   score++;
   enemy_x=0;
   enemy_y=rand()%width;
   bullet_x=0;
  }
  if((bullet_x==enemy_x)&&(bullet_y==enemy_y-1))
  {
   score++;
   enemy_x=0;
   enemy_y=rand()%width;
   bullet_x=0;
  }
  if((bullet_x==enemy_x)&&(bullet_y==enemy_y+1))
  {
   score++;
   enemy_x=0;
   enemy_y=rand()%width;
   bullet_x=0;
  }
  if(enemy_x>high)
  {
   enemy_x=0;
   enemy_y=rand()%width;
  }
  if(speed==1)
   for(int i=1;i<=10000;i++)
   {
    for(int j=1;j<=10000;j++)
    {
     speed=1;//Sleep(500);
    }
   }
     speed=0;
     if(speed==0)
     {
      enemy_x++;
      speed=1;
     }
    }
    void updateWithInput()
 {
  char input;
  if(kbhit())
  {
   input=getch();
   if(input=='a')
    position_y--;
   if(input=='s')
    position_x++;
   if(input=='d')
    position_y++;
   if(input=='w')
    position_x--;
   if(input==' ')
   {
    bullet_x=position_x-1;
    bullet_y=position_y;
   }
  }
 }
 void show()
 {
  gotoxy(0,0);
  int i,j;
  for(i=1;i<=high;i++)
  {
   for(j=1;j<=width;j++)
   {
    if(map[i][j]==0)
     printf(" ");
    if(map[i][j]==1)
     printf("*");
    if(map[i][j]==2)
     printf("#");
    if(map[i][j]==3)
     printf("~");
    if(map[i][j]==4)
     printf("|");
    if(map[i][j]==5)
     printf("|");
   }
   printf("\n");
  }
  printf("\n你的得分:%d",score);
  printf("\n死亡:%d\n\n",n);
  printf("操作说明:ASDW分别操作 左下右上四个移动\n");
  printf("  空格是发射子弹");
  printf("  死亡5次游戏结束");
 }
 void death()
 {
  if((position_x==enemy_x)&&(position_y==enemy_y))//撞敌人死亡一次
  {
   n++;
   position_x=16;
   position_y=rand()%width;   //随机生成自己
   bullet_x=0;
  }
  if((position_x==enemy_x-1)&&(position_y==enemy_y-1))//撞敌人死亡一次
  {
   n++;
   position_x=16;
   position_y=rand()%width;
   bullet_x=0;
  }
  if((position_x==enemy_x-1)&&(position_y==enemy_y+1))//撞敌人死亡一次
  {
   n++;
   position_x=16;
   position_y=rand()%width;
   bullet_x=0;
  }
  if((position_x>=20)||(position_x<=0))//超出上下界面死亡一次
  {
   n++;
   position_x=16;
   position_y=rand()%width;
   bullet_x=0;
  }
  if((position_y>=30)||(position_y<=0))//超出左右界面死亡一次
  {
   n++;
   position_x=16;
   position_y=rand()%width;
   bullet_x=0;
  }
  if(n>=5)
  {
   system("CLS");
   printf("\n\n\n\n游戏结束\n");
   Sleep(1500);//睡眠函数
   exit(-1);//结束执行框
  }
 }
 void main()
 {
  starup();
  while(1)
  {
   HideCursor();
   startMap();
   show();
   updateWithoutInput();
   updateWithInput();
   death();
  }
 }

代码的主要内容是边缘和自身与敌机的形成,和光标的隐藏,以及一些飞机死亡的重生以及游戏结束。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C语言飞机大战小游戏是一个基于控制台的游戏,源代码中引用了plane.h头文件来定义游戏所需的函数和常量。main函数调用了game函数开始游戏,并在游戏结束后使用system("pause")来暂停程序。 在plane.h中,定义了Showmap函数用于打印地图,game函数用于调用开始游戏,Move函数用于控制飞机的移动,Buttle函数用于生成子弹,FadeButtle函数用于消除子弹,Plan函数用于生成敌机,FeoButtle函数用于生成敌机的子弹,Judge函数用于判断游戏的胜负。 在Plan函数中,使用了随机数种子srand((unsigned)time(NULL))来生成随机数,然后根据随机数来确定敌机的坐标。敌机的坐标不能与墙壁重合,也不能与我方飞机碰撞。当找到合适的坐标后,将地方飞机的数据设置为4,并打破循环。 通过调用这些函数和利用随机数生成敌机的坐标,可以实现一个简单的C语言飞机大战小游戏。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [C语言编写飞机大战游戏项目](https://blog.csdn.net/B85951005/article/details/126037896)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值