文章目录
入门小伙伴可先熟悉该改代码,掌握逻辑后在自行敲打。😁
首先创建武器类,武器类衍生了屠龙刀类以及小刀类,在创建英雄和怪兽即可。
一、头文件
1.1 Weapon.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
/*@ 武器类【抽象类】 */
class Weapon
{
public:
/*@ 基础伤害 */
virtual int getBaseDamage() = 0;
/*@ 吸血 */
virtual int getSuckBlood() = 0;
/*@ 是否定身 */
virtual bool getHold() = 0;
/*@ 是否暴击 */
virtual bool getCrit() = 0;
string m_WeaponName; // 武器名称
int m_BaseDamage; // 武器基础伤害值
};
1.2 DragonSword .h
#pragma once
#include "Weapon.h"
/*@ 屠龙刀类,继承武器类 */
class DragonSword :public Weapon
{
public:
DragonSword();
/*@ 基础伤害 */
virtual int getBaseDamage();
/*@ 吸血 */
virtual int getSuckBlood();
/*@ 是否定身 */
virtual bool getHold();
/*@ 是否暴击 */
virtual bool getCrit();
/*@ 是否触发 */
bool isTrigger(int rate);
int suckRate; // 吸血率
int holdRate; // 定身率
int critRate; // 暴力率
};
1.3 Knife.h
#pragma once
#include "Weapon.h"
/*@ 小刀类,继承武器类 */
class Knife :public Weapon
{
public:
Knife();
/*@ 基础伤害 */
virtual int getBaseDamage();
/*@ 吸血 */
virtual int getSuckBlood();
/*@ 是否定身 */
virtual bool getHold();
/*@ 是否暴击 */
virtual bool getCrit();
};
1.4 Monster.h
#pragma once
#include<iostream>
#include<string>
#include "Hero.h"
using namespace std;
class Monster
{
public:
Monster();
string m_MonsterName;
int m_HP;
int m_Mp;
int m_Atk;
int m_Def;
bool m_Hold;
void Attack(Hero *hero);
};
1.5 Hero.h
#pragma once
#include "Weapon.h"
class Monster;
class Hero
{
public:
Hero();
string H_HeroName; // 英雄名称
int H_Atk; // 攻击力
int H_Def; // 防御值
int H_Hp; // 血量
int H_Mp; // 魔法值
Weapon *weapon; // 武器
/*@ 使用武器 */
void EquipWeapon(Weapon *weapon);
/*@ 攻击 */
void Attack(Monster *monster);
};
二、源文件
2.1 DragonSword.cpp
#include "DragonSword.h"
DragonSword::DragonSword(){
this->m_WeaponName = "屠龙刀";
this->m_BaseDamage = 45;
this->suckRate = 15;
this->holdRate = 20;
this->critRate = 15;
}
/*@ 基础伤害 */
int DragonSword::getBaseDamage(){
return this->m_BaseDamage;
}
/*@ 吸血 */
int DragonSword::getSuckBlood(){
if (isTrigger(suckRate)){
return this->suckRate * 0.4;
}
return 0;
}
/*@ 是否定身 */
bool DragonSword::getHold(){
if (isTrigger(holdRate)){
return true;
}
return false;
}
/*@ 是否暴击 */
bool DragonSword::getCrit(){
if (isTrigger(critRate)){
return true;
}
return false;
}
/* @是否触发:通过获取100个随机,取固定区间作为随机值概率 */
bool DragonSword::isTrigger(int rate){
// 生成随机数
int num = rand() % 100 + 1;
if (num < rate){
return true;
}
return false;
}
2.2 Knife.cpp
#include "Knife.h"
Knife::Knife(){
this->m_WeaponName = "小刀";
this->m_BaseDamage = 15;
}
/*@ 基础伤害:15 */
int Knife::getBaseDamage(){
return this->m_BaseDamage;
}
/*@ 吸血:无 */
int Knife::getSuckBlood(){
return 0;
}
/*@ 是否定身:无 */
bool Knife::getHold(){
return false;
}
/*@ 是否暴击:无 */
bool Knife::getCrit(){
return false;
}
2.3 Hero.cpp
#include "Hero.h"
#include "Monster.h"
Hero::Hero(){
this->H_HeroName = "蓝猫";
this->H_Atk = 20;
this->H_Def = 3;
this->H_Hp = 900;
this->H_Mp = 450;
weapon = NULL;
}
/*@ 使用武器 */
void Hero::EquipWeapon(Weapon *weapon){
this->weapon = weapon;
cout << this->H_HeroName << "装备了:" << this->weapon->m_WeaponName << endl;
}
/*@ 攻击:调用一次,发起一次攻击 */
void Hero::Attack(Monster *monster){
int damage = this->H_Atk; // 攻击力
int addHp = 0; // 吸血
bool isHold = false; // 眩晕
bool isCrit = false; // 暴击
// 伤害
damage = this->H_Atk + this->weapon->getBaseDamage();
// 吸血
addHp = this->weapon->getSuckBlood();
// 眩晕
isHold = this->weapon->getHold();
// 暴击
isCrit = this->weapon->getCrit();
// 暴击加成
if (isCrit){
damage += damage;
cout << this->weapon->m_WeaponName << "触发暴击效果,怪物受到暴击伤害:" << damage << endl;
}
// 眩晕
if (isHold){
cout << this->weapon->m_WeaponName << "触发眩晕效果,怪物停止攻击..."<< endl;
}
// 吸血
if (addHp > 0){
this->H_Hp += addHp;
cout << this->weapon->m_WeaponName << "触发吸血效果," << this->H_HeroName << "回复血量:" << addHp << endl;
}
// 是否被眩晕
monster->m_Hold = isHold;
// 真实伤害
int trueDamege = (damage - monster->m_Def) ? damage - monster->m_Def : 1;
monster->m_HP -= trueDamege;
cout << this->H_HeroName << "攻击了" << monster->m_MonsterName << ":" << trueDamege << endl;
}
2.4 Monster.cpp
#include "Monster.h"
Monster::Monster(){
this->m_Atk = 10;
this->m_Def = 1;
this->m_Hold = false;
this->m_HP = 300;
this->m_Mp = 150;
this->m_MonsterName = "哥斯拉";
}
void Monster::Attack(Hero *hero){
if (this->m_Hold){
cout << this->m_MonsterName << "被眩晕,停止攻击" << endl;
return;
}
int damage = (this->m_Atk - hero->H_Def) > 0 ? (this->m_Atk - hero->H_Def) : 1;
hero->H_Hp -= damage;
cout << this->m_MonsterName << "攻击了" << hero->H_HeroName<< ":"<< damage << endl;
}
2.5 main.cpp
#include<iostream>
#include<string>
#include"DragonSword.h"
#include"Hero.h"
#include"Knife.h"
#include"Monster.h"
#include"Weapon.h"
using namespace std;
void test01(){
Monster *monster = new Monster;
Hero *hero = new Hero;
Weapon *knife = new Knife;
Weapon *dragonsword = new DragonSword;
hero->EquipWeapon(dragonsword);
for (int i = 0; i < 5; ++i){
cout << "-----------第" << i+1 << "回合---------" << endl;
hero->Attack(monster);
monster->Attack(hero);
cout << hero->H_HeroName << "剩余血量为:" << hero->H_Hp << endl;
cout << monster->m_MonsterName << "剩余血量为:" << monster->m_HP << endl;
}
}
int main(){
test01();
system("pause");
return 0;
}