本文主要是介绍深蓝学院C++ Project 4 文字版挂机地牢探险游戏的设计 代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
写在前面
调试了周末的半天,算是夯实c++基础与深度解析类的实战,对virtual函数等都有了更好的理解
本机环境visual studio2019 如下图
项目一共有两个文件 下面是代码详情,方便大家参考,毕竟我做的时候找了好久也没找打这个项目的完全版本,算是帮大家加深理解,这个项目可能有一些小bug还没细致排查,至少目前没发现,后续再深入改造。代码如下:
project4.h文件
#pragma once
#include<vector>
#include <windows.h>using namespace std;class role
{
public:int life_value_act;//实际血量int att_value;//攻击值int life_value_max;//血量上限int weapon_kind;//携带武器的种类role();virtual void init_life_bar() = 0;bool judge_live();virtual void de_life(int damage_v, int index);~role();};class people :public role
{
public:int exp_act;//当前经验值int poi_sta_value;//中毒状态的叠加数量vector<int> poi_state_room;//每个中毒状态经过的房间数量倒计时int blood_sta_value;//回血状态的叠加数量vector<int> blood_state_room;int num_master;//击杀怪物数量int num_rooms;//当前房间数量people();void init_life_bar();void print_exp_change(int exp_increase);void exp_add(int room_label);//增加经验void de_life(int damage_v, int master_index);//现在探险者类中实现,如果顺利将功能放在房屋的类中void blood_act();//加血void poi_act();void print_result();~people();
private:int exp_max;//最大经验值
};class master :public role {
public:int master_kind;int master_index;int att_value_save;int master_king_num;//首领怪物攻击次数bool master_king_recover;//首领怪物恢复标志位master(int kind_v, int index_v);void init_life_bar();void init_weapon(int weapon_k);void de_life(int damage_v, int index);~master();
private:int basic_life = 10;int basic_attack = 5;
};class room {
public:room();virtual void init_room(people& p);virtual void battle_event(people& p);virtual void result(people& p);~room();
};class campsite :public room {
public:campsite();void init_room(people& p);void print_campsite();~campsite();
private:static int num_campsite_room;//营地房间数量
};class normal_room :public room {
public:normal_room();void init_room(people& p);void battle_event(people& p);void result(people& p);void print_normal_room();~normal_room();
private:static int num_normal_room;
};class trap_room :public room {
public:trap_room();void init_room(people& p);void battle_event(people& p);void result(people& p);void print_trap_room();~trap_room();
private:static int num_trap_room;
};class king_room :public room {
public:king_room();void init_room(people& p);void battle_event(people& p);void result(people& p);void print_king_room();~king_room();
private:static int num_king_room;
};class weapon_room :public room {
public:weapon_room();void init_room(people& p);void battle_event(people& p);void result(people& p);void print_weapon_room();~weapon_room();
private:static int num_weapon_room;int life_value_record_;
};
project4.cpp文件
#include"project4.h"
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<ctype.h>
#include<algorithm>
#include <cstdlib>
#include <ctime>using namespace std;role::role() {this->weapon_kind = 0;//没有携带武器
}bool role::judge_live() {return this->life_value_act > 0;
}
void role::de_life(int damage_v, int index) {this->life_value_act = this->life_value_act - damage_v;
}
role::~role() {
}
//定义探险者的成员函数
people::people() {this->exp_max = 10;this->exp_act = 0;this->poi_sta_value = 0;this->blood_sta_value = 0;this->num_master = 0;this->att_value = 10;this->num_rooms = 0;this->blood_state_room = { 0 };this->poi_state_room = { 0 };
}
void people::init_life_bar() {this->life_value_max = 100;this->life_value_act = this->life_value_max;
}
void people::print_exp_change(int exp_increase)
{cout << "获得" << exp_increase << "点经验, 当前经验("<< exp_act <<"/" << exp_max << ")" << endl;
}
void people::exp_add(int room_label) {if (room_label==1 or room_label==2) {this->exp_act += (this->num_master);print_exp_change((this->num_master));}else if (room_label==3) {this->exp_act += 5;print_exp_change(5);}else if (room_label==4) {this->exp_act += (2);print_exp_change(2);}if (this->exp_act >= this->exp_max) {this->life_value_act = this->life_value_max;this->exp_act = this->exp_act % this->exp_max;cout << "恢复生命值:" << life_value_act << endl;cout << "当前经验值:" << exp_act << " / " << exp_max << ")" << endl;}
}
void people::de_life(int damage_v, int master_index) {//现在探险者类中实现,如果顺利将功能放在房屋的类中if (damage_v > 0) {this->life_value_act -= damage_v;cout << "怪物" << master_index << "攻击探险者,造成" << damage_v << "点伤害,探险者(" <<this->life_value_act << " / " << life_value_max << ")" << endl;}
}void people::blood_act() {this->life_value_act = min(this->life_value_max, this->life_value_act + (this->blood_sta_value));cout << "持续恢复效果触发,获得" << (this->blood_sta_value) << "点生命,当前生命(" << life_value_act << " / " << life_value_max << ")" << endl;//cout<<"效果剩余:"<<blood_sta_value<<" "<<blood_state_room[0]<<endl;for (auto i = this->blood_state_room.begin(); i<this->blood_state_room.end(); i++) {*i = *i - 1;if (*i <= 0) {if (i == this->blood_state_room.begin()) {//cout<<"condition1"<<endl;this->blood_state_room[0] = 0;}else {//cout<<"condition2"<<endl;this->blood_state_room.erase(i);}this->blood_sta_value = max(0, this->blood_sta_value - 1);}}//cout<<"效果剩余:"<blood_sta_value<<" "<blood_state_room[0]<<endl;
}
void people::poi_act() {this->life_value_act -= (this->poi_sta_value);cout << "持续损伤效果触发,失去" << (this->poi_sta_value) << "点生命,当前生命(" << life_value_act << " / " << life_value_max << ")"<<endl;for (auto i = this->poi_state_room.begin(); i<this->poi_state_room.end(); i++) {*i = *i - 1;if (*i <= 0) {if (i == this->poi_state_room.begin()) {//cout<<"condition1"<<endl;this->poi_state_room[0] = 0;}else {//cout<<"condition2"<<endl;this->poi_state_room.erase(i);}this->poi_sta_value = max(0, this->poi_sta_value - 1);}}}
void people::print_result() {campsite c_room;normal_room n_room;trap_room t_room;king_room k_room;weapon_room w_room;cout << "探险者死亡,共走过" << num_rooms << "个房间." << endl;c_room.print_campsite();n_room.print_normal_room();t_room.print_trap_room();k_room.print_king_room();w_room.print_weapon_room();
}
people::~people() {
}
//怪物类的成员函数
master::master(int kind_v, int index_v) {this->master_index = index_v;this->master_kind = kind_v;this->master_king_num = 0;this->master_king_recover = true;this->att_value_save = 10;this->att_value = 5;
}void master::init_life_bar() {if (this->master_kind==1)//对应基础怪物{//按设定随机生成+— 40%范围的属性值this->life_value_max = ( rand() % (basic_life * 14 - basic_life * 6 + 10) ) / 10 + basic_life*6 / 10;this->att_value = (rand() % (basic_attack * 14 - basic_attack * 6 + 10)) / 10 + basic_attack * 6 / 10;cout << "遇到怪物" <<master_index << "(" << life_value_max << " / " << life_value_max << ")" << endl;}else if (this->master_kind==2)//对应陷阱房间怪物{this->life_value_max = this->basic_life*2;this->att_value = this->basic_attack*2;cout << "遇到陷阱怪物" << "(" << life_value_max << " / " << life_value_max << ")" << endl;}else if (this->master_kind==3)//对应怪物首领{this->life_value_max = 40;this->att_value = 14;cout << "遇到首领怪物" << "(" << life_value_max << " / " << life_value_max << ")" << endl;}else if (this->master_kind==4)//武器房间的怪物{people p;p.init_life_bar();this->life_value_max = p.life_value_max*4 / 10;this->att_value = p.att_value*4 / 10;cout << "遇到变异怪物" << "(" << life_value_max << " / " << life_value_max << ")" << endl;}this->life_value_act = this->life_value_max;
}
void master::init_weapon(int weapon_k) {this->weapon_kind = weapon_k;//怪物房间携带武器种类
}
void master::de_life(int damage_v, int index) {if (this->master_kind==1 or this->master_kind==2 or this->master_kind==4){this->life_value_act -= damage_v;cout << "探险者攻击怪物" << master_index << ", 造成" << damage_v << "点伤害,怪物" << master_index<< "(" << life_value_act << " / " << life_value_max << ")" << endl;}else if (this->master_kind==3) {this->life_value_act -= damage_v;cout << "探险者攻击怪物首领, 造成" << damage_v << "点伤害,首领怪物" << master_index<< "(" << life_value_act << " / " << life_value_max << ")" << endl;if (this->att_value==0) {this->att_value = this->att_value_save;}else if (this->life_value_act <= 10 && this->master_king_recover==true) {this->att_value_save = this->att_value;this->att_value = 0;this->life_value_act += 20;this->master_king_recover = false;cout << "怪物首领恢复生命20点,怪物首领" << "(" << life_value_act << " / " << life_value_max << ")" << endl;}else{this->att_value += 1;//攻击力增加}}}
master::~master() {
}
//营地类的成员函数
room::room() {//cout<<"创建一个房间"<<endl;
}
void room::init_room(people& p) {
}
void room::battle_event(people& p) {
}
void room::result(people& p) {
}
room::~room() {//cout<<"删除当前房间"<<endl;
}campsite::campsite() {
}
void campsite::init_room(people& p) {num_campsite_room++;cout << "进入营地房间" << endl;p.num_rooms++;if (p.poi_sta_value > 0){p.poi_act();}if (p.blood_sta_value > 0) {p.blood_act();}p.life_value_act = p.life_value_max;cout << "探险者恢复全部生命值,当前生命值:(" << p.life_value_act << " / " << p.life_value_max << ")." << endl;
}
void campsite::print_campsite() {cout << "进入营地数量:" << num_campsite_room << endl;
}
campsite::~campsite() {
}
int campsite::num_campsite_room = 0;//普通房间的成员函数
normal_room::normal_room() {
}
void normal_room::init_room(people& p) {num_normal_room++;cout << "进入普通房间" << endl;p.num_rooms++;if (p.poi_sta_value > 0){p.poi_act();}if (p.blood_sta_value > 0) {p.blood_act();}int probability_re = rand() % 100;//cout<<"事件概率:"<<probability_re<<endl;if (probability_re < 10) {p.life_value_act = min(100, p.life_value_act + 10);//恢复10点生命力cout << "触发事件,恢复10点生命,获得持续恢复效果,当前生命(" << p.life_value_act << "/" << p.life_value_max << ")." << endl;p.blood_sta_value++;if (p.blood_sta_value == 1 && p.blood_state_room[0] == 0) {p.blood_state_room[0] = 2;}p.blood_state_room.push_back(2);}if (!p.judge_live()) {//探险者去世事件p.print_result();}
}
void normal_room::battle_event(people& p) {//cout<<"战斗开始"<<endl;int num_master = rand() % 3 + 1;vector<master> masters;for (int i = 1; i <= num_master; i++) {master m1(1, i);m1.init_life_bar();masters.push_back(m1);}sort(masters.begin(), masters.end(), [](master& m1, master& m2) {return m1.att_value > m2.att_value;});//按照战斗力对怪物排名cout << "战斗开始" << endl;while (p.judge_live()) {//开始战斗if (masters.size() > 0) {masters[0].de_life(p.att_value, 1);if (!masters[0].judge_live()) {cout << "怪物" << masters[0].master_index << "死亡" << endl;masters.erase(masters.begin());p.num_master++;}for (int i = 0; i < masters.size(); i++) {p.de_life(masters[i].att_value, masters[i].master_index);if (!p.judge_live()) {p.print_result();break;}}}else {//所有怪兽被消灭break;}}if (!p.judge_live()) {//探险者去世事件p.print_result();}
}
void normal_room::result(people& p) {p.exp_add(1);p.num_master = 0;//击杀怪物清零
}
void normal_room::print_normal_room() {cout << "进入普通房间:" << num_normal_room << endl;
}
normal_room::~normal_room() {
}
int normal_room::num_normal_room = 0;
//陷阱房间类成员函数
trap_room::trap_room() {
}
void trap_room::init_room(people& p) {num_trap_room++;cout << "进入陷阱房间" << endl;p.num_rooms++;if (p.poi_sta_value > 0){p.poi_act();}if (p.blood_sta_value > 0) {p.blood_act();}int reduce_v = max(1, (p.life_value_act / 10));p.life_value_act -= reduce_v;//生命数<20,则每次减1.cout << "触发事件,损失" << reduce_v << "点生命,造成持续损伤,当前生命(" << p.life_value_act << " / " << p.life_value_max << ")." << endl;p.poi_sta_value++;if (p.poi_sta_value == 1 && p.poi_state_room[0] == 0) {p.poi_state_room[0] = 5;}p.poi_state_room.push_back(5);if (!p.judge_live()) {//探险者去世事件p.print_result();}
}
void trap_room::battle_event(people& p) {master m1(2, 1);m1.init_life_bar();cout << "战斗开始" << endl;while (p.judge_live()) {m1.de_life(p.att_value, 1);if (!m1.judge_live()) {cout << "怪物" << m1.master_index << "死亡" << endl;p.num_master = 1;break;//怪物死亡跳出循环}p.de_life(m1.att_value, m1.master_index);if (!p.judge_live()) {//探险者去世事件p.print_result();}}
}
void trap_room::result(people& p) {p.exp_add(2);p.num_master = 0;//对怪物数量清零
}
void trap_room::print_trap_room() {cout << "进入陷阱房间:" << num_trap_room << endl;
}
trap_room::~trap_room() {
}
int trap_room::num_trap_room = 0;//首领房间
king_room::king_room() {
}
void king_room::init_room(people& p) {num_king_room++;cout << "进入怪物首领房间" << endl;p.num_rooms++;p.poi_sta_value = 0;p.poi_state_room = { 0 };if (p.blood_sta_value > 0) {p.blood_act();}p.life_value_act = min(p.life_value_max, p.life_value_act + 20);//最大为上限值cout << "生命恢复, 当前生命(" << p.life_value_act << " / " << p.life_value_max << ")." << endl;cout << "持续损伤效果被移除" << endl;
}
void king_room::battle_event(people& p) {master m1(3, 1);m1.init_life_bar();cout << "战斗开始" << endl;while (p.judge_live()) {m1.de_life(p.att_value, 1);if (!m1.judge_live()) {cout << "首领怪物死亡" << endl;break;//怪物死亡跳出循环}p.de_life(m1.att_value, m1.master_index);if (m1.att_value != 0) {m1.master_king_num += 1;}if (!p.judge_live()) {//探险者去世事件p.print_result();}}
}
void king_room::result(people& p) {p.exp_add(3);
}
void king_room::print_king_room() {cout << "进入怪物首领房间数量:" << num_king_room << endl;
}
king_room::~king_room() {
}
int king_room::num_king_room = 0;//武器房间类初始化
weapon_room::weapon_room() {
}
void weapon_room::init_room(people& p) {num_weapon_room++;cout << "进入武器房间" << endl;p.num_rooms++;this->life_value_record_ = p.life_value_act;if (p.poi_sta_value > 0){p.poi_act();}if (p.blood_sta_value > 0) {p.blood_act();}if (!p.judge_live()) {//探险者去世事件p.print_result();}
}
void weapon_room::battle_event(people& p) {master m1(4, 1);m1.init_life_bar();cout << "战斗开始" << endl;while (p.judge_live()) {m1.de_life(p.att_value, 1);if (!m1.judge_live()) {cout << "武器怪物死亡" << endl;break;//怪物死亡跳出循环}p.de_life(m1.att_value, m1.master_index);if (!p.judge_live()) {//探险者去世事件p.print_result();}}
}
void weapon_room::result(people& p) {p.exp_add(4);p.life_value_act = this->life_value_record_;//恢复记录生命cout << "探险者恢复进入房间时生命值(" << p.life_value_act << " / " << p.life_value_max << ")" << endl;
}
void weapon_room::print_weapon_room() {cout << "进入武器房间数量:" << num_weapon_room << endl;
}
weapon_room::~weapon_room() {
}
int weapon_room::num_weapon_room = 0;int main() {people explorer1;explorer1.init_life_bar();//初始化探险家cout << "这里有一个人" << endl;//people *ref_ex=&explorer;int probility_room = 0;room* ptr_room = new room();srand(time(0));while (explorer1.judge_live()) {probility_room = rand() % 100;//通过随机函数生成不同的房间//cout<<"生成房间概率"<<probility_room<<endl;if (probility_room <= 9) {ptr_room = new campsite();}else if (probility_room <= 64) {ptr_room = new normal_room();}else if (probility_room <= 79) {ptr_room = new trap_room();}else if (probility_room <= 89) {ptr_room = new king_room();}else if (probility_room <= 99) {ptr_room = new weapon_room();}ptr_room->init_room(explorer1);if (!explorer1.judge_live()) {break;}Sleep(1000);ptr_room->battle_event(explorer1);if (!explorer1.judge_live()) {break;}Sleep(1000);ptr_room->result(explorer1);}return 1;}
运行结果
这篇关于深蓝学院C++ Project 4 文字版挂机地牢探险游戏的设计 代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!