回合制PK小游戏

回合制PK小游戏

需要英雄,怪物,武器

一、武器类

1、abstractweapon.h

将武器类抽象化作为基类为派生类服务,这样要使用武器的话就只需要提供一个接口了

#pragma once//抽象武器类
#include<string>
using std::string;
class abstractweapon
{
public:
	string weaponname;//名字
	int basedamage;//伤害值
    //武器的三个功能:基础伤害,暴击,吸血
	virtual int getbasedemage()=0;//获取基础伤害值
	virtual int getsucklood()=0;//吸血
	virtual bool getcrit() = 0;//是否暴击

};

abstractweapon.cpp

#include "abstractweapon.h"

2、sword.h

创建一个剑类

#pragma once
#include"abstractweapon.h"//要包含抽象武器类的头文件
class sword:public abstractweapon//多态---要继承才行
{
public:
	virtual int getbasedemage();//获取基础伤害值
	virtual int getsucklood();//吸血
	virtual bool getcrit();//是否暴击
	sword();
};

sword.cpp

#include "sword.h"

int sword::getbasedemage()
{
	return this->basedamage;//接收基础的伤害值
}

int sword::getsucklood()
{
	return 0;//无法吸血
}

bool sword::getcrit()
{
	return false;//无法暴击
}

sword::sword()//构造函数---初始化剑类
{
	this->weaponname = "大宝剑";//武器名字
	this->basedamage = 10;//武器的基础伤害值
}

3、knife.h

创建一个刀类

#pragma once
#include"abstractweapon.h"
class knife:public abstractweapon
{
public:
	virtual int getbasedemage();//获取基础伤害值
	virtual int getsucklood();//吸血
	virtual bool getcrit();//是否暴击
	
    //这把刀可以吸血,也可以暴击
	int suckrate;//吸血率
	int critrate;//暴击率
	//用bool类型判断是否产生暴击
	bool istrigger(int rate);
	knife();//初始化这个类
};


knife.cpp

#include "knife.h"

int knife::getbasedemage()
{
	return this->basedamage;//获取这把刀的基础伤害
}

int knife::getsucklood()
{
	if (istrigger(suckrate))
	{
		return basedamage*2;//如果吸血,吸多少血量
	}
	return false;
}

bool knife::getcrit()
{
	if (istrigger(critrate))
	{
		return true;
	}
	return false;
}

bool knife::istrigger(int rate)
{
	int num = rand() % 100;//随机num(0~99)
	if (num < rate)
	{
		return true;
	}
	return false;
}

knife::knife()
{
	this->basedamage = 20;//基础伤害值
	this->critrate = 20;//暴击率
	this->suckrate = 20;//吸血率
	this->weaponname = "刀";//武器的名字
}

二、怪物类

1、monster.h

#pragma once
#include<string>
using std::string;
class monster
{
public:
	int atk;//攻击力
	int def;//防御力
	int hp;//血量
	string name;//怪物的名字
	monster();//初始化怪物属性
};

monster.cpp

#include "monster.h"

monster::monster()
{
	this->atk = 110;
	this->def = 60;
	this->hp = 1000;
	this->name = "杰顿";
}

三、英雄类

1、hero.h

#pragma once
#include"abstractweapon.h"//英雄要使用武器,所以要包含头文件
#include<string>
using std::string;
class hero
{
public:
	int atk;//攻击
	int def;//防御值
	int hp;//血量
	string name;//名字

	abstractweapon* weapon;//武器,包含武器类
	//装备武器,看使用什么武器
	void equipweapon(abstractweapon* weapon);
	hero();

};


hero.cpp

#include "hero.h"

void hero::equipweapon(abstractweapon * weapon)
{
	this->weapon = weapon;//第二个weapon就是你要传进来的武器指针地址
	this->atk += weapon->getbasedemage();//这个就是计算,你装备了武器之后,你的攻击力提高到了多少
}

hero::hero()//初始化英雄类
{
	this->atk = 100;
	this->def = 50;
	this->hp = 900;
	this->weapon = NULL;//最开始是没有装备武器的
	this->name = "杰克";
}

四、管理类

这个是最重要的,创建一个新的类来管理英雄,武器,怪物

1.manage.h

#pragma once
#include"hero.h"
#include"knife.h"
#include"monster.h"
#include"sword.h"
#include<iostream>
#include<time.h>//因为前面吸血率,暴击率使用了随机数
using std::cout;
using std::cin;
using std::endl;
#define DELE_BOJ(p) {if(p!=NULL){delete p;p=NULL;}}
class manage
{
	hero* Hero;//创建英雄类指针
	monster* Monster;//创建怪物类指针
public:
	manage();
	~manage();//因为new了对象,要释放内存,加一个析构函数
	void initgame();//初始化游戏
	void updatagame();//刷新游戏
private://加在私有属性下面,因为这些功能不需要让使用者知道
	void select();//选择对武器的使用
	void heroattack();//英雄攻击
	void monsterattack();//怪物攻击
};


manage.cpp

#include "manage.h"

manage::manage()
{
	Hero = NULL;
	Monster = NULL;//最开始,让两个指针为空
}

manage::~manage()
{
	DELE_BOJ(Hero);
	DELE_BOJ(Monster);//释放内存

}

void manage::initgame()
{
    //new出两个对象,英雄和怪物
	this->Hero = new hero;
	this -> Monster = new monster;
    //创建随机数种子
	srand((unsigned)time(NULL));

}

void manage::updatagame()//刷新游戏
{
	select();//选择使用武器否
	getchar();//回车实现作用
	while (1)
	{
		heroattack();//英雄攻击
		if (this->Monster->hp <= 0)
		{
			printf("英雄%s剩余血量:%d\n", this->Hero->name.c_str(), this->Hero->hp);
			printf("怪物%s剩余血量:%d\n", this->Monster->name.c_str(), this->Monster->hp);
			cout << "英雄打死了怪物" << endl;
			break;
		}
		printf("英雄%s剩余血量:%d\n", this->Hero->name.c_str(), this->Hero->hp);
		printf("怪物%s剩余血量:%d\n", this->Monster->name.c_str(), this->Monster->hp);
		monsterattack();
		if (this->Monster->hp <= 0)
		{
			printf("英雄%s剩余血量:%d\n", this->Hero->name.c_str(), this->Hero->hp);
			printf("怪物%s剩余血量:%d\n", this->Monster->name.c_str(), this->Monster->hp);
			cout << "怪物打死了英雄" << endl;
			break;
		}
		printf("英雄%s剩余血量:%d\n", this->Hero->name.c_str(), this->Hero->hp);
		printf("怪物%s剩余血量:%d\n", this->Monster->name.c_str(), this->Monster->hp);
		getchar();
		system("pause");

	}
	getchar();

}

void manage::select()
{
	int num = 0;
	cout << "1不要武器\t2装备剑\t3装备刀" << endl;
	cin >> num;
	switch (num)
	{
	case 1:
		this->Hero->weapon = NULL;//选1是没有武器的
		cout << "你没有装备武器就上了" << endl;
		break;
	case 2:
		this->Hero->equipweapon(new sword);//装备了剑
		cout << "你装备武器[剑]" << endl;
		break;
	case 3:
		this->Hero->equipweapon(new knife);//装备了刀
		cout << "你装备武器[刀]" << endl;

	default:
		break;
	}
}

void manage::heroattack()//英雄攻击怪物
{
    //先初始化
	int damage = 0;//伤害
	int addhp = 0;//吸血量
	bool iscrit = false;//是否暴击
	if (this->Hero->weapon != NULL)//如果装备了武器
	{
        //看是否暴击
		iscrit = this->Hero->weapon->getcrit();
        //看能吸多少血
		addhp = this->Hero->weapon->getsucklood();//吸血量
	}
	damage = this->Hero->atk;//英雄加上武器的伤害
	if (iscrit)//如果暴击,造成的暴击效果
	{
		damage *= 2;
		cout << "触发暴击,伤害翻倍" << endl;
	}
	if (addhp > 0)//吸血量大于0,造成的效果
	{
		this->Hero->hp += addhp;
		cout << "吸取血量" << addhp << endl;
	}
	int truedamage = 0;//真实伤害,因为怪物还有防御值
	truedamage = (damage - (this->Monster->def) )> 0 ?( damage - (this->Monster->def)) : 1;
    //更新怪物的血量
	this->Monster->hp -= truedamage;
	printf("英雄%s:打了一下怪物:%s,怪物掉了%d血量\n", this->Hero->name.c_str(), this->Monster->name.c_str(), truedamage);
}

void manage::monsterattack()//怪物攻击英雄
{
	int truedamage = ((this->Monster->atk - this->Hero->def) > 0) ? (this->Monster->atk - this->Hero->def) : 1;
	this->Hero->hp -= truedamage;
	printf("怪物%s:打了一下英雄:%s,英雄掉了%d血量\n", this->Monster->name.c_str(), this->Hero->name.c_str(), truedamage);
}

posted @ 2021-02-28 14:32  kisfly  阅读(130)  评论(0编辑  收藏  举报