设计并编写代码自动格斗类游戏

题目描述:
1)角色类CRole为基类:
构造函数、析构函数;
成员变量:头像、HP(血量)、ATK(攻击力)、DEF(防御力)、Lv(等级),EXP(经验值);
成员函数:武器攻击、跳跃。
2)英雄类CHero继承于CRole类
构造函数、析构函数;
英雄类新增技能踢腿(成员函数)、抱摔(成员函数),给对方造成伤害具体值由学员自己进行设定;
3)敌人类CEnemy继承于CRole类
构造函数、析构函数;
新增技能劈掌(成员函数)、连环腿(成员函数),给对方造成伤害值由学员自己进行设定;
4)战斗类CBattle
构造函数、析构函数及其他成员函数
双方HP(血量)初始值为100,开始战斗以后,英雄和敌人轮流攻击对方,掉血量=攻击方技能伤害值+ATK(攻击方的攻击力)-DEF(防御方的防御力),当一方血量为0时,战斗结束,玩家经验值增加相应点数,当经验等级达一定时候,玩家等级提升。

“game.h”

#pragma once
#include<iostream>
#include<string>

//角色类
class CRole{
	public:
		std::string sculpture;			//头像
		int HP;							//血量
		int ATK;						//攻击力
		int DEF;						//防御力
		int Lv;							//等级
		int EXP;						//经验
	public:
		CRole() {};						//默认构造函数
		CRole(const std::string sculpture,int HP,int ATK,int DEF,int Lv,int EXP);
		~CRole();
		void WeaponAttack();			//武器攻击
		void Jump();					//跳跃
		void show();
	public:
		int wa_ATK;
};

//英雄类
class CHero :public CRole {
	public:
		CHero() {};						//默认构造函数
		CHero(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP);
		~CHero();						//析构函数
		void Kicking();					//踢腿
		void Wresting();				//抱摔
	public:
		int k_ATK;						//踢腿攻击力
		int w_ATK;						//抱摔攻击力
};

//敌人类
class CEnemy:public CRole {
	public:
		CEnemy() {};					//默认构造函数
		CEnemy(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP);
		~CEnemy();						//析构函数
		void Chopping_Palm();			//劈掌
		void Serial_Leg();				//连环腿
	public:
		int p_ATK;						//劈掌攻击力
		int s_ATK;						//连环腿攻击力
};

//战斗类
class CBattle :public CRole {
	public:
		CBattle();						//构造函数
		~CBattle();						//析构函数
		void ready(CHero &obj1, CEnemy &obj2);//战斗准备函数
		void fighting(CHero obj1, CEnemy obj2);//战斗函数
};

"game.cpp"

#include"pch.h"
#include"game.h"
#include<string>
#include<Windows.h>
#include<time.h>

//角色类——构造函数
CRole::CRole(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP) {
	this->sculpture = sculpture;
	this->HP = HP;
	this->ATK = ATK;
	this->DEF = DEF;
	this->Lv = Lv;
	this->EXP = EXP;
}
void CRole::show() {
	std::cout << "角色:" << this->sculpture << " 血量:" << this->HP << " 攻击力:"
		<< this->ATK << " 防御力:" << this->DEF << " 等级:" << this->Lv << " 经验:" << this->EXP << std::endl;
}
//角色类——析构函数
CRole::~CRole(){
	
}

//角色类——武器攻击
void CRole::WeaponAttack() {
	wa_ATK =10;
}

//角色类——跳跃
void CRole::Jump() {
	std::cout << "miss" << std::endl;
}

//英雄类——构造函数
CHero::CHero(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP)
	:CRole(sculpture,HP,ATK,DEF,Lv,EXP)
{
	
}

//英雄类——析构函数
CHero::~CHero() {
	
}

//英雄类——踢腿
void CHero::Kicking() {
	k_ATK = 10;
}

//英雄类——抱摔
void CHero::Wresting() {
	w_ATK = 15;
}

//敌人类——构造函数
CEnemy::CEnemy(const std::string sculpture, int HP, int ATK, int DEF, int Lv, int EXP)
	:CRole(sculpture, HP, ATK, DEF, Lv, EXP) 
{
}

//敌人类——析构函数
CEnemy::~CEnemy() {
	
}

//敌人类——劈掌
void CEnemy::Chopping_Palm() {
	p_ATK = 10;
}

//敌人类——连环腿
void CEnemy::Serial_Leg() {
	s_ATK = 16;
}

//战斗类——构造函数
CBattle::CBattle() {
	std::cout << "自动格斗小游戏" << std::endl;
}

//战斗类——准备函数
void CBattle::ready(CHero &obj1, CEnemy &obj2){
	std::cout << "战斗双方" << std::endl;
	obj1.show();
	obj2.show();
	std::cout << "准备战斗" << std::endl;
	for (int i = 3;i > 0;i--) {
		std::cout << i << std::endl;
		Sleep(1000);
	}
	std::cout << "战斗开始" << std::endl;
	Sleep(1000);
}

//战斗类——析构函数
CBattle::~CBattle() {
	
}

//战斗类——格斗函数
void CBattle::fighting(CHero obj1, CEnemy obj2) {
	srand((unsigned)time(0));
	int Blood_Loss;
	int temp, temp1;
	int t = 1;
	//掉血量=攻击方技能伤害+ATK(攻击方的攻击力)-防御方的防御力DEF
	while (obj1.HP > 0 && obj2.HP > 0) {
		std::cout << "第" << t << "回合" << std::endl;
		temp = rand() % (3 - 0 + 1) + 0;//随机生成攻击类型
		temp1 = rand() % (2 - 0 + 1) + 0;//随机产生跳跃躲避攻击,三分之一的概率触发跳跃闪避
		if (t % 2 == 1) {
			switch (temp) {
				case 0://普通攻击
					if (temp1 == 1) {
						obj2.Jump();
						std::cout<< obj1.sculpture << "对" << obj2.sculpture << "使出的普通攻击被" << obj2.sculpture << "跳跃躲避了" << std::endl;
					}
					else {
						Blood_Loss = obj1.ATK - obj2.DEF;//掉血量
						obj2.HP -= Blood_Loss;//obj2剩余血量
						std::cout << obj1.sculpture << "使出普通攻击对" << obj2.sculpture << "造成了" << Blood_Loss << "点伤害" << std::endl;
					}
					break;
				case 1:
					if (temp1 == 1) {//武器攻击
						obj2.Jump();
						std::cout << obj1.sculpture << "对" << obj2.sculpture << "使出的武器攻击被" << obj2.sculpture << "跳跃躲避了" << std::endl;
					}
					else {
						obj1.WeaponAttack();
						Blood_Loss = obj1.ATK + obj1.wa_ATK - obj2.DEF;
						obj2.HP -= Blood_Loss;
						std::cout << obj1.sculpture << "使用武器对" << obj2.sculpture << "造成了" << Blood_Loss << "点伤害" << std::endl;
					}
					break;
				case 2:
					if (temp1 == 1) {//踢腿攻击
						obj2.Jump();
						std::cout << obj1.sculpture << "对" << obj2.sculpture << "使出踢腿攻击被" << obj2.sculpture << "跳跃躲避了" << std::endl;
					}
					else {
						obj1.Kicking();
						Blood_Loss = obj1.ATK + obj1.k_ATK - obj2.DEF;
						obj2.HP -= Blood_Loss;
						std::cout << obj1.sculpture << "使用踢腿对" << obj2.sculpture << "造成了" << Blood_Loss << "点伤害" << std::endl;
					}
					break;
				case 3:
					if (temp1 == 1) {//抱摔攻击
						obj2.Jump();
						std::cout << obj1.sculpture << "对" << obj2.sculpture << "使出抱摔攻击被" << obj2.sculpture << "跳跃躲避了" << std::endl;
					}
					else {
						obj1.Wresting();//抱摔
						Blood_Loss = obj1.ATK + obj1.w_ATK - obj2.DEF;
						obj2.HP -= Blood_Loss;
						std::cout << obj1.sculpture << "使用抱摔对" << obj2.sculpture << "造成了" << Blood_Loss << "点伤害" << std::endl;
					}
					break;
			}
		}
		if (t % 2 == 0) {
			switch (temp) {
			case 0://普通攻击
				if (temp1 == 1) {
					obj1.Jump();
					std::cout << obj2.sculpture << "对" << obj1.sculpture << "使出的普通攻击被" << obj1.sculpture << "跳跃躲避了" << std::endl;
				}
				else {
					Blood_Loss = obj2.ATK - obj1.DEF;//掉血量
					obj1.HP -= Blood_Loss;//obj2剩余血量
					std::cout << obj2.sculpture << "使出普通攻击对" << obj1.sculpture << "造成了" << Blood_Loss << "点伤害" << std::endl;
				}
				break;
			case 1:
				if (temp1 == 1) {//武器攻击
					obj1.Jump();
					std::cout << obj2.sculpture << "对" << obj1.sculpture << "使出的武器攻击被" << obj1.sculpture << "跳跃躲避了" << std::endl;
				}
				else {
					obj2.WeaponAttack();
					Blood_Loss = obj2.ATK + obj2.wa_ATK - obj1.DEF;
					obj1.HP -= Blood_Loss;
					std::cout << obj2.sculpture << "使用武器对" << obj1.sculpture << "造成了" << Blood_Loss << "点伤害" << std::endl;
				}
				break;
			case 2:
				if (temp1 == 1) {//劈掌攻击
					obj1.Jump();
					std::cout << obj2.sculpture << "对" << obj1.sculpture << "使出的劈掌攻击被" << obj1.sculpture << "跳跃躲避了" << std::endl;
				}
				else {
					obj2.Chopping_Palm();//劈掌
					Blood_Loss = obj2.ATK + obj2.p_ATK - obj1.DEF;
					obj1.HP -= Blood_Loss;
					std::cout << obj2.sculpture << "使用劈掌对" << obj1.sculpture << "造成了" << Blood_Loss << "点伤害" << std::endl;
				}
				break;
			case 3:
				if (temp1 == 1) {//连环腿攻击
					obj1.Jump();
					std::cout << obj2.sculpture << "对" << obj1.sculpture << "使出的连环腿攻击被" << obj1.sculpture << "跳跃躲避了" << std::endl;
				}
				else {
					obj2.Serial_Leg();//连环腿
					Blood_Loss = obj2.ATK + obj2.s_ATK - obj1.DEF;
					obj1.HP -= Blood_Loss;
					std::cout << obj1.sculpture << "使用连环腿对" << obj2.sculpture << "造成了" << Blood_Loss << "点伤害" << std::endl;
				}
				break;
			}
		}
		t++;
	}
	std::cout << std::endl<<"战斗结束"<<std::endl;
	if (obj1.HP > 0) {
		int exp;
		exp = obj2.Lv * 10;
		obj1.EXP += exp;
		std::cout << obj2.sculpture << "死亡,";
		std::cout << obj1.sculpture << "胜利!获得" << exp << "经验,目前经验值为:" << obj1.EXP << std::endl;
	}
	if (obj2.HP > 0) {
		int exp;
		exp = obj1.Lv * 5;
		obj2.EXP += exp;
		std::cout << obj1.sculpture << "死亡,";
		std::cout << obj2.sculpture << "胜利!获得" << exp << "经验,目前经验值为:" << obj2.EXP << std::endl;
	}
}

"main.cpp"

// FightingGame.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include"game.h"
#include <iostream>
#include<string>
#include<Windows.h>
#include<time.h>
int main()
{
	//创建普通角色
	CRole Ordy_character1("村民1",100,3,5,1,0);
	CRole Ordy_character2("村民2",100,3,6,1,20);
	CRole Ordy_character3("村民3", 100,4,9,3,100);
	//创建英雄角色
	CHero Hero1("张飞", 100, 19, 5, 1, 0);
	CHero Hero2("刘备", 100, 18, 5, 1, 0);
	CHero Hero3("关羽", 100, 20, 5, 1, 0);
	//创建敌人角色
	CEnemy enemy1("小黄巾1",100,16,3,3,100);
	CEnemy enemy2("小黄巾2",100,16,3,3,100);
	CEnemy enemy3("小黄巾3", 100, 16, 3, 3, 100);
	/*std::cout << "战斗双方" << std::endl;
	Ordy_character1.show();
	Hero1.show();*/
	CBattle battle1;
	battle1.ready(Hero1, enemy1);
	battle1.fighting(Hero1, enemy1);
}

运行结果:

原文地址:https://www.cnblogs.com/izzwhf/p/10707247.html