C ++ simple games PK

Requirements Analysis:
hero class: name, base attack power, defense power, blood, weapons use
weapons class: name, attack power bonus, whether vampire, whether crit
monster class: name, attack power, defense power, would have been given body
design focus:
(1) package weapon class is an abstract class, so the underlying parent. Favorable and extended operation
arms extended (inherited) are: a knife, Dragon sword
(2) heroes and monsters, each class has its own functions to Attack Attack
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_Name;//武器名称
	int m_BaseDamage;//基础伤害
};

knife.h

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

class Knife:public Weapon
{
public:
	Knife();
	//获取基础伤害
	virtual int getBaseDamage();
	//获取吸血
	virtual int getSuckBlood();
	//是否暴击
	virtual bool getHold();
	//是否定身
	virtual bool getCrit();

};
};

DragonSword.h

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


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;
};

hero.h

#pragma once 
#include<iostream>
#include<string>
#include"Weapon.h"
#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 * m_weapon;
	
	void EquipWeapon(Weapon * weapon);
	void Attack(Monster * mon);
};

Monster.h

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

class Hero;
class Monster
{
public:
	Monster();
    void Monster::Attack(Hero * hero);
	string m_name;
	int m_Hp;
	int m_Atk;
	int m_Def;
	bool m_Hold;
	
};

hero.cpp

#include"hero.h"

Hero::Hero()
{
	this->m_Hp=500;
	this->m_Atk=50;
	this->m_Def=50;
	this->m_name="法师";
	this->m_weapon =NULL;
}
void Hero::EquipWeapon(Weapon * weapon)
{
	this->m_weapon=weapon;
	cout<<"英雄:"<<this->m_name<<"装备了"<<weapon->m_Name<<endl;
}
void Hero::Attack(Monster * mon)
{
	int damage=0;
	int addHp=0;

	bool isCrit=false;
	bool isHold=false;

	
	if(this->m_weapon == NULL)//没有武器的情况下
	{
		damage = this->m_Atk;
	}
	else
	{
		//计算伤害
		damage = this->m_Atk +  this->m_weapon->getBaseDamage();
		//计算吸血
		addHp = this->m_weapon->getSuckBlood();
		//计算定身
		isHold = this->m_weapon->getHold();
		//是否暴击
		isCrit= this->m_weapon->getCrit();
	}
	if(isCrit)
	{
		damage = damage *2;
		cout<<"英雄的武器出发了暴击效果,双倍伤害值:"<<damage<<endl;
	}
	if(isHold)
	{
		cout<<"玩家出发了定身效果,怪物停止攻击一回合"<<endl;
	}
	if(addHp>0)
	{
		cout<<"英雄的武器触发了吸血效果,英雄增加的吸血量为:"<<addHp<<endl;
	}
	//掉怪物定身
	mon->m_Hold=isHold;
	//计算真是伤害
	int trueDamage= (damage- mon->m_Def)>0?damage-mon->m_Def:1;

	mon->m_Hp -=trueDamage;

	this->m_Hp +=addHp;
	cout<<"英雄"<<this->m_name<<"攻击了敌人"<<mon->m_name<<"造成了真实伤害:"<<trueDamage<<endl;
}

Monster.cpp

#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;
}

main()

#include<iostream>
using namespace std;
#include"DragonSword.h"
#include"hero.h"
#include"knife.h"
#include"Monster.h"
#include<ctime>

void play()
{
	//定义新英雄
	Hero *hero = new Hero;
	//定义怪物
	Monster * mon=new Monster;
	//定义武器
	Knife * kni= new Knife;
	DragonSword * drag=new DragonSword;


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

	int opear;
	cin>>opear;
	switch(opear)
	{
		case 1:cout<<"你还是太年轻了!"<<endl;break;
		case 2:hero->EquipWeapon(kni);break;
		case 3:hero->EquipWeapon(drag);break;
	}

	getchar();

	int round=1;
	while(1)
	{
		getchar();
		system("cls");

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

		
		hero->Attack(mon);
		if(mon->m_Hp<0)
		{
			cout<<"怪物:"<<mon->m_name<<"挂了,顺利通关"<<endl;
			break;
		}


		mon->Attack(hero);
		if(hero->m_Hp<=0)
		{
			cout<<"英雄:"<<hero->m_name<<"挂了,游戏结束"<<endl;
			break;
		}
		
		cout<<"英雄剩余血量:"<<hero->m_Hp<<endl;
		cout<<"怪物剩余血量"<<mon->m_Hp<<endl;

		round++;
	}	

	delete hero;
	delete mon;
	delete kni;
	delete drag;
}


int main()
{
	srand(time(NULL));
	play();
	return 0;
}
Published 38 original articles · won praise 13 · views 4326

Guess you like

Origin blog.csdn.net/YanWenCheng_/article/details/103974113