C + + Object-oriented game model

Source: Internet
Author: User
Tags getmagic


A simple simulation of the "League of Legends" model, the first to abstract all heroes into hero, they have a lot of common attributes and attack characteristics, so first define an abstract class.

Since the notes have been written clearly, there is no need to repeat them.

#ifndef __hero__#define __hero__#define true1#define false0typedef enum Ability {ability_q = 0x0000000a, Ability_w = 0x000 0000b, ability_e = 0x0000000c, Ability_r = 0x0000000d} ability; Q,w,e,r Skill enumeration typedef INTHEROPARAM;TYPEDEF INTBOOL;//Hero Abstract class Hero{protected:heroparam _level;//class Heroparam _ experience;//experience BOOL _died;//whether death/* Consumption series Parameters */heroparam _max_health;//Max Health Heroparam _cur_health;//Current Health Heroparam _ health_up_speed;//Life recovery Speed (dots per second) Heroparam _max_mana;//maximum mana heroparam _cur_mana;//current Mana Heroparam _mana_up_speed;// Mana Recovery Speed (Dots/sec)/* Attack series Parameters */const Heroparam _effective_per_level;//per level growth attack (point/per level) Heroparam _effective;//Current Attack Heroparam _ effective_penetration;//current attack penetration Heroparam _crit;//critical strike Chance (0 to) Heroparam _magic;//current spell strength Heroparam _magic_penetration ;//Current spell penetration/* Defense series Parameters */const Heroparam _armor_per_level;//per level growth armor (point/per level) Heroparam _armor;//Current Armor Heroparam _resistance;// Current Magical resistance Public:hero (Heroparam max_health, Heroparam health_up_speed, Heroparam Max_mana, Heroparam mana_up_speed, const Heroparam effective_pEr_level, Heroparam effective, Heroparam Effective_penetration,heroparam Crit,heroparam magic, HEROPARAM magic_ Penetration, const Heroparam armor_per_level, Heroparam armor, heroparam resistance);//Construct a Hero virtual ~hero ();// Destruction Hero virtual void attack (Hero *desthero);//Common attack target hero virtual void Excute (Hero *desthero, ability what) = 0;//to target Hero release skills ( What is the specified skill, q,w,e,r four skills) virtual Heroparam geteffective ();//Gain Attack virtual Heroparam getarmor ();//Get Armor value virtual Heroparam Geteffectivepenetration ();//Get Armor Penetration value virtual Heroparam getcurhealth ();//Get current health value virtual void Putcurhealth (Heroparam Cur_health);//Set current health value virtual void die (Hero *srchero);//Death (killed by a hero) virtual const char* Getselfname (char *name) = 0;// Get the hero's own name virtual Heroparam getmagic ();//Get Spell strength virtual Heroparam getmagicpenetration ();//Get spell penetrating virtual Heroparam Getresistance ();//gain magical resistance}; #endif

Next, the non-pure virtual function in this abstract class is implemented

#include "stdafx.h" #include "Hero.h" Hero::hero (Heroparam max_health, Heroparam health_up_speed, Heroparam Max_mana, Heroparam mana_up_speed, const Heroparam effective_per_level, Heroparam effective, Heroparam effective_penetration,  Heroparam Crit, Heroparam Magic, Heroparam magic_penetration, const Heroparam armor_per_level, Heroparam armor, HEROPARAM Resistance): _effective_per_level (Effective_per_level), _armor_per_level (armor_per_level) {_level = 1;_experience = 0 ; _died = False;_max_health = Max_health;_cur_health = Max_health;_health_up_speed = Health_up_speed;_max_mana = Max_mana ; _cur_mana = Max_mana;_mana_up_speed = mana_up_speed;_effective = Effective;_effective_penetration = Effective_ Penetration;_crit = Crit;_magic = Magic;_magic_penetration = Magic_penetration;_armor = Armor;_resistance = resistance;} Hero::~hero () {}void hero::attack (Hero *desthero) {Heroparam armor = Desthero->getarmor ()-this-> Geteffectivepenetration (); Less armor after penetration armor = armor > 0? armor:0; NursingA cannot be negative heroparam effective = this->geteffective (); BOOL biscrited = FALSE; SYSTEMTIME St; Getlocaltime (&st); Srand ((unsigned) st.wmilliseconds); int num = rand ()% (100-1) + 1; Generate 1 to 100 to random number if (num <= _crit)//Decide whether to crit to see if the resulting random number is less than critical chance, the higher the critical chance, the easier it is to set up {effective <<= 1;//critical hit biscrited = TRUE ;} Heroparam lost_health = (int) ((float) effective * (+/(+ + (float) armor)); Target loss of health value, actual physical damage caused to you = own attack *[100/(100+ each other armor value)]heroparam Cur_health = Desthero->getcurhealth ()-lost_health; Remaining health value Char name[255];d esthero->getselfname (name); Get the target Hero name if (biscrited) {printf ("You normally attack"%s "and Crit!) \ n ", name);} else{printf ("You have normally attacked"%s ".) \ n ", name);} if (cur_health <= 0) {Desthero->die (this);//The health value is less than or equal to 0 for death printf ("You killed"%s "!) \ n ", name); _experience + = 50; Kill the Hero plus 50 points experience//experience value to 100 on upgrade if (_experience >=) {_experience = 0;printf ("You upgraded! The current level is%d. \ n ", _level);}} Else{desthero->putcurhealth (cur_health);//blood loss without death printf ("%s" loses%d health, and the remaining health is%d.) \ n ", name, Lost_health, cur_health);}} Heroparam Hero::geteffectivE () {return _effective;} Heroparam Hero::getarmor () {return _armor;} Heroparam hero::geteffectivepenetration () {return _effective_penetration;} Heroparam Hero::getcurhealth () {return _cur_health;} void Hero::p utcurhealth (Heroparam cur_health) {_cur_health = Cur_health;} void Hero::d ie (Hero *srchero) {char name[255];srchero->getselfname (name);//printf ("You were killed by"%s "!) \ n ", name);} Heroparam Hero::getmagic () {return _magic;} Heroparam hero::getmagicpenetration () {return _magic_penetration;} Heroparam hero::getresistance () {return _resistance;}


I do not know if this is the right time to calculate the critical chance, because each critical hit is a separate event, that is, if the luck is particularly good, it may be a critical hit every time, of course, I believe that no one will be so good luck!

In this way, the abstract class of hero is defined and implemented, and then a specific hero is derived from this class, such as now deriving a Ashe (Ash) class

Header file:

#ifndef __ashe__#define __ashe__#include "Hero.h"//Ice Shooter-Ash class Ashe:public Hero{public:ashe (Heroparam max_health, Heroparam Health_up_speed, Heroparam Max_mana, Heroparam mana_up_speed, const Heroparam Effective_per_level, HEROPARAM Effective, Heroparam Effective_penetration,heroparam crit, Heroparam Magic, Heroparam magic_penetration, const Heroparam Armor_per_level, Heroparam armor, heroparam resistance);//Constructs a ash virtual void Excute (Hero *desthero, ability what); Virtual const char* getselfname (char *name);}; #endif

Source file:

#include "stdafx.h" #include "Ashe.h" Ashe::ashe (Heroparam max_health, Heroparam health_up_speed, Heroparam Max_mana, Heroparam mana_up_speed, const Heroparam effective_per_level, Heroparam effective, Heroparam effective_penetration, Heroparam Crit,heroparam Magic, Heroparam magic_penetration, const Heroparam armor_per_level, Heroparam armor, HEROPARAM Resistance): Hero (Max_health, Health_up_speed, Max_mana, Mana_up_speed, Effective_per_level, effective, effective_ Penetration, Crit, magic, magic_penetration, Armor_per_level, armor, resistance) {}void Ashe::excute (Hero *desthero, Ability what) {char name[255];d esthero->getselfname (name); if (ability_q = = what) {printf ("[Ice shot]" slows down "%s". \ n ", name);} else if (Ability_w = = what)//w skill has the physical damage of 40+1 full damage to the target {if (_cur_mana <) {return;} _cur_mana-= 60; W Skill consumes 60 mana Heroparam armor = Desthero->getarmor ()-this->geteffectivepenetration (); armor = Armor > 0? armor:0; Armor cannot be negative heroparam lost_health = (int) ((+ + (float) this->geteffectivE ()) * (+/(+ + (float) armor))); Target loss of health value, actual physical damage caused to you =100/(100+ armor value) Heroparam Cur_health = Desthero->getcurhealth ()-lost_health; The remaining health value printf ("[million arrows]" slows down "%s". \ n ", name), if (cur_health <= 0) {Desthero->die (this);//health value is less than or equal to 0 for death printf (" You killed "%s"!) \ n ", name);//experience value to 100 upgrade if (_experience >=) {_experience = 0;printf (" You upgraded! The current level is%d. \ n ", _level);}} Else{desthero->putcurhealth (cur_health);//blood loss without death printf ("%s" loses%d health, and the remaining health is%d.) \ n ", name, Lost_health, cur_health);}} else if (ability_e = = what) {printf ("[Eagle] Sky" just illuminates "%s"). \ n ", name);} else if (Ability_r = = what)//r skill spells damage to the target 250+1 full spell strength {if (_cur_mana <) {return;} _cur_mana-= 100; R Skill consumes 100 mana heroparam resistance = desthero->getresistance ()-this->getmagic (); Minus magic Resistance after spell penetration resistance = resistance > 0? resistance:0; Magic Resistance cannot be negative heroparam lost_health = (int) ((+ + (float) this->getmagic ()) * (+/(+ + (float) resistance)); Target loss of health, actual magical damage to you =100/(100+ magical resistance) Heroparam Cur_health = Desthero->getcurhealth ()-Lost_health; The remaining health value printf ("[Magical crystal Sword]" hit "%s". \ n ", name), if (cur_health <= 0) {Desthero->die (this);//health value is less than or equal to 0 for death printf (" You killed "%s"!) \ n ", name), if (_experience >=) {_experience = 0;printf (" You upgraded! The current level is%d. \ n ", _level);}} Else{desthero->putcurhealth (cur_health);//blood loss without death printf ("%s" loses%d health, and the remaining health is%d.) \ n ", name, Lost_health, cur_health);}}} Const char* Ashe::getselfname (char *name) {return strcpy (name, "Ice Shooter");

The constructor of the Ashe class is constructed by calling the constructor of the parent class directly, because all the hero's basic attributes and attack characteristics are similar, but the values are different.

Finally, 2 ash are constructed, demonstrating a ash attack No. second Ash

LOLModel.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include "Ashe.h" int main (int a RGC, char* argv[]) {Hero *ashe1 = new Ashe (528, 5, 234, 7, 3, 51, 0, 55, 0, 0, 3, 22, 30);//Ash with critical chance Rune, assuming 55% critical hit Hero *ashe2 = new Ashe (528, 5, 234, 7, 3, 80, 0, 0, 0, 0, 3, 32, 40); Common runes of Ash ashe1->attack (ashe2); Sleep (Ashe1->excute); (Ashe2, Ability_w); Sleep (Ashe1->excute); (Ashe2, Ability_r); Sleep (Ashe1->attack); (ashe2); Sleep (Ashe1->attack); (ashe2); Sleep (Ashe1->attack); (ashe2); Sleep (Ashe1->attack); (ashe2); Sleep (Ashe1->excute) (Ashe2, Ability_w);d elete ashe1;delete Ashe2;return 0;}

A pointer to a parent class is used to point to a specific hero's object, and the function of the subclass object is invoked in a polymorphic manner.

In fact, there are many factors not considered, such as the target has been dead or can be attacked, such as life and mana reply is not considered. If you really want to consider, you can open a new 1 second trigger timer, each trigger to traverse all objects, the corresponding values are added to these objects. Not perfect too much, anyway ...

C + + Object-oriented game model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.