C++ | 【04】游戏案例

入门小伙伴可先熟悉该改代码,掌握逻辑后在自行敲打。😁

首先创建武器类,武器类衍生了屠龙刀类以及小刀类,在创建英雄和怪兽即可。

一、头文件
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;
}

三、运行结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jxiepc

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值