c/c++语言开发共享利用C++编写简易宝可梦对战小游戏

最近想到了用c++写个小时候课间嘴上玩的那种宝可梦游戏,先试写了个demo,只有早期的三个宝可梦和基础招式,感兴趣的朋友可以再自己添加,也没有各种物防特攻数值啥的,等以后会搞图形化界面了再做个复杂的,

最近想到了用c++写个小时候课间嘴上玩的那种宝可梦游戏,先试写了个demo,只有早期的三个宝可梦和基础招式,感兴趣的朋友可以再自己添加,也没有各种物防特攻数值啥的,等以后会搞图形化界面了再做个复杂的,功能完善的。

玩法就是选择宝可梦和电脑对战,除了攻击还有三次防御的机会,可以完全抵消对面这轮的攻击,增加了一点博弈的感觉(其实和电脑也没啥好博弈的)

上代码

#include<iostream>  #include<windows.h>  #include<ctime>  enum type {normal,fire,grass,water};//定义招式与pokemon属性  struct atk//招式结构体  {      char name[20];//招式名      int damage;//伤害      type type;//属性      int speed;//招式速度      int pp; //招式技能点数量  };  atk def{"def",0,normal,10,3};//防御  struct pokemon//pokemon结构体  {      char name[30];//名      int hp;//初始血量      int speed;//pokemon本身速度      type type;//属性      atk atk1;//招式1到4      atk atk2;      atk atk3;      atk atk4;      int def;//防御点数  };  void initial();  int choose(int);  int aichoose(int);  int p_choose(int,pokemon);  int ai_choose(int,pokemon);  void duel(pokemon p_pokemon,pokemon ai_pokemmon);  double damage_coeff(type type1,type type2);     int main()  {      using namespace std;      int cont=1;//设定循环游戏的flag      while(cont==1)      {          system("cls");//新游戏开始时清屏          //初始化各个招式与pokemon          atk watergun{"water gun",40,water,2,2};//水枪          atk quickattack{"quick attack",40,normal,5,3};//电光石火          atk scratch{"scratch",40,normal,2,8};//抓          atk bubblebeam{"bubble beam",65,water,1,1};//泡沫光线          atk razorleaf{"razor leaf",55,grass,3,1};//飞叶快刀          atk vinewhip{"vine whip",35,grass,4,2};//藤鞭          atk tackle{"tackle",35,normal,2,10};//撞击          atk ember{"ember",40,fire,2,1};//火苗          atk firespin{"fire spin",35,fire,4,2};//火焰旋涡          pokemon bulbasaur{"bulbasaur",200,3,grass,tackle,quickattack,vinewhip,razorleaf,3};//妙蛙种子          pokemon charmander{"charmander",200,2,fire,scratch,quickattack,ember,firespin,3};//小火龙          pokemon squirtle{"squirtle",200,1,water,tackle,quickattack,watergun,bubblebeam,3};//杰尼龟          initial();          pokemon p_pokemon;          pokemon ai_pokemon;          int num1,num2;          num1=choose(num1);          num2=aichoose(num2);          //确定双方选择的pokemon          switch(num1)          {              case 1:                  p_pokemon=bulbasaur;                  break;              case 2:                  p_pokemon=charmander;                  break;              case 3:                  p_pokemon=squirtle;                  break;          }          switch(num2)          {              case 0:                  ai_pokemon=bulbasaur;                  break;              case 1:                  ai_pokemon=charmander;                  break;              case 2:                  ai_pokemon=squirtle;                  break;          }          system("cls");          sleep(500);          cout <<"you choose "<<p_pokemon.name<<" and your opponent choose "<<ai_pokemon.name<<endl;          duel(p_pokemon,ai_pokemon);          cout <<"if you want to play another round,enter number 1,or enter others number to quit:"<<endl;          cin >>cont;      }      cin.get();      return 0;  }     void initial()//初始化界面  {      using namespace std;      cout <<"welcome to the pokemon duel!"<<endl;      cout <<"your pokemon has 3 chances to defend.once you decide to defend,you will not get damage this turn."<<endl;      cout <<"but also,you can not attack this turn."<<endl;      cout <<"now choose your pokemon by enter the number:";      cout <<"1.bulbasaur 2.charmander 3.squirtle";  }  int choose(int num)//玩家选择pokemon,利用递归让玩家正确输入  {      using namespace std;      cin >>num;      if(num!=1&&num!=2&&num!=3)      {          cout <<"you choose a wrong number, choose it again:";          num=choose(num);      }      return num;  }  int aichoose(int num)//电脑选择pokemon,利用随机数让电脑随机选择  {      using namespace std;      srand((unsigned)time(null));      num = rand() % 3;      return num;  }  int p_choose(int pchoose,pokemon p_pokemon)//玩家选择招式,利用递归让玩家正确输入  {      using namespace std;      cin >>pchoose;      //判断玩家所选的招式pp是否足够      if(pchoose==1&&p_pokemon.atk1.pp<=0)      {          cout <<"you don't have enough pp"<<endl;          pchoose=p_choose(pchoose,p_pokemon);      }      if(pchoose==2&&p_pokemon.atk2.pp<=0)      {          cout <<"you don't have enough pp"<<endl;          pchoose=p_choose(pchoose,p_pokemon);      }      if(pchoose==3&&p_pokemon.atk3.pp<=0)      {          cout <<"you don't have enough pp"<<endl;          pchoose=p_choose(pchoose,p_pokemon);      }      if(pchoose==4&&p_pokemon.atk4.pp<=0)      {          cout <<"you don't have enough pp"<<endl;         pchoose=p_choose(pchoose,p_pokemon);      }      if(pchoose==5&&p_pokemon.def<=0)      {          cout <<"you don't have enough pp"<<endl;          pchoose=p_choose(pchoose,p_pokemon);      }      if(pchoose>5||pchoose<=0)      {          cout <<"you choose a wrong number, choose it again:";          pchoose=p_choose(pchoose,p_pokemon);      }      return pchoose;  }  int ai_choose(int aichoose,pokemon ai_pokemon)//利用随机数让电脑随机选择  {      using namespace std;      srand((unsigned)time(null));      aichoose = rand() % 5;      //判断电脑选择招式的pp是否足够      if(aichoose==0&&ai_pokemon.atk1.pp<=0)      {          sleep(1000);          aichoose=ai_choose(aichoose,ai_pokemon);      }      if(aichoose==1&&ai_pokemon.atk2.pp<=0)      {          sleep(1000);          aichoose=ai_choose(aichoose,ai_pokemon);      }      if(aichoose==2&&ai_pokemon.atk3.pp<=0)      {          sleep(1000);          aichoose=ai_choose(aichoose,ai_pokemon);      }      if(aichoose==3&&ai_pokemon.atk4.pp<=0)      {          sleep(1000);          aichoose=ai_choose(aichoose,ai_pokemon);      }      if(aichoose==4&&ai_pokemon.def<=0)      {          sleep(1000);          aichoose=ai_choose(aichoose,ai_pokemon);      }      return aichoose;  }  double damage_coeff(type type1,type type2)//计算攻击时伤害加成系数,type1为招式属性,type2为pokemon属性  {      if(type1==normal)      {          return 1;      }      if(type1==grass)      {          switch(type2)          {              case grass:                  return 0.5;                  break;              case fire:                  return 0.5;                  break;              case water:                  return 2;                  break;          }      }      if(type1==fire)      {          switch(type2)          {              case grass:                  return 2;                  break;              case fire:                  return 0.5;                  break;              case water:                  return 0.5;                  break;          }      }      if(type1==water)      {          switch(type2)          {              case grass:                  return 0.5;                  break;              case fire:                  return 2;                  break;              case water:                  return 0.5;                  break;          }      }  }  void duel(pokemon p_pokemon,pokemon ai_pokemmon)//战斗流程函数  {      using namespace std;      while(p_pokemon.hp>0&&ai_pokemmon.hp>0)//双方hp均大于0时继续战斗      {          //显示双方相关信息          cout <<"your hp:"<<p_pokemon.hp<<" your opponent's hp:"<<ai_pokemmon.hp<<endl<<endl;          cout <<"choose your action this turn by ernter the number:"<<endl;          cout <<"1."<<p_pokemon.atk1.name<<" pp:"<<p_pokemon.atk1.pp<<endl;          cout <<"2."<<p_pokemon.atk2.name<<" pp:"<<p_pokemon.atk2.pp<<endl;          cout <<"3."<<p_pokemon.atk3.name<<" pp:"<<p_pokemon.atk3.pp<<endl;          cout <<"4."<<p_pokemon.atk4.name<<" pp:"<<p_pokemon.atk4.pp<<endl;          cout <<"5.deffense pp:"<<p_pokemon.def;          atk this_turn_p_atk;//定义玩家本轮所选招式          atk this_turn_ai_atk;//定义电脑本轮所选招式          int pchoose;          pchoose=p_choose(pchoose,p_pokemon);          int aichoose;          aichoose=ai_choose(aichoose,ai_pokemmon);          switch(pchoose)          {              case 1:                  p_pokemon.atk1.pp=p_pokemon.atk1.pp-1;                  this_turn_p_atk=p_pokemon.atk1;                  break;              case 2:                  p_pokemon.atk2.pp=p_pokemon.atk2.pp-1;                  this_turn_p_atk=p_pokemon.atk2;                  break;              case 3:                  p_pokemon.atk3.pp=p_pokemon.atk3.pp-1;                  this_turn_p_atk=p_pokemon.atk3;                  break;              case 4:                  p_pokemon.atk4.pp=p_pokemon.atk4.pp-1;                  this_turn_p_atk=p_pokemon.atk4;                  break;              case 5:                  p_pokemon.def=p_pokemon.def-1;                  this_turn_p_atk=def;                  break;          }          switch(aichoose)          {              case 0:                  ai_pokemmon.atk1.pp=ai_pokemmon.atk1.pp-1;                  this_turn_ai_atk=ai_pokemmon.atk1;                  break;              case 1:                  ai_pokemmon.atk2.pp=ai_pokemmon.atk2.pp-1;                  this_turn_ai_atk=ai_pokemmon.atk2;                  break;              case 2:                  ai_pokemmon.atk3.pp=ai_pokemmon.atk3.pp-1;                  this_turn_ai_atk=ai_pokemmon.atk3;                  break;              case 3:                  ai_pokemmon.atk4.pp=ai_pokemmon.atk4.pp-1;                  this_turn_ai_atk=ai_pokemmon.atk4;                  break;              case 4:                  ai_pokemmon.def=ai_pokemmon.def-1;                  this_turn_ai_atk=def;                  break;          }          system("cls");          //判断是否有玩家本轮采取防御          if(aichoose==4&&pchoose!=5)          {              cout <<"you use "<<this_turn_p_atk.name<<".";              sleep(500);              cout <<"but your attack was defended!"<<endl;          }          else if(pchoose==5&&aichoose!=4)          {              cout <<"your opponent use "<<this_turn_ai_atk.name<<".";              sleep(500);              cout <<"but you defend the attack from the opponent!"<<endl;          }          else if(pchoose==5&&aichoose==4)          {              cout <<"you both defend this turn."<<endl;              sleep(250);          }          else//双方都没有防御的情况          {              int p_speed,ai_speed;              double p_coeff,ai_coeff;              //真正的速度为pokemon本身速度加招式速度              p_speed=p_pokemon.speed+this_turn_p_atk.speed;//本轮玩家真正的速度              ai_speed=ai_pokemmon.speed+this_turn_ai_atk.speed;//本轮电脑真正的速度              //计算伤害系数              p_coeff=damage_coeff(this_turn_p_atk.type,ai_pokemmon.type);              ai_coeff=damage_coeff(this_turn_ai_atk.type,p_pokemon.type);              //比较双方谁本轮速度快,判断先手              if(ai_speed>p_speed)              {                  cout <<"your opponent use "<<this_turn_ai_atk.name<<" !"<<endl;                  sleep(500);                  p_pokemon.hp=p_pokemon.hp-this_turn_ai_atk.damage*ai_coeff;//计算收到的伤害,伤害为招式伤害乘以伤害系数                  if(p_pokemon.hp<=0)                  {                      cout <<"you have been defeated!"<<endl;                  }                  else if(p_pokemon.hp>0)                  {                      cout <<"you use "<<this_turn_p_atk.name<<" !"<<endl;                      sleep(500);                      ai_pokemmon.hp=ai_pokemmon.hp-this_turn_p_atk.damage*p_coeff;                      if(ai_pokemmon.hp<=0)                      {                          cout <<"you win!"<<endl;                      }                      }              }              else if(ai_speed<p_speed)              {                  cout <<"you use "<<this_turn_p_atk.name<<" !"<<endl;                  sleep(500);                  ai_pokemmon.hp=ai_pokemmon.hp-this_turn_p_atk.damage*p_coeff;                  if(ai_pokemmon.hp<=0)                  {                      cout <<"you win!"<<endl;                  }                  else                  {                      cout <<"your opponent use "<<this_turn_ai_atk.name<<" !"<<endl;                      sleep(500);                      p_pokemon.hp=p_pokemon.hp-this_turn_ai_atk.damage*ai_coeff;                      if(p_pokemon.hp<=0)                      {                          cout <<"you have been defeated!"<<endl;                      }                  }              }              else if(ai_speed==p_speed)              {                  cout <<"you both attack at the same time!"<<endl;                  sleep(500);                  cout <<"you use "<<this_turn_p_atk.name<<" !"<<endl;                  sleep(500);                  cout <<" your opponent use "<<this_turn_ai_atk.name<<" !"<<endl;                  sleep(500);                  p_pokemon.hp=p_pokemon.hp-this_turn_ai_atk.damage*ai_coeff;                  ai_pokemmon.hp=ai_pokemmon.hp-this_turn_p_atk.damage*p_coeff;                  if(ai_pokemmon.hp<=0&&p_pokemon.hp>0)                  {                      cout <<"you win!"<<endl;                  }                  else if(ai_pokemmon.hp>0&&p_pokemon.hp<=0)                  {                      cout <<"you have been defeated!"<<endl;                  }                  else if(ai_pokemmon.hp<=0&&p_pokemon.hp<=0)                  {                      cout <<"draw game!";                  }              }          }      }  }

以上就是利用c++编写简易宝可梦对战小游戏的详细内容,更多关于c++宝可梦对战游戏的资料请关注<计算机技术网(www.ctvol.com)!!>其它相关文章!

需要了解更多c/c++开发分享利用C++编写简易宝可梦对战小游戏,都可以关注C/C++技术分享栏目—计算机技术网(www.ctvol.com)!

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

ctvol管理联系方式QQ:251552304

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

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

精彩推荐