c/c++语言开发共享C++编写实现飞机大战

本文实例为大家分享了c++编写实现飞机大战的具体代码,供大家参考,具体内容如下前几天看大佬写了个神经网络训练ai玩飞机大战,我想,凭我现有知识能不能也写一个飞机大战,就进行了尝试,成果如下。#incl

c/c++开发分享C++编写实现飞机大战实例为大家分享了c++编写实现飞机大战的具体代码,供大家参考,具体内容如下

前几天看大佬写了个神经网络训练ai玩飞机大战,我想,凭我现有知识能不能也写一个飞机大战,就进行了尝试,成果如下。

#include<iostream>  #include<ctime>  #include<stdlib.h>  #include<windows.h>  using namespace std;  const int mapx = 40, mapy = 35, cost = 2, prise = 5;   //cost: cost of bullet,   prise: prise of killing a enemy.  class plane  {      public:          void start();      private:          void reset();          void get_enemy(int &y);          void print() const;          void update_print();          char map[mapx][mapy];/*      plane model:      /=|=           */          int plane_y, plane_x, score, cont;  };

到此我们设计了飞机的模型(我水平不够 整个游戏就用一个类了- -,这个类其实是整个游戏的类 不是飞机类)关于变量cont的说明我放在后面了 接下来我写了一个初始化函数,为类内变量初始化。

void plane::reset()  {      for(int i = 0; i < mapx; i++)      {          for(int j = 0; j < mapy; j++)          {              if(!i || !j || j == mapy - 1)              {                  map[i][j] = '#';              }              else                  map[i][j] = ' ';          }      }      plane_x = mapx - 1;      plane_y = mapy/2 - 2;      score = cont = 0;      map[plane_x][plane_y] = '/';      map[plane_x][plane_y + 1] = map[plane_x][plane_y + 3] = '=';      map[plane_x][plane_y + 2] = '|';      map[plane_x][plane_y + 4] = '\';  }

然后我利用时间参数的随机数得到敌机的位置,这里其实有个问题,因为时间是按一定顺序均匀变化的,我们如果直接用时间作随机数种子的话,敌机的出现会非常均匀,因此我引入了一个cont变量,用来打乱我们均匀的时间参数的个位数。具体使用见后文。

void plane::get_enemy(int &y) const  {      srand(int(time(0)));      int n = rand();      if(cont%2)          n -= cont;      else          n += cont;      y = n % (mapy - 2) + 1;  }

这个函数就是随机生成敌机的位置,cont在此就起到打乱随机生成数的个位数的目的,每更新一次,cont++,为防止cont过大,我规定cont==10时,就将cont = 0,使其能在1到9变化,影响个位数。

void plane::print() const  {      system("cls");      for(int i = 0; i < mapx; i++)      {          for(int j = 0; j < mapy; j++)          {              cout<<map[i][j];          }          cout<<endl;      }      cout<<"score : "<<score<<'.'<<endl<<"pay "<<cost<<" scores to send '+' and get "<<prise<<" scores by killing enemies."<<endl;  }

这里是一个打印的函数,不赘述。

void plane::update_print()  {      for(int i = 1; i < mapx; i++)      {          for(int j = 1; j < mapy - 1; j++)          {              if(map[i][j] == 'm')              {                  if(i == mapx - 1)                      map[i][j] = ' ';                  else if(map[i + 1][j] == '+')                  {                      map[i][j] = map[i+1][j] = ' ';                      score += prise;                  }              }              else if(map[i][j] == '+')              {                  map[i][j] = ' ';                  if(i != 1)                      map[i-1][j] = '+';              }          }      }      for(int i = mapx - 2; i > 0; i--)      {          for(int j = 1; j < mapy - 1; j++)          {              if(map[i][j] == 'm')              {                  if(i != mapx - 1)                      if(map[i+1][j] == '+')                      {                          map[i + 1][j] = ' ';                          score += prise;                      }                      else                          map[i + 1][j] = 'm';                  map[i][j] = ' ';              }          }      }      int enemy_y;      get_enemy(enemy_y);      if(map[1][enemy_y] == '+')      {          map[1][enemy_y] = ' ';          score += prise;      }      else          map[1][enemy_y] = 'm';                for(int i = 0; i < 5; i++)      {          if(map[plane_x][plane_y + i] != 'm')              map[plane_x][plane_y + i] = ' ';      }      bool jleft, jright, jup, jdown;      jleft = jright = jup = jdown = false;        if(getasynckeystate(vk_left) & 0x8000)          if(plane_y != 1)              jleft = true;      if(getasynckeystate(vk_right) & 0x8000)          if(plane_y + 4 != mapy - 2)              jright = true;      if(getasynckeystate(vk_up) & 0x8000)          if(plane_x != 1)              jup = true;      if(getasynckeystate(vk_down) & 0x8000)          if(plane_x != mapx - 1)              jdown = true;      if(!(jleft && jright))      {          if(jleft)              plane_y--;          if(jright)              plane_y++;      }      if(!(jup && jdown))      {          if(jup)              plane_x--;          if(jdown)              plane_x++;      }      if(getasynckeystate(vk_space) & 0x8000)          {              score -= cost;              if(map[plane_x - 1][plane_y + 2] == ' ')                  map[plane_x - 1][plane_y + 2] = '+';              else if(map[plane_x - 1][plane_y + 2] == 'm')              {                  map[plane_x - 1][plane_y + 2] = ' ';                  score += prise;              }          }        if(map[plane_x][plane_y]=='m'||map[plane_x][plane_y+1]=='m'||      map[plane_x][plane_y+2]=='m'||map[plane_x][plane_y+3]=='m'||map[plane_x][plane_y+4]=='m')      {          system("cls");          for(int i = 0; i < mapx; i++)          {              cout<<"game over."<<endl;          }          cout<<"your final scores are "<<score<<'.'<<endl;          system("pause");          exit(1);      }      map[plane_x][plane_y] = '/';      map[plane_x][plane_y + 1] = map[plane_x][plane_y + 3] = '=';      map[plane_x][plane_y + 2] = '|';      map[plane_x][plane_y + 4] = '\';      cont++;      if(cont == 10)              cont = 0;      print();  }

这个函数我其实感觉自己写的太大了,应该进一步分装,这确实是个不足之处。具体操作就是每轮对飞机的移动,还有子弹和敌机的前进以及判断子弹是否达到敌机和我们的飞机是否撞到敌机。其中我用到了windows.h文件中的getasynckeystate函数,其参数为键盘某个键的vk值(可查表),返回一个16个位的数(因操作系统不同而不同,我的计算机是返回16位)。若该键在上次判断到此次判断之间被按下过,则0号位为1,反之为0;若该键正在被按下,则15号位为1,反之为0.将返回值与0x8000作“与&”操作,则第一位的数字决定了我们&操作的结果。因为xxxx xxxx xxxx xxxx & 1000 0000 0000 0000 == x000 0000 0000 0000.从而操控我们的飞机。

void plane::start()  {      reset();      while(1)      {          sleep(50);          update_print();      }  }

开始函数,用以从类外部访问类内的private函数,并且组织起循环。
然后 用主函数运行即可。

int main()  {      plane plane_game;      plane_game.start();      return 0;  }

效果如下:

C++编写实现飞机大战

以上就是c/c++开发分享C++编写实现飞机大战的全部内容,希望对大家的学习有所帮助,也希望大家多多支持<计算机技术网(www.ctvol.com)!!>。

需要了解更多c/c++开发分享C++编写实现飞机大战,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/1238068.html

(0)
上一篇 2022年9月11日
下一篇 2022年9月11日

精彩推荐