C + +: develop a game logic engine

Developing a game may seem very difficult, but with the help of C + +, it will become easier.
If you are an old hand in developing games, you should know the Unity engine. This engine not only provides logic support, but also provides UI support. It's all dirty.
However, we also want to develop a game engine. Can be achieved. But
Today, our protagonist is a pure logic engine, which means that we give logic (that is, function) support, not UI support.
Well, don't say so much, let's get to the point!

C or C++

Maybe many people prefer C + +. Because the function of C + + is much stronger than that of C. And the object-oriented of C + +. (this is very important!)
The object-oriented of C + + is very important. Why? An example is "seconds understand":

//C
#include <stdio.h>
int main(){
	int player1_hp=100,player1_agg=50;
	int player2_hp=120,player2_agg=20;
	printf("player1 Attacked player2");
	player1_hp-=player2_agg;
	printf("%d",player1_hp);
	return 0;
}

And C + +:

//C++
#include <iostream>
#include <string>
using namespace std;
class Player{
	public:
		string name="anonymous person";
		double hp=100.00;
		double agg=50.00;
		double dv=20.00;
	void agg_to(double *hp,double *dv){
		*hp-=agg-*dv;
	}
};
int main(){
	Player Player1;
	Player Player2;
	Player2.hp=120.00;
	Player1.agg_to(&Player2.hp,&Player2.dv);
	cout<<Player2.hp<<endl;
}

C + + pointer and object-oriented give us a good development method. We can use these new features to develop game logic engine faster and better!

Project statement

The copyright belongs to me and cannot be reproduced without permission.
It cannot be used for commercial purposes. You can contact the author to purchase the copyright and use it for commercial purposes.

Project development process

Tangled disease must see

We use the dev cppide development environment. Actually, I think it's enough. If you are afraid of disk explosion with vs, you are afraid of being unskilled with codeblock. After thinking about it, I finally decided to use dev CPP. You can use it or choose it yourself.

Start development

We will create a new project and a new file (game.h) and prepare to start development:

#include <iostream>
using namespace std;

After laying this foundation, let's write a class:

class Player{
	public:
		
		/* Particular attention */
		/* Some settings cannot use player directly XXX = XXX change! You need to use the specified function! Otherwise, it will lead to some chain effects that cannot be performed! */
		string name="Guest";  //The role name can be used as player Name = XXX change 
		double hp=0; //Health value must use player set_ HP settings, if you need to reduce, you need to use player reduce_ HP reduce the specified value and increase the need to use player plus_ HP increase 
		double dv=0; //Defense value must use player set_ DV setting, which automatically calculates and modifies when an attack is triggered 
		string buff="nothing BUFF"; //New!  You can set Buff to make the role stronger. This option is available for developers to call by themselves. You can use player Buff=xxx 
		double agg=0; //Attack power must use player set_ The AGG setting will be modified automatically 
		double chp=0; //Critical hit must use player set_ CHP settings will be modified automatically 
		double scp=0; //Super critical damage must use player set_ SCP settings will be modified automatically 
		/* Next, special attributes */ 
		double power=0; //Strength. Player must be used to increase the character's attack power and critical hit power through strength set_ Power modification 
		double endurance=0; //Endurance. The higher the endurance, the higher the HP and critical damage. You must use player set_ Endurance modification 
		double agile=0; //Agile. If the agility is high, the character can avoid critical hit with a higher probability. You must use player set_ agile 
		double intelligence=0; //Intelligence. If intelligence is high, agility and endurance will increase accordingly. You must use player set_ intelligence
		/* To initialize special properties, you can directly use player XXX = XXX modification */
		/* Preset */
		int player=0; //Player is required set_ Player (x) setting, optional: 1: swordsman 2: Archer 3: Giant
		int player_buff=0; //Use player set_ player_ Buff (x) setting, optional: 1: refreshing (endurance, strength, intelligence and agility increased by 10) 2: eating fast (usually after eating a certain food, intelligence and endurance increased by 50) 
		/* Detailed settings */
		double dwa=0; //The additional damage when receiving an attack can be modified directly and can be used in special levels
		double ms=0; //The moving speed can be modified directly for self calling
		int chr=0;//Critical hit possibility (1 low probability, 2 random, 3 high probability), player is required set_ Chr (x) setting
		/* Customize advanced parameters */
		bool obm=false; //Whether to turn on "thorn mode" (damage rebound) requires player set_ obm(True or False)
		bool survival=true; //Survival. Player is required set_ Survival (true or false) setting
		bool debug=false; //Whether the role starts debug Player is required set_ Debug (true or false) setting
		/* coordinate */ 
		double x=0; //Coordinate X is used for 2D/3D game positioning and can be modified directly 
		double y=0; //Coordinate Y is used for 2D/3D game positioning and can be modified directly 
		double z=0; //Coordinate Z,, for 3D game positioning, direct modification 
		/* Hierarchy */
		int lv=1; //The level can be modified directly, but there will be no attribute promotion. upgrade is required 

We must realize these basic functions. Otherwise, the logic engine will be meaningless. This engine can be used for many development purposes, optional.
Now that the framework has been set, the next step is to complete the functions one by one!

Life value

Health is a key part of the game. (abbreviated as hp) it is used to judge the survival of this role and the of this role - in the simplest Internet language, it is "whether the blood is thick or not".
The higher the health value, the outcome may not be decided. Suppose your HP is 500 and your opponent is 100, but your attack power is 10 and its attack power is 100. This is simply incomparable, isn't it?
Since health is so important, let's develop the first function: setting, reducing and increasing health.
Maybe you're wondering why you don't go straight to the player hp=xxx?
Because we have to add some "chain reaction"!

void set_hp(double sethp){
		hp=sethp;
		if(debug){
			cout<<"Role set"<<name<<"Your HP is"<<hp;
		}
	}	 
	void reduce_hp(double reducehp){
		hp-=reducehp;
		if(debug){
			cout<<"Will role"<<name<<"Your health has been subtracted"<<reducehp<<endl;
		}
	}
	void plus_hp(double plushp){
		hp+=plushp;
		if(debug){
			cout<<"Will role"<<name<<"Your health has been increased"<<plushp<<endl;
		}
	}

Carefully, my friend found that there was a debug option before. Yes, in order for developers to see the debug, they have to use function modification. (I said I was too difficult)

Defense value

Defense value is also very important! If you have 100HP, 50DV, 20AGG. The other party has 100HP, 0DV and 50AGG. Guess who wins and who loses?
Of course you won! Because - your 50DV is completely immune to the opponent's attack power (AGG).
Therefore, in order to ensure the rationality of the game, it is recommended that all developers pay attention to the DV setting when using the engine (although sometimes critical hits can break through the shield, but...)
Well, no more nonsense, code.

void set_dv(double setdv){
		dv=setdv;
		if(debug){
			cout<<"Will role"<<name<<"The defense value of is set to"<<dv<<endl;
		}
	}

Hahaha, it's Debug again. Do you feel speechless?

Custom BUFF

There are two buffs on it -- a buff and a player_buff. What's the difference between the two? Read the notes carefully and you should suddenly realize it.
The buff value can be called by the developer (for example, writing a buff feature), and the player_buff stores some buff effects that have been written for developers to call.
The word buff actually appeared very early. If you hit Fanyi baidu. COM, search... Light yellow!
Hehe, that's not what I mean. This word often appears on the Internet. It means a skill. Some parameters (such as agg) can be enhanced after acquisition
Well, there's no nonsense. Go to the code!
Alas, it's a mistake. It's written directly after the comment [laughter]. It's really confused.

aggressivity

"Every inch of money is worth an inch of attack, and every inch of money is hard to buy VIP." this sentence has been wildly spread among my friends recently (what I said...). Ha ha, this sentence well illustrates the role of attack power. The higher the attack power, the greater the possibility of winning. But it should also be analyzed in combination with DV and HP (I heard some people say HP is * Pu, not it) (I also heard some people say I do implant advertising, not it!)
All right, next, code!

void set_agg(double setagg){
		agg=setagg;
		if(debug){
			cout<<"Will role"<<name<<"Your attack power is set to"<<agg<<endl;
		}
		
	}

emmm... Continue debug ging

CHP AND SCP: critical power And super critical power!

Crit power is useless (I never watch crit...) why? Because the critical hit top has a lot of damage, but the possibility is very small.
"The wind blows a thousand lines of horsetail, and the critical hit is completely unfulfilled." This sentence may also be quite...... but it can also reflect the unimportance of critical hit (snickering).
All right, code!

void set_chp(double setchp){
		chp=setchp;
		if(debug){
			cout<<"Will role"<<name<<"Critical hit set to"<<agg<<endl;
		}
	}
	void set_scp(double setscp){
		scp=setscp;
		if(debug){
			cout<<"Will role"<<name<<"Super critical power set to"<<scp<<endl;
		}
	}

Ha ha, continue debugging, bang bang~

Special options

Playing games often has some special options. For example, whether you are hungry or not, lack of strength and energy are very common. Therefore, we have to achieve.
code!

void set_power(double setpower){
		power=setpower;
		agg+=setpower*5;
		chp+=setpower*5;
		if(debug){
			cout<<"Will role"<<name<<"Your strength is set to"<<power<<endl;
		}
	}
	void set_endurance(double setendurance){
		endurance=setendurance;
		hp+=setendurance*10;
		chp+=setendurance*5;
		if(debug){
			cout<<"Will role"<<name<<"Your endurance is set to"<<endurance<<endl;
		}
	}
	void set_agile(double setagile){
		agile=setagile;
		if(debug){
			cout<<"Will role"<<name<<"Agility is set to"<<agile<<endl;
		}
	}
	void set_intelligence(double setintelligence){
		intelligence=setintelligence;
		set_agile(agile+setintelligence*5);
		set_endurance(endurance+setintelligence*8);
		if(debug){
			cout<<"Will role"<<name<<"Your intelligence is set to"<<intelligence<<endl;
		}
	}

Wow, in addition to debug, there are new people at last. Good Happy~~

Preset

In order for game developers to test the engine first, we have prepared three character presets:

No.1 Swordsman
No.2 Archer
No.3 giant

(PS: Google translation is really delicious)
These three presets must be set by the function!

void set_player(int player){
		if(player==1){
			// Default character: swordsman 
			set_hp(100.00);
			set_agg(15.05);
			set_dv(5.25);
			name="Swordsman";
		}
		else if(player==2){
			// Default character: Archer
			set_hp(80.00);
			set_agg(22.00);
			set_dv(5.00);
			name="Archer"; 
		}
		else{
			// Default character: Giant 
			set_hp(500.50);
			set_agg(25.05);
			set_dv(0); 
			name="giant";
		}
	}

Then comes our own BUFF:

void set_player_buff(int buff){
		if(debug){
			cout<<name<<"The system comes with BUFF Changed to parameter:"<<buff<<endl;
		}
		if(buff==1){
			// refreshed
			set_agile(agile+10);
			set_endurance(endurance+10);
			set_intelligence(intelligence+10);
			set_power(power+10);
		}
		else if(buff==2){
			// glut oneself with delicacies 
			set_endurance(endurance+50);
			set_intelligence(intelligence+50);
		}else if(buff==-1){
			 // Restore the state before refreshing buff
			set_agile(agile-10);
			set_endurance(endurance-10);
			set_intelligence(intelligence-10);
			set_power(power-10);
		}else if(buff==-2){
			// Resume eating
			set_endurance(endurance-50);
			set_intelligence(intelligence-50);
		}
	}

Advanced features

Put on the code:

int set_chr(int setchr){
		// 1 very small 2 random 3 very large
		chr=setchr;
		return 1;
	}
	void set_obm(bool setobm){
		obm=setobm;
		if(debug){
			cout<<name<<"of OBM The value is set to"<<obm<<endl;
		}
	}
	int set_survival(bool setsurvival){
		if(setsurvival!=true){
			survival=false;
			if(debug){
				cout<<name<<"The survival value of is set to"<<setsurvival<<" This character has exited the game"<<endl;
			}
			
		}
		else{
			survival=true;
		}
		return survival;
	}
	int set_debug(bool setdebug){
		if(setdebug){
			debug=true;
		}
		return 1;
	}
	int ua(Player aPlayer){
		// Character attacked
		// Aplayer > another character of the attacking character
		if(survival && aPlayer.survival){
			double za=0; //The last attack
		za+=aPlayer.agg;
		za-=dv;
		za-=rand()%5;
		if(za<=0){
			if((rand()%5)==1){
				return -1; //-1 means immune 
			}
			// You have a chance to be immune. If you are not immune, your attack power will be increased
			za+=aPlayer.agg;
			za-=dv;
			za-=rand()%3;
			if(za<=0){
				// It's really impossible to calculate. If - 2 is returned, it means that the game is unfair and cannot be carried out
				if(debug){
					cout<<"It's unfair! The attack was disintegrated by the shield!"<<endl; 
				}
				return -2; 
			} 
		}
		// After setting up the last attack 
		// Judge critical hit and super critical hit
		srand((int)time(0));
		if(chp!=0){
		if(agile>10){
			//End critical hit 
		}else{
			
		
			if(chr==1){
			if(rand()%100==18){
				za=aPlayer.chp;
			}
		} 
		if(chr==2){
			int randnum=rand()%100;
			if(randnum==18 || randnum==12 || randnum==48 || randnum==69 || randnum==89 || randnum==99){
				za=aPlayer.chp;
			}
		} 
		if(chr==3){
			int randnum=rand()%100;
			if(randnum>50 && randnum<80){
				za=aPlayer.chp;
			}
		}}
		}
		int randnum2=rand()%100;
		if(aPlayer.scp!=0){
			if(randnum2==1){
			za=aPlayer.scp;
		}
		}
		
		if(debug){
			cout<<"role"<<name<<"About to be"<<za<<"Point attack."<<endl;
		}
		hp-=za;
		if(debug){
			cout<<"role"<<name<<"Your blood volume becomes"<<hp<<"Point."<<endl;
		}
		if(hp<=0){
			survival=false; //Auto set False
			if(debug){
				if(survival){
					cout<<name<<"In and"<<aPlayer.name<<"Died in the battle."<<endl;
				}
				
			} 
		}
		return 1;
		}
		else{
			if(debug){
				cout<<"role"<<name<<"Or role"<<aPlayer.name<<"He's dead"<<endl;
			}
		}
	}
	
	void print_all(){
		cout<<"PRINT_ALL"<<endl;
		/* Particular attention */
		/* Some settings cannot use player directly XXX = XXX change! You need to use the specified function! Otherwise, it will lead to some chain effects that cannot be performed! */
		cout<<"Role name:"<<name<<endl;  //The role name can be used as player Name = XXX change 
		cout<<"HP:"<<hp<<endl; //Health value must use player set_ HP settings, if you need to reduce, you need to use player reduce_ HP reduce the specified value and increase the need to use player plus_ HP increase 
		cout<<"Defense value:"<<dv<<endl; //Defense value must use player set_ DV setting, which automatically calculates and modifies when an attack is triggered 
		cout<<"custom BUFF: "<<buff<<endl; //New!  You can set Buff to make the role stronger. This option is available for developers to call by themselves. You can use player Buff=xxx 
		cout<<"aggressivity:"<<agg<<endl; //Attack power must use player set_ The AGG setting will be modified automatically 
		cout<<"Critical hit force:"<<chp<<endl; //Critical hit must use player set_ CHP settings will be modified automatically 
		cout<<"Super critical power:"<<scp<<endl; //Super critical damage must use player set_ SCP settings will be modified automatically 
		/* Next, special attributes */ 
		cout<<"Special attribute list:"<<endl<<power<<endl; //Strength. Player must be used to increase the character's attack power and critical hit power through strength set_ Power modification 
		cout<<endurance<<endl; //Endurance. The higher the endurance, the higher the HP and critical damage. You must use player set_ Endurance modification 
		cout<<agile<<endl; //Agile. If the agility is high, the character can avoid critical hit with a higher probability. You must use player set_ agile 
		cout<<intelligence<<endl; //Intelligence. If intelligence is high, agility and endurance will increase accordingly. You must use player set_ intelligence
		/* To initialize special properties, you can directly use player XXX = XXX modification */
		/* Preset */
		cout<<"Preset list:"<<endl<<player<<endl; //Player is required set_ Player (x) setting, optional: 1: swordsman 2: Archer 3: Giant
		cout<<player_buff<<endl; //Use player set_ player_ Buff (x) setting, optional: 1: refreshing (endurance, strength, intelligence and agility increased by 10) 2: eating fast (usually after eating a certain food, intelligence and endurance increased by 50) 
		/* Detailed settings */
		cout<<"Detailed setting list:"<<endl;
		cout<<dwa<<endl; //The additional damage when receiving an attack can be modified directly and can be used in special levels
		cout<<ms<<endl; //The moving speed can be modified directly for self calling
		cout<<chr<<endl;//Critical hit possibility (1 low probability, 2 random, 3 high probability), player is required set_ Chr (x) setting
		/* Customize advanced parameters */
		cout<<"Customize advanced parameters:"<<endl; 
		cout<<obm<<endl; //Whether to turn on "thorn mode" (damage rebound) requires player set_ obm(True or False)
		cout<<survival<<endl; //Survival. Player is required set_ Survival (true or false) setting
		cout<<debug<<endl; //Whether the role starts debug Player is required set_ Debug (true or false) setting
		/* coordinate */ 
		cout<<"Location:"<<endl;
		cout<<x<<endl; //Coordinate X is used for 2D/3D game positioning and can be modified directly 
		cout<<y<<endl; //Coordinate Y is used for 2D/3D game positioning and can be modified directly 
		cout<<z<<endl; //Coordinate Z,, for 3D game positioning, direct modification 
	}

Other roles

Now that the Player object has been perfectly completed, we have to write other things next.
Playing games, you are the most common
NPC!
All codes:

class npc{
	public:
		// NPC role
		// NPC function is not perfect, sorry 
		string name; //Name, use NPC Name = XX modify 
		double hp=0; //HP, use NPC set_ HP (x) modification 
		double agg=0; //Attack power, use NPC set_ AGG (x) modification 
		double x; //Coordinate X is used for 2D/3D game positioning and can be modified directly 
		double y; //Coordinate Y is used for 2D/3D game positioning and can be modified directly 
		double z; //Coordinate Z,, for 3D game positioning, direct modification
		string say[]; //The game interface can be set through NPC Say [x] easy access
		bool survival; //Self modification, used to judge survival in the game 
	void set_hp(double sethp){
		hp=sethp;
	}
	void set_agg(double setagg){
		agg=setagg;
	}
};

(PS: NPC only gives the framework, no other)
Well, and
goods!

class Prop{
	public:
		string name; //Item name (can be modified directly) 
		string introduce;//Introduction to props (can be modified directly) 
		int prid;//Prop ID (can be modified directly)
		/* Role changes after using props */
		string buff="nothing BUFF"; //Set what custom buffs the role will have (which can be modified directly)
		int player_buff=0; //Set what default buff will be added to the role (directly modified) (see player class for details)
		int power=0; //Prop must be used to increase strength and increase the character's attack power and critical hit power through strength set_ Power modification 
		int endurance=0; //The higher the endurance, the more damage the prop set_ Endurance modification 
		int agile=0; //Increase agility. If the agility is high, the character can avoid critical hit with a higher probability. Prop must be used set_ agile 
		int intelligence=0; //Increase intelligence. If intelligence is high, agility and endurance will increase accordingly. You must use prop set_ intelligence
		/* Use parameters */
		string use_type; //Add your type of use (e.g. beverage, food, medicine)
		int use_lv=0; //Restricted use level 
	void set_power(int setpower){
		power=setpower;
	}
	void set_endurance(int setendurance){
		endurance=setendurance;
	}
	void set_agile(int setagile){
		agile=setagile;
	}
	void set_intelligence(int setintelligence){
		intelligence=setintelligence;
	}
	int use(Player usePlayer){
		if(usePlayer.lv>=use_lv){
			usePlayer.buff=buff;
			usePlayer.player_buff=player_buff;
			usePlayer.set_power(usePlayer.power+power);
			usePlayer.set_intelligence(usePlayer.intelligence+intelligence);
			usePlayer.set_agile(usePlayer.agile+agile);
			usePlayer.set_endurance(usePlayer.endurance+endurance);
			if(usePlayer.debug){
				cout<<"role"<<usePlayer.name<<"Used props"<<name<<endl;
			}
			return 1;
		}
		else{
			return 0;//Insufficient grade 
		}
	}
	
}; 

complete

Well, the whole project has been completed (named EasyGame)
Version No.: 1.0.0
Open source to Gitee:https://gitee.com/tiantian520tt/easygame/
I'm 11, sorry for my shortcomings!

Tags: C++ Game Development

Posted by greepit on Thu, 19 May 2022 17:37:07 +0300