C++ PK小游戏

随手写的一个C++案例

在这里插入图片描述

main.cpp

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

void play() {
	Monster *monster = new Monster;
	Hero * hero = new Hero;
	Weapon* knife = new Knife;
	Weapon* dragonSword = new DragonSword;

	//用户选择武器
	cout << "请选择你的武器" << endl;
	cout << "1、赤手空拳" << endl;
	cout << "2、长剑" << endl;
	cout << "3、屠龙宝刀" << endl;
	int tmp_ChooseWeapon;
	cin >> tmp_ChooseWeapon;
	//ChooseWeapon();
	switch (tmp_ChooseWeapon)
	{
	case 1:
		cout << "你没有选择任何武器" << endl;
		break;
	case 2:
		cout << "你选择了普通长剑" << endl;
		hero->EquipWeapon(knife);
		break;
	case 3:
		cout << "你选择了屠龙宝刀" << endl;
		hero->EquipWeapon(dragonSword);
		break;
	}
	getchar();
	int Tmp_round = 1;
	cout << "游戏回合正式开始!\n";

	while (true)
	{
		getchar();
		system("cls");
		cout << "-----------这是当前第:"<< Tmp_round <<"回合!-------"<< endl;

		//英雄攻击
		hero->Attack(monster);
		//判断英雄是否阵亡
		if (monster->get_m_Hp() <= 0) {
			cout << "怪物‘" << monster->get_m_Name() << "’已经被击败,你赢了!" << endl;
			break;
		}
		//怪物攻击
		monster->Attack(hero);
		//判断怪物是否击败
		if (hero->m_Hp <= 0) {
			cout << "英雄‘" << hero->m_Name << "’已经阵亡,你输了!\n游戏结束!\n" << endl;
			break;
		}
		cout << "英雄‘" << hero->m_Name << "’剩余的血量值:"<< hero->m_Hp << endl;
		cout << "怪物‘" << hero->m_Name << "’剩余的血量值:" << monster->get_m_Hp() << endl;
		Tmp_round++;
	}
	delete monster;
	delete hero;
	delete knife;
	delete dragonSword;
}


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

	play();
	system("pause");
	return EXIT_SUCCESS;
}

Weapon.h

#pragma once
#ifndef WEAPON_H
#define WEAPON_H
#include<iostream>
#include<string>

class Weapon{
public:
	virtual int get_m_baseDamage() = 0;//基础伤害
	virtual int get_SuckBlood() = 0;//吸血
	virtual bool get_Hold() = 0;//禁锢
	virtual bool get_Crit() = 0;//暴击

private:

public:
	std::string m_WeaponName;
	int m_BaseDamage;

};

#endif // !WEAPON_H

Knife.h

#pragma once
#ifndef KNIFE_H
#define KNIFE_H
#include<iostream>
#include<string>
#include"Weapon.h"

class Knife : public Weapon {
public:
	Knife();
	virtual int get_m_baseDamage();//基础伤害
	virtual int get_SuckBlood();//吸血
	virtual bool get_Hold();//禁锢
	virtual bool get_Crit();//暴击

private:

};


#endif

Knife.cpp

#include"Knife.h"

Knife::Knife() {
	this->m_WeaponName = "长剑";
	this->m_BaseDamage = 10;
}

int Knife::get_m_baseDamage() {
	return this->m_BaseDamage;
}

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

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

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

DragonSword .h

#pragma once
#ifndef DRAGONSWORD_H
#define DRAGONSWORD_H
#include<iostream>
#include<string>
#include"Weapon.h"

class DragonSword : public Weapon {
public:
	DragonSword();
	virtual int get_m_baseDamage();//基础伤害
	virtual int get_SuckBlood();//吸血
	virtual bool get_Hold();//禁锢
	virtual bool get_Crit();//暴击
	bool IsTrigger(int rate);//判断吸血禁锢暴击的概率是否触发
	//int get_SuckRate() { return SuckRate; }
	//int get_HoldRate() { return HoldRate; }
	//int get_CritRate() { return CritRate; }
private:
	//吸血禁锢暴击的概率
	int SuckRate = 10;
	int HoldRate = 35;
	int CritRate = 50;
};


#endif

DragonSword .cpp

#include"DragonSword.h"
#include<time.h>

DragonSword::DragonSword()
{
	this->m_WeaponName = "屠龙宝刀";
	this->m_BaseDamage = 35;
}

int DragonSword::get_m_baseDamage()
{
	return this->m_BaseDamage;
}

int DragonSword::get_SuckBlood()
{
	return (this->m_BaseDamage * SuckRate)/100;
}

bool DragonSword::get_Hold()
{
	if (IsTrigger(HoldRate)) {
		return true;
	}
	return false;
}

bool DragonSword::get_Crit()
{
	if (IsTrigger(CritRate)) {
		return true;
	}
	return false;
}

bool DragonSword::IsTrigger(int rate)
{
	//判断是否触发特效
	int num = rand() % 100 + 1;
	//std::cout <<"---------随机数:" << num;
	if (num<=rate) {
		return true;
	}
	return false;
}

Hero.h

#pragma once
#ifndef HERO_H
#define HERO_H
#include<iostream>
#include<string>
#include"Weapon.h"
#include"Monster.h"

class Monster ;
class Hero {
public:
	Hero();
	void EquipWeapon(Weapon *weapon);
	void Attack(Monster *monster);
private:

public:
	std::string m_Name;
	int m_Atk;
	int m_Def;
	int m_Hp;
	Weapon *weapon;
};



#endif

Hero.cpp

#include"Hero.h"

Hero::Hero()
{
	this->m_Hp = 450;
	this->m_Atk = 70;
	this->m_Def = 40;
	this->m_Name = "剑士";
	this->weapon = NULL;
}

//装备武器
void Hero::EquipWeapon(Weapon * weapon)
{
	this->weapon = weapon;
	std::cout << "玩家:" << this->m_Name << "装备了武器《" << this->weapon->m_WeaponName << "》\n";
}

//攻击
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->get_m_baseDamage();
		//计算吸血
		addHP = this->weapon->get_SuckBlood();
		std::cout << "----------------------" << addHP << std::endl;
		//计算是否禁锢
		isHold = this->weapon->get_Hold();
		//计算是否暴击
		isCrit = this->weapon->get_Crit();
	}
	if (isCrit) {
		damage = damage * 2;
		addHP += addHP;
		std::cout << "英雄的武器:‘" << this->weapon->m_WeaponName << "’触发了暴击效果,暴击值为" << damage << "。\n"
			<< "    同时英雄的吸血效果加倍,吸血值:" << addHP << "。\n";
	}
	if (isHold) {
		std::cout << "英雄的武器:‘" << this->weapon->m_WeaponName << "’触发了禁锢效果,迫使敌方停止攻击一回合。\n";
	}
	//
	monster->m_Hold = isHold;
	int Tmp_Damage = damage - monster->get_m_Def();//缓冲值
	int True_Damage = Tmp_Damage > 0 ? Tmp_Damage : 1;//实际伤害
	this->m_Hp += addHP;
	if (Tmp_Damage != 1) {
		std::cout << "英雄‘" << this->m_Name << "’攻击了敌人‘" 
			<< monster->get_m_Name() << "’并造成了" 
			<< True_Damage << "的伤害值\n";
		int Tmp_monster_Hp = monster->get_m_Hp() - Tmp_Damage;
		monster->set_m_Hp(Tmp_monster_Hp);
	}
	else {
		std::cout << "英雄‘" << this->m_Name << "’攻击了敌人‘"
			<< monster->get_m_Name() << "’并没有造成伤害\n";
		int Tmp_monster_Hp = monster->get_m_Hp() - Tmp_Damage;
		monster->set_m_Hp(Tmp_monster_Hp);
	}
}

Monster.h

#pragma once
#ifndef MONSTER_H
#define MONSTER_H
#include<iostream>
#include<string>
#include"Hero.h"
#include"Weapon.h"

class Hero;
class Monster {
public:
	Monster();
	void Attack(Hero * hero);
	int get_m_Def() { return this->m_Def; }
	std::string get_m_Name() { return this->m_Name; }
	int get_m_Hp() { return this->m_Hp; }
	void set_m_Hp(int a) { this->m_Hp = a; }
private:
	std::string m_Name;
	int m_Atk;
	int m_Def;
	int m_Hp;
public:
	bool m_Hold;
};

#endif

Monster.cpp

#include "Monster.h"

Monster::Monster()
{
	this->m_Hp = 300;
	this->m_Atk = 60;
	this->m_Def = 60;
	this->m_Hold = false;
	this->m_Name = "黄金哥布林";
}

void Monster::Attack(Hero * hero)
{
	if (this->m_Hold) {
		std::cout << "怪物‘" << this->m_Name << "’被禁锢了,本回合无法造成伤害\n";
		return;
	}
	else {
	//计算伤害
		int damage = this->m_Atk - hero->m_Def;
		if (damage!=1) {
			std::cout << "怪物‘" << this->m_Name << "’对英雄‘"
				<< hero->m_Name << "’造成了"<< damage <<"的伤害\n";
		}
		else {
			std::cout << "怪物‘" << this->m_Name << "’没有对英雄‘"
				<< hero->m_Name <<"’造成伤害\n";
		}
		hero->m_Hp -= damage;
	}
}

发布了106 篇原创文章 · 获赞 269 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_32642107/article/details/104946919