题目描述:
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(攻击方的攻击力)-防御方的防御力DEFwhile (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);}
运行结果: