EX——4 RPG游戏·改 (圣杯战争不完全版)

2023-11-20 17:20

本文主要是介绍EX——4 RPG游戏·改 (圣杯战争不完全版),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

在接近一天的施工后,RPG游戏,基本施工完成,(4)修改 main 函数,随机生成不同角色的敌人,并保证程序正常运行。这一条会在过几天(也可能几周)的完全版发布。

我将原本的RPG游戏汉化了,并且加入了不少游戏的neta,看不懂也图个乐子( ̄▽ ̄)/

 

目录:

mian.cpp:主程序

container.h container.cpp:属性

player.h player.cpp:角色

swordsman.h swordsman.cpp: saber相关

archer.h archer.cpp: archer相关

mage.h mage.cpp: caster相关

 

 

main.cpp

  1 //=======================
  2 //        main.cpp
  3 //=======================
  4 
  5 // main function for the RPG style game
  6 
  7 #include <iostream>
  8 #include <string>
  9 using namespace std;
 10 
 11 #include "swordsman.h"
 12 #include"archer.h"
 13 #include"mege.h"
 14 
 15 int main()
 16 {
 17     string tempName;
 18     bool success=0;        //flag for storing whether operation is successful
 19     cout << "圣杯,是源于基督的传说的奇迹之遗物" << endl << "在基督教圈内,也有颇多追寻圣杯的旅行者们的传说流传着。"<<endl<<"然后在出现的圣杯被圣堂教会判定为“真”的时候,理所当然会爆发它的争夺战。" << endl << "为了追求万能的许愿机——圣杯而发生的战斗,就是" << endl << "圣杯战争。" << endl;
 20     system("pause");
 21     system("cls");
 22     cout <<"现在,你是一名御主,你的英灵名为: ";
 23     cin >>tempName;        // get player's name from keyboard input
 24     player *human=NULL;        // use pointer of base class, convenience for polymorphism
 25     int tempJob;        // temp choice for job selection
 26     do
 27     {
 28         cout <<"其职阶为: 1 剑士(Saber), 2 弓兵(Archer), 3 法师(Caster)"<<endl;
 29         cin>>tempJob;
 30         system("cls");        // clear the screen
 31         switch(tempJob)
 32         {
 33         case 1:
 34             human=new swordsman(1,tempName);    // create the character with user inputted name and job
 35             success=1;        // operation succeed
 36             break;
 37         case 2:
 38             human = new archer(1, tempName);    // create the character with user inputted name and job
 39             success = 1;        // operation succeed
 40             break;
 41         case 3:
 42             human = new mege(1, tempName);    // create the character with user inputted name and job
 43             success = 1;        // operation succeed
 44             break;
 45         default:
 46             break;                // In this case, success=0, character creation failed
 47         }
 48     }while(success!=1);        // so the loop will ask user to re-create a character
 49 
 50     int tempCom;            // temp command inputted by user
 51     int nOpp=0;                // the Nth opponent
 52     for(int i=1;nOpp<5;i+=2)    // i is opponent's level
 53     {
 54         nOpp++;
 55         system("cls");
 56         cout<<"回合" <<nOpp<<endl;
 57         cout<<"你的敌人为, 一个等级为 "<<i<<" 的剑士."<<endl;
 58         system("pause");
 59         swordsman enemy(i, "Warrior");    // Initialise an opponent, level i, name "Junior"
 60         human->reFill();                // get HP/MP refill before start fight
 61         
 62         while(!human->death() && !enemy.death())    // no died
 63         {
 64             success=0;
 65             while (success!=1)
 66             {
 67                 showinfo(*human,enemy);                // show fighter's information
 68                 cout<<"请御主下达指令: "<<endl;
 69                 cout<<"1 攻击; 2 宝具; 3 使用元素瓶; 4 使用元素灰瓶; 0 逃跑(退出游戏)"<<endl;
 70                 cin>>tempCom;
 71                 switch(tempCom)
 72                 {
 73                 case 0:
 74                     cout<<"你,抛弃你的荣耀 Y/N"<<endl;
 75                     char temp;
 76                     cin>>temp;
 77                     if(temp=='Y'||temp=='y')
 78                         return 0;
 79                     else
 80                         break;
 81                 case 1:
 82                     success=human->attack(enemy);
 83                     human->isLevelUp();
 84                     enemy.isDead();
 85                     break;
 86                 case 2:
 87                     success=human->specialatt(enemy);
 88                     human->isLevelUp();
 89                     enemy.isDead();
 90                     break;
 91                 case 3:
 92                     success=human->useHeal();
 93                     break;
 94                 case 4:
 95                     success=human->useMW();
 96                     break;
 97                 default:
 98                     break;
 99                 }
100             }
101             if(!enemy.death())        // If AI still alive
102                 enemy.AI(*human);
103             else                            // AI died
104             {
105                 cout<<"YOU WIN"<<endl;
106                 human->transfer(enemy);        // player got all AI's items
107             }
108             if (human->death())
109             {
110                 system("cls");
111                 cout<<endl<<setw(50)<<"GAME OVER"<<endl;
112                 delete human;        // player is dead, program is getting to its end, what should we do here?
113                 system("pause");
114                 return 0;
115             }
116         }
117     }
118     delete human;            // You win, program is getting to its end, what should we do here?
119     system("cls");
120     cout<<"Congratulations! 你成为了大圣杯的拥有者!!"<<endl;
121     system("pause");
122     return 0;
123 }

 

container.h

 1 //=======================
 2 //        container.h
 3 //=======================
 4 
 5 // The so-called inventory of a player in RPG games
 6 // contains two items, heal and magic water
 7 
 8 #ifndef _CONTAINER        // Conditional compilation
 9 #define _CONTAINER
10 
11 class container        // Inventory
12 {
13 protected:
14     int numOfHeal;            // number of heal
15     int numOfMW;            // number of magic water
16 public:
17     container();            // constuctor
18     void set(int heal_n, int mw_n);    // set the items numbers
19     int nOfHeal();            // get the number of heal
20     int nOfMW();            // get the number of magic water
21     void display();            // display the items;
22     bool useHeal();            // use heal
23     bool useMW();            // use magic water
24 };
25 
26 #endif

 

container.cpp

 1 //=======================
 2 //        container.cpp
 3 //=======================
 4 #include "container.h"
 5 #include <iostream>
 6 using namespace std;
 7 // default constructor initialise the inventory as empty
 8 container::container()
 9 {
10     set(0,0);
11 }
12 
13 // set the item numbers
14 void container::set(int heal_n, int mw_n)
15 {
16     numOfHeal=heal_n;
17     numOfMW=mw_n;
18 }
19 
20 // get the number of heal
21 int container::nOfHeal()
22 {
23     return numOfHeal;
24 }
25 
26 // get the number of magic water
27 int container::nOfMW()
28 {
29     return numOfMW;
30 }
31 
32 // display the items;
33 void container::display()
34 {
35     cout<<"你包裹中的物品: "<<endl;
36     cout<<"元素瓶(HP+100): "<<numOfHeal<<endl;
37     cout<<"元素灰瓶(MP+80): "<<numOfMW<<endl;
38 }
39 
40 //use heal
41 bool container::useHeal()
42 {
43     numOfHeal--;
44     return 1;        // use heal successfully
45 }
46 
47 //use magic water
48 bool container::useMW()
49 {
50     numOfMW--;
51     return 1;        // use magic water successfully
52 }

 

player.h

 1 //=======================
 2 //        player.h
 3 //=======================
 4 
 5 // The base class of player
 6 // including the general properties and methods related to a character
 7 
 8 #ifndef _PLAYER
 9 #define _PLAYER
10 
11 #include <iomanip>        // use for setting field width
12 #include <time.h>        // use for generating random factor
13 #include "container.h"
14 #include <string>
15 #include <windows.h>
16 using namespace std;
17 
18 enum job {sw, ar, mg};    /* define 3 jobs by enumerate type
19                                sword man, archer, mage */
20 class player
21 {
22     friend void showinfo(player &p1, player &p2);
23     friend class swordsman;
24     friend class archer;
25     friend class mege;
26 
27 protected:
28     int HP, HPmax, MP, MPmax, AP, DP, speed, EXP, LV;
29     // General properties of all characters
30     string name;    // character name
31     job role;        /* character's job, one of swordman, archer and mage,
32                        as defined by the enumerate type */
33     container bag;    // character's inventory
34 
35 public:
36     virtual bool attack(player &p)=0;    // normal attack
37     virtual bool specialatt(player &p)=0;    //special attack
38     virtual void isLevelUp()=0;            // level up judgement
39     /* Attention!
40     These three methods are called "Pure virtual functions".
41     They have only declaration, but no definition.
42     The class with pure virtual functions are called "Abstract class", which can only be used to inherited, but not to constructor objects. 
43     The detailed definition of these pure virtual functions will be given in subclasses. */
44 
45     void reFill();        // character's HP and MP resume
46     bool death();        // report whether character is dead
47     void isDead();        // check whether character is dead
48     bool useHeal();        // consume heal, irrelevant to job
49     bool useMW();        // consume magic water, irrelevant to job
50     void transfer(player &p);    // possess opponent's items after victory
51     void showRole();    // display character's job
52     
53 private:
54     bool playerdeath;            // whether character is dead, doesn't need to be accessed or inherited
55 };
56 
57 #endif

 

player.cpp

  1 //=======================
  2 //        player.cpp
  3 //=======================
  4 #include<iostream>
  5 #include"player.h"
  6 using namespace std;
  7 // character's HP and MP resume
  8 void player::reFill()
  9 {
 10     HP=HPmax;        // HP and MP fully recovered
 11     MP=MPmax;
 12 }
 13 
 14 // report whether character is dead
 15 bool player::death()
 16 {
 17     return playerdeath;
 18 }
 19 
 20 // check whether character is dead
 21 void player::isDead()
 22 {
 23     if(HP<=0)        // HP less than 0, character is dead
 24     {
 25         cout<<name<<" 阵亡." <<endl;
 26         system("pause");
 27         playerdeath=1;    // give the label of death value 1
 28     }
 29 }
 30 
 31 // consume heal, irrelevant to job
 32 bool player::useHeal()
 33 {
 34     if(bag.nOfHeal()>0)
 35     {
 36         HP=HP+100;
 37         if(HP>HPmax)        // HP cannot be larger than maximum value
 38             HP=HPmax;        // so assign it to HPmax, if necessary
 39         cout<<name<<" 喝下元素瓶, HP 回复了 100."<<endl;
 40         bag.useHeal();        // use heal
 41         system("pause");
 42         return 1;    // usage of heal succeed
 43     }
 44     else                // If no more heal in bag, cannot use
 45     {
 46         cout<<"元素瓶数量不足"<<endl;
 47         system("pause");
 48         return 0;    // usage of heal failed
 49     }
 50 }
 51 
 52 // consume magic water, irrelevant to job
 53 bool player::useMW()
 54 {
 55     if(bag.nOfMW()>0)
 56     {
 57         MP=MP+100;
 58         if(MP>MPmax)
 59             MP=MPmax;
 60         cout<<name<<" 喝下元素灰瓶, MP 回复了 100."<<endl;
 61         bag.useMW();
 62         system("pause");
 63         return 1;    // usage of magic water succeed
 64     }
 65     else
 66     {
 67         cout<<"元素灰瓶数量不足"<<endl;
 68         system("pause");
 69         return 0;    // usage of magic water failed
 70     }
 71 }
 72 
 73 // possess opponent's items after victory
 74 void player::transfer(player &p)
 75 {
 76     cout<<name<<" 获得"<<p.bag.nOfHeal()<<" 个元素瓶, 和 "<<p.bag.nOfMW()<<" 个元素灰瓶."<<endl;
 77     system("pause");
 78     bag.set(bag.nOfHeal() + p.bag.nOfHeal(), bag.nOfMW() + p.bag.nOfMW());
 79     // set the character's bag, get opponent's items
 80 }
 81 
 82 // display character's job
 83 void player::showRole()
 84 {
 85     switch(role)
 86     {
 87     case sw:
 88         cout<<"剑士";
 89         break;
 90     case ar:
 91         cout<<"弓兵";
 92         break;
 93     case mg:
 94         cout<<"法师";
 95         break;
 96     default:
 97         break;
 98     }
 99 }
100 
101 
102 // display character's job
103 void showinfo(player &p1, player &p2)
104 {
105     system("cls");
106     cout<<"##############################################################"<<endl;
107     cout<<"# 从者"<<setw(10)<<p1.name<<"   LV. "<<setw(3) <<p1.LV
108         <<"  # 敌人"<<setw(10)<<p2.name<<"   LV. "<<setw(3) <<p2.LV<<" #"<<endl;
109     cout<<"# HP "<<setw(3)<<(p1.HP<=999?p1.HP:999)<<'/'<<setw(3)<<(p1.HPmax<=999?p1.HPmax:999)
110         <<" | MP "<<setw(3)<<(p1.MP<=999?p1.MP:999)<<'/'<<setw(3)<<(p1.MPmax<=999?p1.MPmax:999)
111         <<"     # HP "<<setw(3)<<(p2.HP<=999?p2.HP:999)<<'/'<<setw(3)<<(p2.HPmax<=999?p2.HPmax:999)
112         <<" | MP "<<setw(3)<<(p2.MP<=999?p2.MP:999)<<'/'<<setw(3)<<(p2.MPmax<=999?p2.MPmax:999)<<"      #"<<endl;
113     cout<<"# AP "<<setw(3)<<(p1.AP<=999?p1.AP:999)
114         <<" | DP "<<setw(3)<<(p1.DP<=999?p1.DP:999)
115         <<" | speed "<<setw(3)<<(p1.speed<=999?p1.speed:999)
116         <<" # AP "<<setw(3)<<(p2.AP<=999?p2.AP:999)
117         <<" | DP "<<setw(3)<<(p2.DP<=999?p2.DP:999)
118         <<" | speed "<<setw(3)<<(p2.speed<=999?p2.speed:999)<<"  #"<<endl;
119     cout<<"# EXP"<<setw(7)<<p1.EXP<<" 职阶: "<<setw(7);
120     p1.showRole();
121     cout<<"   # EXP"<<setw(7)<<p2.EXP<<" 职阶: "<<setw(7);
122     p2.showRole();
123     cout<<"    #"<<endl;
124     cout<<"--------------------------------------------------------------"<<endl;
125     p1.bag.display();
126     cout<<"##############################################################"<<endl;
127 }

 

swordsman.h

 1 //=======================
 2 //        swordsman.h
 3 //=======================
 4 
 5 // Derived from base class player
 6 // For the job Swordsman
 7 
 8 #include "player.h"
 9 class swordsman : public player        // subclass swordsman publicly inherited from base player
10 {
11 public:
12     swordsman(int lv_in=1, string name_in="Not Given");    
13         // constructor with default level of 1 and name of "Not given"
14     void isLevelUp();
15     bool attack (player &p);
16     bool specialatt(player &p);
17         /* These three are derived from the pure virtual functions of base class
18            The definition of them will be given in this subclass. */
19     void AI(player &p);                // Computer opponent
20 };

swordsman.cpp

  1 //=======================
  2 //        swordsman.cpp
  3 //=======================
  4 #include<iostream>
  5 #include"swordsman.h"
  6 using namespace std;
  7 // constructor. default values don't need to be repeated here
  8 swordsman::swordsman(int lv_in, string name_in)
  9 {
 10     role=sw;    // enumerate type of job
 11     LV=lv_in;
 12     name=name_in;
 13     
 14     // Initialising the character's properties, based on his level
 15     HPmax=150+8*(LV-1);        // HP increases 8 point2 per level
 16     HP=HPmax;
 17     MPmax=75+2*(LV-1);        // MP increases 2 points per level
 18     MP=MPmax;
 19     AP=25+4*(LV-1);            // AP increases 4 points per level
 20     DP=25+4*(LV-1);            // DP increases 4 points per level
 21     speed=25+2*(LV-1);        // speed increases 2 points per level
 22     
 23     playerdeath=0;
 24     EXP=LV*LV*75;
 25     bag.set(lv_in, lv_in);
 26 }
 27 
 28 void swordsman::isLevelUp()
 29 {
 30     if(EXP>=LV*LV*75)
 31     {
 32         LV++;
 33         AP+=4;
 34         DP+=4;
 35         HPmax+=8;
 36         MPmax+=2;
 37         speed+=2;
 38         cout<<name<<" 等级提升!"<<endl;
 39         cout<<"HP 提升了 8 点 达到了 "<<HPmax<<endl;
 40         cout<<"MP 提升了 2 点 达到了 "<<MPmax<<endl;
 41         cout<<"Speed 提升了 2 点 达到了 "<<speed<<endl;
 42         cout<<"AP 提升了 4 点 达到了 "<<AP<<endl;
 43         cout<<"DP 提升了 5 点 达到了 "<<DP<<endl;
 44         system("pause");
 45         isLevelUp();    // recursively call this function, so the character can level up multiple times if got enough exp
 46     }
 47 }
 48 
 49 bool swordsman::attack(player &p)
 50 {
 51     double HPtemp=0;        // opponent's HP decrement
 52     double EXPtemp=0;        // player obtained exp
 53     double hit=1;            // attach factor, probably give critical attack
 54     srand((unsigned)time(NULL));        // generating random seed based on system time
 55 
 56     // If speed greater than opponent, you have some possibility to do double attack
 57     if ((speed>p.speed) && (rand()%100<(speed-p.speed)))        // rand()%100 means generates a number no greater than 100
 58     {
 59         HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+25));        // opponent's HP decrement calculated based their AP/DP, and uncertain chance
 60         cout<<name<<"的攻击命中了 "<<p.name<<", "<<p.name<<"的 HP 减少了 "<<HPtemp<<endl;
 61         p.HP=int(p.HP-HPtemp);
 62         EXPtemp=(int)(HPtemp*1.2);
 63     }
 64 
 65     // If speed smaller than opponent, the opponent has possibility to evade
 66     if ((speed<p.speed) && (rand()%50<1))
 67     {
 68         cout<<name<<"的攻击被  "<<p.name<<" 闪避了"<<endl;
 69         system("pause");
 70         return 1;
 71     }
 72 
 73     // 10% chance give critical attack
 74     if (rand()%100<=10)
 75     {
 76         hit=1.5;
 77         cout<<"命中要害: ";
 78     }
 79 
 80     // Normal attack
 81     HPtemp=(int)((1.0*AP/p.DP)*AP*5/(rand()%4+10));
 82     cout<<name<<" 使用 直刺, "<<p.name<<"的 HP 减少了 "<<HPtemp<<endl;
 83     EXPtemp=(int)(EXPtemp+HPtemp*1.2);
 84     p.HP=(int)(p.HP-HPtemp);
 85     cout<<name<<" 获得 "<<EXPtemp<<" 点经验值."<<endl;
 86     EXP=(int)(EXP+EXPtemp);
 87     system("pause");
 88     return 1;        // Attack success
 89 }
 90 
 91 bool swordsman::specialatt(player &p)
 92 {
 93     if(MP<35)
 94     {
 95         cout<<"你没有足够的魔力发动宝具!"<<endl;
 96         system("pause");
 97         return 0;        // Attack failed
 98     }
 99     else
100     {
101         MP-=35;            // consume 35 MP to do special attack
102         
103         //10% chance opponent evades
104         if(rand()%100<=10)
105         {
106             cout<<name<<"的宝具被 "<<p.name<<" 规避"<<endl;
107             system("pause");
108             return 1;
109         }
110         
111         double HPtemp=0;        
112         double EXPtemp=0;        
113         //double hit=1;            
114         //srand(time(NULL));        
115         HPtemp=(int)(AP*1.3+20);        // not related to opponent's DP
116         EXPtemp=(int)(HPtemp*1.5);        // special attack provides more experience
117         cout<<name<<" 发动了————修罗邪光斩, "<<p.name<<"的 HP 减少了 "<<HPtemp<<endl;
118         cout<<name<<" 获得 "<<EXPtemp<<" 点经验值."<<endl;
119         p.HP=(int)(p.HP-HPtemp);
120         EXP=(int)(EXP+EXPtemp);
121         system("pause");
122     }
123     return 1;    // special attack succeed
124 }
125 
126 // Computer opponent
127 void swordsman::AI(player &p)
128 {
129     if ((HP<(int)((1.0*p.AP/DP)*p.AP*1.5))&&(HP+100<=1.1*HPmax)&&(bag.nOfHeal()>0)&&(HP>(int)((1.0*p.AP/DP)*p.AP*0.5)))
130         // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
131     {
132         useHeal();
133     }
134     else
135     {
136         if(MP>=40 && HP>0.5*HPmax && rand()%100<=30)
137             // AI has enough MP, it has 30% to make special attack
138         {
139             specialatt(p);
140             p.isDead();        // check whether player is dead
141         }
142         else
143         {
144             if (MP<40 && HP>0.5*HPmax && bag.nOfMW())
145                 // Not enough MP && HP is safe && still has magic water
146             {
147                 useMW();
148             }
149             else
150             {
151                 attack(p);    // normal attack
152                 p.isDead();
153             }
154         }
155     }
156 }

 

archer.h

 1 #include "player.h"
 2 class archer :public player
 3 {
 4 public:
 5     archer(int lv_in = 1, string name_in = "Not Given");
 6     // constructor with default level of 1 and name of "Not given"
 7     void isLevelUp();
 8     bool attack(player &p);
 9     bool specialatt(player &p);
10     /* These three are derived from the pure virtual functions of base class
11     The definition of them will be given in this subclass. */
12     void AI(player &p);                // Computer opponent
13 };

 

archer.cpp

  1 //=======================
  2 //        archer.cpp
  3 //=======================
  4 #include<iostream>
  5 #include"archer.h"
  6 using namespace std;
  7 // constructor. default values don't need to be repeated here
  8 archer::archer(int lv_in, string name_in)
  9 {
 10     role = sw;    // enumerate type of job
 11     LV = lv_in;
 12     name = name_in;
 13 
 14     // Initialising the character's properties, based on his level
 15     HPmax = 135 + 5 * (LV - 1);        // HP increases 5 point2 per level
 16     HP = HPmax;
 17     MPmax = 85 + 5 * (LV - 1);        // MP increases 5 points per level
 18     MP = MPmax;
 19     AP = 25 + 4 * (LV - 1);            // AP increases 4 points per level
 20     DP = 25 + 4 * (LV - 1);            // DP increases 4 points per level
 21     speed = 40 + 8 * (LV - 1);        // speed increases 8 points per level
 22 
 23     playerdeath = 0;
 24     EXP = LV*LV * 75;
 25     bag.set(lv_in, lv_in);
 26 }
 27 
 28 void archer::isLevelUp()
 29 {
 30     if (EXP >= LV*LV * 75)
 31     {
 32         LV++;
 33         AP += 4;
 34         DP += 4;
 35         HPmax += 5;
 36         MPmax += 5;
 37         speed += 8;
 38         cout << name << " 等级提升!" << endl;
 39         cout << "HP 提升了 5 点 达到了 " << HPmax << endl;
 40         cout << "MP 提升了 5 点 达到了 " << MPmax << endl;
 41         cout << "Speed 提升了 8 点 达到了 " << speed << endl;
 42         cout << "AP 提升了 4 点 达到了 " << AP << endl;
 43         cout << "DP 提升了 4 点 达到了 " << DP << endl;
 44         system("pause");
 45         isLevelUp();    // recursively call this function, so the character can level up multiple times if got enough exp
 46     }
 47 }
 48 
 49 bool archer::attack(player &p)
 50 {
 51     double HPtemp = 0;        // opponent's HP decrement
 52     double EXPtemp = 0;        // player obtained exp
 53     double hit = 1;            // attach factor, probably give critical attack
 54     srand((unsigned)time(NULL));        // generating random seed based on system time
 55 
 56                                         // If speed greater than opponent, you have some possibility to do double attack
 57     if ((speed>p.speed) && (rand() % 100<(speed - p.speed)))        // rand()%100 means generates a number no greater than 100
 58     {
 59         HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 15));        // opponent's HP decrement calculated based their AP/DP, and uncertain chance
 60         cout << name << "的攻击命中了 " << p.name << ", " << p.name << "的 HP 减少了 " << HPtemp << endl;
 61         p.HP = int(p.HP - HPtemp);
 62         EXPtemp = (int)(HPtemp*1.2);
 63     }
 64 
 65     // If speed smaller than opponent, the opponent has possibility to evade
 66     if ((speed<p.speed) && (rand() % 50<1))
 67     {
 68         cout << name << "的攻击被  " << p.name << " 闪避了" << endl;
 69         system("pause");
 70         return 1;
 71     }
 72 
 73     // 10% chance give critical attack
 74     if (rand() % 100 <= 10)
 75     {
 76         hit = 1.5;
 77         cout << "命中要害: ";
 78     }
 79 
 80     // Normal attack
 81     HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 10));
 82     cout << name << " 使用 三连射, " << p.name << "的 HP 减少了 " << HPtemp << endl;
 83     EXPtemp = (int)(EXPtemp + HPtemp*1.2);
 84     p.HP = (int)(p.HP - HPtemp);
 85     cout << name << " 获得 " << EXPtemp << " 点经验值." << endl;
 86     EXP = (int)(EXP + EXPtemp);
 87     system("pause");
 88     return 1;        // Attack success
 89 }
 90 
 91 bool archer::specialatt(player &p)
 92 {
 93     if (MP<40)
 94     {
 95         cout << "你没有足够的魔力发动宝具!" << endl;
 96         system("pause");
 97         return 0;        // Attack failed
 98     }
 99     else
100     {
101         MP -= 40;            // consume 40 MP to do special attack
102 
103                             //10% chance opponent evades
104         if (rand() % 100 <= 10)
105         {
106             cout << name << "的宝具被 " << p.name << " 规避" << endl;
107             system("pause");
108             return 1;
109         }
110 
111         double HPtemp = 0;
112         double EXPtemp = 0;
113         //double hit=1;            
114         //srand(time(NULL));        
115         HPtemp = (int)(AP*1.2 + 30);        // not related to opponent's DP
116         EXPtemp = (int)(HPtemp*1.5);        // special attack provides more experience
117         cout << name << " 发动了————流星一条(笑), " << p.name << "的 HP 减少了 " << HPtemp << endl;
118         cout << name << " 获得 " << EXPtemp << " 点经验值." << endl;
119         p.HP = (int)(p.HP - HPtemp);
120         EXP = (int)(EXP + EXPtemp);
121         system("pause");
122     }
123     return 1;    // special attack succeed
124 }
125 
126 // Computer opponent
127 void archer::AI(player &p)
128 {
129     if ((HP<(int)((1.0*p.AP / DP)*p.AP*1.5)) && (HP + 100 <= 1.1*HPmax) && (bag.nOfHeal()>0) && (HP>(int)((1.0*p.AP / DP)*p.AP*0.5)))
130         // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
131     {
132         useHeal();
133     }
134     else
135     {
136         if (MP >= 40 && HP>0.5*HPmax && rand() % 100 <= 30)
137             // AI has enough MP, it has 30% to make special attack
138         {
139             specialatt(p);
140             p.isDead();        // check whether player is dead
141         }
142         else
143         {
144             if (MP<40 && HP>0.5*HPmax && bag.nOfMW())
145                 // Not enough MP && HP is safe && still has magic water
146             {
147                 useMW();
148             }
149             else
150             {
151                 attack(p);    // normal attack
152                 p.isDead();
153             }
154         }
155     }
156 }

 

mege.h

 1 #include "player.h"
 2 class mege : public player        
 3 {
 4 public:
 5     mege(int lv_in = 1, string name_in = "Not Given");
 6     // constructor with default level of 1 and name of "Not given"
 7     void isLevelUp();
 8     bool attack(player &p);
 9     bool specialatt(player &p);
10     /* These three are derived from the pure virtual functions of base class
11     The definition of them will be given in this subclass. */
12     void AI(player &p);                // Computer opponent
13 };

 

mege.cpp

  1 //=======================
  2 //        mege.cpp
  3 //=======================
  4 #include<iostream>
  5 #include"mege.h"
  6 using namespace std;
  7 // constructor. default values don't need to be repeated here
  8 mege::mege(int lv_in, string name_in)
  9 {
 10     role = sw;    // enumerate type of job
 11     LV = lv_in;
 12     name = name_in;
 13 
 14     // Initialising the character's properties, based on his level
 15     HPmax = 100 + 2 * (LV - 1);        // HP increases 2 point2 per level
 16     HP = HPmax;
 17     MPmax = 150 + 8 * (LV - 1);        // MP increases 8 points per level
 18     MP = MPmax;
 19     AP = 25 + 5 * (LV - 1);            // AP increases 2 points per level
 20     DP = 25 + 3 * (LV - 1);            // DP increases 6 points per level
 21     speed = 30 + 3 * (LV - 1);        // speed increases 3 points per level
 22 
 23     playerdeath = 0;
 24     EXP = LV*LV * 75;
 25     bag.set(lv_in, lv_in);
 26 }
 27 
 28 void mege::isLevelUp()
 29 {
 30     if (EXP >= LV*LV * 75)
 31     {
 32         LV++;
 33         AP += 5;
 34         DP += 3;
 35         HPmax += 2;
 36         MPmax += 8;
 37         speed += 3;
 38         cout << name << " 等级提升!" << endl;
 39         cout << "HP 提升了 2 点 达到了 " << HPmax << endl;
 40         cout << "MP 提升了 8 点 达到了 " << MPmax << endl;
 41         cout << "Speed 提升了 3 点 达到了 " << speed << endl;
 42         cout << "AP 提升了 5 点 达到了 " << AP << endl;
 43         cout << "DP 提升了 3 点 达到了 " << DP << endl;
 44         system("pause");
 45         isLevelUp();    // recursively call this function, so the character can level up multiple times if got enough exp
 46     }
 47 }
 48 
 49 bool mege::attack(player &p)
 50 {
 51     double HPtemp = 0;        // opponent's HP decrement
 52     double EXPtemp = 0;        // player obtained exp
 53     double hit = 1;            // attach factor, probably give critical attack
 54     srand((unsigned)time(NULL));        // generating random seed based on system time
 55 
 56                                         // If speed greater than opponent, you have some possibility to do double attack
 57     if ((speed>p.speed) && (rand() % 100<(speed - p.speed)))        // rand()%100 means generates a number no greater than 100
 58     {
 59         HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 10));        // opponent's HP decrement calculated based their AP/DP, and uncertain chance
 60         cout << name << "的攻击命中了 " << p.name << ", " << p.name << "的 HP 减少了 " << HPtemp << endl;
 61         p.HP = int(p.HP - HPtemp);
 62         EXPtemp = (int)(HPtemp*1.2);
 63     }
 64 
 65     // If speed smaller than opponent, the opponent has possibility to evade
 66     if ((speed<p.speed) && (rand() % 50<1))
 67     {
 68         cout << name << "的攻击被  " << p.name << " 闪避了" << endl;
 69         system("pause");
 70         return 1;
 71     }
 72 
 73     // 10% chance give critical attack
 74     if (rand() % 100 <= 10)
 75     {
 76         hit = 1.5;
 77         cout << "命中要害: ";
 78     }
 79 
 80     // Normal attack
 81     HPtemp = (int)((1.0*AP / p.DP)*AP * 5 / (rand() % 4 + 10));
 82     cout << name << " 使用 封魔针, " << p.name << "的 HP 减少了 " << HPtemp << endl;
 83     EXPtemp = (int)(EXPtemp + HPtemp*1.2);
 84     p.HP = (int)(p.HP - HPtemp);
 85     cout << name << " 获得 " << EXPtemp << " 点经验值." << endl;
 86     EXP = (int)(EXP + EXPtemp);
 87     system("pause");
 88     return 1;        // Attack success
 89 }
 90 
 91 bool mege::specialatt(player &p)
 92 {
 93     if (MP<60)
 94     {
 95         cout << "你没有足够的魔力发动宝具!" << endl;
 96         system("pause");
 97         return 0;        // Attack failed
 98     }
 99     else
100     {
101         MP -= 60;            // consume 60 MP to do special attack
102 
103                             //10% chance opponent evades
104         if (rand() % 100 <= 10)
105         {
106             cout << name << "的宝具被 " << p.name << " 规避" << endl;
107             system("pause");
108             return 1;
109         }
110 
111         double HPtemp = 0;
112         double EXPtemp = 0;
113         //double hit=1;            
114         //srand(time(NULL));        
115         HPtemp = (int)(AP*2.0 + 20);        // not related to opponent's DP
116         EXPtemp = (int)(HPtemp*1.5);        // special attack provides more experience
117         cout << name << " 发动了————梦想封印, " << p.name << "的 HP 减少了 " << HPtemp << endl;
118         cout << name << " 获得 " << EXPtemp << " 点经验值." << endl;
119         p.HP = (int)(p.HP - HPtemp);
120         EXP = (int)(EXP + EXPtemp);
121         system("pause");
122     }
123     return 1;    // special attack succeed
124 }
125 
126 // Computer opponent
127 void mege::AI(player &p)
128 {
129     if ((HP<(int)((1.0*p.AP / DP)*p.AP*1.5)) && (HP + 100 <= 1.1*HPmax) && (bag.nOfHeal()>0) && (HP>(int)((1.0*p.AP / DP)*p.AP*0.5)))
130         // AI's HP cannot sustain 3 rounds && not too lavish && still has heal && won't be killed in next round
131     {
132         useHeal();
133     }
134     else
135     {
136         if (MP >= 60 && HP>0.5*HPmax && rand() % 100 <= 30)
137             // AI has enough MP, it has 30% to make special attack
138         {
139             specialatt(p);
140             p.isDead();        // check whether player is dead
141         }
142         else
143         {
144             if (MP<60 && HP>0.5*HPmax && bag.nOfMW())
145                 // Not enough MP && HP is safe && still has magic water
146             {
147                 useMW();
148             }
149             else
150             {
151                 attack(p);    // normal attack
152                 p.isDead();
153             }
154         }
155     }
156 }

转载于:https://www.cnblogs.com/zhibifenli/p/9130637.html

这篇关于EX——4 RPG游戏·改 (圣杯战争不完全版)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/396209

相关文章

Python开发围棋游戏的实例代码(实现全部功能)

《Python开发围棋游戏的实例代码(实现全部功能)》围棋是一种古老而复杂的策略棋类游戏,起源于中国,已有超过2500年的历史,本文介绍了如何用Python开发一个简单的围棋游戏,实例代码涵盖了游戏的... 目录1. 围棋游戏概述1.1 游戏规则1.2 游戏设计思路2. 环境准备3. 创建棋盘3.1 棋盘类

国产游戏崛起:技术革新与文化自信的双重推动

近年来,国产游戏行业发展迅猛,技术水平和作品质量均得到了显著提升。特别是以《黑神话:悟空》为代表的一系列优秀作品,成功打破了过去中国游戏市场以手游和网游为主的局限,向全球玩家展示了中国在单机游戏领域的实力与潜力。随着中国开发者在画面渲染、物理引擎、AI 技术和服务器架构等方面取得了显著进展,国产游戏正逐步赢得国际市场的认可。然而,面对全球游戏行业的激烈竞争,国产游戏技术依然面临诸多挑战,未来的

火柴游戏java版

代码 /*** 火柴游戏* <p>* <li>有24根火柴</li>* <li>组成 A + B = C 等式</li>* <li>总共有多少种适合方式?</li>* <br>* <h>分析:</h>* <li>除去"+"、"="四根,最多可用火柴根数20根。</li>* <li>全部用两根组合成"1",最大数值为1111。使用枚举法,A和B范围在0~1111,C为A+B。判断</li>** @

国产游戏行业的崛起与挑战:技术创新引领未来

国产游戏行业的崛起与挑战:技术创新引领未来 近年来,国产游戏行业蓬勃发展,技术水平不断提升,许多优秀作品在国际市场上崭露头角。从画面渲染到物理引擎,从AI技术到服务器架构,国产游戏已实现质的飞跃。然而,面对全球游戏市场的激烈竞争,国产游戏技术仍然面临诸多挑战。本文将探讨这些挑战,并展望未来的机遇,深入分析IT技术的创新将如何推动行业发展。 国产游戏技术现状 国产游戏在画面渲染、物理引擎、AI

第四次北漂----挣个独立游戏的素材钱

第四次北漂,在智联招聘上,有个小公司主动和我联系。面试了下,决定入职了,osg/osgearth的。月薪两万一。 大跌眼镜的是,我入职后,第一天的工作内容就是接手他的工作,三天后他就离职了。 我之所以考虑入职,是因为 1,该公司有恒歌科技的freex平台源码,可以学学,对以前不懂的解解惑。 2,挣点素材钱,看看张亮002的视频,他用了6000多,在虚幻商城买的吸血鬼游戏相关的素材,可以玩两年。我

nyoj 1038 纸牌游戏

poj 的一道改编题,说是翻译题更恰当,因为只是小幅度改动。 一道模拟题,代码掌控能力比较好,思维逻辑清晰的话就能AC。 代码如下: #include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct node{char c[5];int rk;char da[5];int nu

如果出一个名叫白神话悟空的游戏

最近黑神话由于与原著不符引起了原著派的争议。 所以我在摸鱼的时候想到如果游科或者某个别的公司“痛改前非”不夹带私货完全复刻吴承恩百回版剧情制作一个“重走西游路”的游戏,会有一个什么样的销量?(设定为原著派已经多方渠道认证,此游戏的确没有夹带私货,绝大部分复刻了原著剧情) 游戏玩法我想了几类 超长线性有岔路蜈蚣形状地图,蜈蚣的腿部是探索区域和支线,重走西游路线,开篇就是开始取经前唐玄宗御弟cg

《黑暗之魂2:原罪学者》是什么类型的游戏 《黑暗之魂》可以在苹果Mac电脑上玩吗?

在宏大的世界观游戏中,《黑暗之魂2:原罪学者》脱颖而出,以其探索性和挑战性征服了全球玩家的心灵。下面我们来看看《黑暗之魂2:原罪学者》是什么类型的游戏,《黑暗之魂2:原罪学者》可以在苹果电脑玩吗的相关内容。 一、《黑暗之魂2:原罪学者》是什么类型的游戏 《黑暗之魂2:原罪学者》作为《黑暗之魂2》的增强版和重制版,是一款FromSoftware制作、BANDAI NAMCO和FromSoft

简单取石子游戏~博弈

很坑爹的小游戏,至于怎么坑爹,嘎嘎~自己研究去吧~! #include<stdio.h>#include<windows.h>#include<iostream>#include<string.h>#include<time.h>using namespace std;void Loc(int x,int y);/*定位光标*/void Welcome(); /*创建欢迎界面*/

黑神话:悟空》增加草地绘制距离MOD使游戏场景看起来更加广阔与自然,增强了游戏的沉浸式体验

《黑神话:悟空》增加草地绘制距离MOD为玩家提供了一种全新的视觉体验,通过扩展游戏中草地的绘制距离,增加了场景的深度和真实感。该MOD通过增加草地的绘制距离,使游戏场景看起来更加广阔与自然,增强了游戏的沉浸式体验。 增加草地绘制距离MOD安装 1、在%userprofile%AppDataLocalb1SavedConfigWindows目录下找到Engine.ini文件。 2、使用记事本编辑