【C++】pk游戏—(玩转多态)

pk游戏—(玩转多态)

本案例为多态应用;

武器类属性:基础伤害、特效(吸血、定身、暴击);

屠龙刀拥有:吸血、定身、暴击的几率

小刀:无任何能力

英雄:攻击力、防御、血量、武器、装备武器、攻击怪物

怪物:攻击力、防御、血量、攻击英雄

核心算法:通过isTrigger判断是否触发特效
//系统随机产生 1~100的数字
//(例如传进来一个30,然后产生一个随机数,如果这个随机数在1-30之间,则触发特效)

代码实现:

1.Dragonsword.h

#pragma once 
#include <iostream>
#include "Weapon.h"
#include <string>
using namespace std;

class DragonSword :public Weapon
{
public:
	DragonSword();

	//获取基础伤害
	virtual int getBaseDamage();

	//获取吸血
	virtual int getSuckBlood();

	//获取是否定身
	virtual bool getHold();

	//获取是否暴击
	virtual bool getCrit();

	//  吸血率  定身率 暴力率 
	int suckRate;
	int holdRate;
	int critRate;

	//传入概率 判断是否触发
	bool isTrigger(int rate);


};

2.Hero.h

#pragma once 
#include <iostream>
#include "Weapon.h"
#include <string>
#include "Monster.h"
using namespace std;

class Monster;

class Hero
{
public:
	Hero();

	string m_Name; //人名

	int m_Atk; //攻击力

	int m_Def; // 防御力

	int m_Hp; //血量

	Weapon * weapon; //武器

	void EquipWeapon(Weapon * weapon);//装备武器

	void Attack(Monster * monster);//攻击怪物
};

3.Knife.h

#pragma once 
#include <iostream>
#include "Weapon.h"
#include <string>
using namespace std;


class Knife :public Weapon
{
public:
	Knife();

	//获取基础伤害
	virtual int getBaseDamage();

	//获取吸血
	virtual int getSuckBlood();

	//获取是否定身
	virtual bool getHold();

	//获取是否暴击
	virtual bool getCrit();

};



4.Monster.h

#pragma once 
#include <iostream>
#include "Weapon.h"
#include <string>
#include "Hero.h"
using namespace std;

class Hero;

class Monster
{
public:
	Monster();

	string m_Name;

	int m_Hp;

	int m_Atk;

	int m_Def;

	bool m_Hold;

	void Attack(Hero * hero);

};

5.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.DragonSword.c

#include "DragonSword.h"

DragonSword::DragonSword()
{
	this->m_BaseDamage = 20;
	this->m_WeaponName = "屠龙刀";
	this->suckRate = 20;
	this->holdRate = 30;
	this->critRate = 35;

}

//基础伤害
int DragonSword::getBaseDamage()
{
	return this->m_BaseDamage;
}

//吸血
int DragonSword::getSuckBlood()
{
	if (isTrigger(suckRate))
	{
		return this->m_BaseDamage * 0.5;  //按照武器基础伤害一半吸血
	}
	return 0;
}

//定身
bool DragonSword::getHold()
{
	if (isTrigger(holdRate))
	{
		return true;
	}
	return false;
}

//暴击
bool DragonSword::getCrit()
{
	if (isTrigger(critRate))
	{
		return true;
	}
	return false;
}

//通过isTrigger判断是否触发特效
//随机 1~100的数字
//(例如传进来一个30,然后产生一个随机数,如果这个随机数在1-30之间,则触发特效)
bool DragonSword::isTrigger(int rate)
{


int num = rand() % 100 + 1;//产生随机数1-100
if (num < rate)
{
	return true;
}
return false;


}


2.Hero.c

#include "Hero.h"

Hero::Hero()
{
	this->m_Hp = 500;//血量

	this->m_Atk = 50;//攻击力

	this->m_Def = 50;//防御

	this->m_Name = "法师";

	this->weapon = NULL;

}

//装备武器
void Hero::EquipWeapon(Weapon * weapon)
{
	this->weapon = weapon;

	cout << "英雄:" << this->m_Name << " 装备了武器 << " << this->weapon->m_WeaponName << " >> " << endl;

}
//攻击
void Hero::Attack(Monster * monster)
{
	int damage = 0;//伤害
	int addHp = 0;//加血
	bool isHold = false;//定身
	bool isCrit = false;//暴击

	if (this->weapon == NULL) //武器为空 没有加成
	{
		damage = this->m_Atk;
	}
	else
	{
		//基础伤害
		damage = this->m_Atk + this->weapon->getBaseDamage();
		//计算吸血
		addHp = this->weapon->getSuckBlood();
		//计算定身
		isHold = this->weapon->getHold();
		//计算暴击
		isCrit = this->weapon->getCrit();
	}
	if (isCrit) //暴击 伤害 加成
	{
		damage = damage * 2;
		cout << "英雄的武器触发了暴击效果,怪物收到了双倍的伤害,伤害值:" << damage << endl;
	}
	if (isHold)
	{
		cout << "英雄的武器触发了定身效果,怪物停止攻击一回合" << endl;
	}
	if (addHp > 0)
	{
		cout << "英雄的武器触发了吸血效果,英雄增加的血量为" << addHp << endl;
	}

	//设置怪物定身
	monster->m_Hold = isHold;

	//计算真实伤害
	int trueDamage = (damage - monster->m_Def) > 0 ? damage - monster->m_Def : 1;

	monster->m_Hp -= trueDamage;//怪物掉血

	this->m_Hp += addHp;//加血

	cout << "英雄 " << this->m_Name << " 攻击了敌人  " << monster->m_Name << " 造成了 伤害" << trueDamage << endl;
}

3.Knife.c

#include "Knife.h"


Knife::Knife()
{
	this->m_BaseDamage = 10;

	this->m_WeaponName = "小刀";
}

int Knife::getBaseDamage()
{

	return this->m_BaseDamage;

}

int Knife::getSuckBlood()
{
	return 0;
}

bool Knife::getHold()
{
	return false;
}

bool Knife::getCrit()
{
	return false;
}

4.Monster.c

#include "Monster.h"


Monster::Monster()
{
	this->m_Hp = 300;

	this->m_Atk = 70;

	this->m_Def = 40;

	this->m_Hold = false;

	this->m_Name = "哥布林";
}

void Monster::Attack(Hero * hero)
{
	if (this->m_Hold)
	{
		cout << "怪物 " << this->m_Name << " 被定身了,本回合无法攻击" << endl;
		return;
	}

	//计算攻击伤害
	int damage = (this->m_Atk - hero->m_Def) > 0 ? this->m_Atk - hero->m_Def : 1;

	hero->m_Hp -= damage;

	cout << "怪物 " << this->m_Name << "攻击了英雄 " << hero->m_Name << " 造成了 伤害" << damage << endl;

}

5.主函数调用部分

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include "Hero.h"
#include "Monster.h"
#include "Weapon.h"
#include "Knife.h"
#include "DragonSword.h"
#include <ctime>
using namespace std;

void play()
{
	//创建怪物
	Monster * monster = new Monster;
	//创建英雄
	Hero * hero = new Hero;

	//创建武器
	Weapon * kinfe = new Knife;
	Weapon * dragon = new DragonSword;

	//让用户选择 武器

	cout << "请选择武器:" << endl;
	cout << "1. 赤手空拳" << endl;
	cout << "2. 小刀" << endl;
	cout << "3. 屠龙刀" << endl;

	int oper;//选择

	cin >> oper;
	switch (oper)
	{
	case 1:
		cout << "你真牛X,你还是太年轻了!" << endl;
		break;
	case 2:
		hero->EquipWeapon(kinfe);
		break;
	case 3:
		hero->EquipWeapon(dragon);
		break;
	}

	getchar(); //输入缓冲区里有个回车 ,多获取一次值

	int round = 1;//回合

	while (true)
	{
		getchar();

		system("cls");//清屏

		cout << "----- 当前第 " << round << " 回合开始 ------" << endl;

		if (hero->m_Hp <= 0)
		{
			cout << "英雄  " << hero->m_Name << "已挂 ,游戏结束" << endl;
			break;
		}
		hero->Attack(monster);

		if (monster->m_Hp <= 0)
		{
			cout << "怪物  " << monster->m_Name << "已挂,顺利通关" << endl;
			break;
		}
		monster->Attack(hero);


		if (hero->m_Hp <= 0)
		{
			cout << "英雄  " << hero->m_Name << "已挂 ,游戏结束" << endl;
			break;
		}

		cout << "英雄  " << hero->m_Name << "剩余血量:" << hero->m_Hp << endl;
		cout << "怪物  " << monster->m_Name << "剩余血量:" << monster->m_Hp << endl;

		round++;


	}

	delete monster;
	delete hero;
	delete kinfe;
	delete dragon;

}

int main() {

	srand((unsigned int)time(NULL));

	play();


	system("pause");
	return 0;
}
  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值