C++ small game practice

Let's imagine a simple scenario:

Basic implementation (use Class to encapsulate monsters and heroes)

There is a hero and a monster: 1. We can initialize the hero's blood volume, mana, and attack power, and the monster's blood volume and attack power in the terminal. 2. The hero has 4 skills, each skill has a certain damage (or can increase blood), but needs to consume a certain amount of mana (can be written in the code). (You can consider the cooling time, for example, the first skill can only be played once after 3 rounds). 3. The game is a turn-based game. Heroes can use skills and normal attacks, while monsters can only use normal attacks. First the hero attack, then the monster attack. Then there is the hero attack, followed by the monster attack. cycle in turn. The hero can choose the skill or the normal attack through the input of the keyboard, but if the mana of the hero is not enough, the skill cannot be released, so the attack fails, and this round will not cause any damage. After each round, the hero will recover a certain amount of mana. (As the number of rounds increases, the mana recovery ability of the hero increases) 4. Output the final winning result. 5. You can end the game by typing q. In a round, you can enter a command to view the information of both sides (the hero's blood volume, blue volume, attack power, recovery ability and monster's blood volume, attack power)

#include <iostream>
using namespace std;

class NPC{
public:
	NPC();
	NPC(int Health, int Attack);
	~NPC();

	int Health, Attack;
private:

};

NPC::NPC() {
}

NPC::NPC(int Health, int Attack) {
	this->Health = Health;
	this->Attack = Attack;
}

NPC::~NPC(){
}

class Hero : public NPC{
public:
	Hero(int Health, int Blue, int Attack);
	~Hero();

	int Blue;
	void Normal_Attackl(NPC &n);   //普通攻击
	void Attack_Skill(NPC &n);	  //攻击技能
	void Restoration_Skill(NPC &n);//回复技能
	
private:

};

Hero::Hero(int Health, int Blue, int Attack){
	this->Health = Health;
	this->Attack = Attack;
	this->Blue = Blue;
}

Hero::~Hero(){
}

void Hero::Normal_Attackl(NPC &n) {
	cout << "hero使用了普通公鸡,对monster造成了20点伤害!" << endl;
	n.Health -= this->Attack;
}

void Hero::Attack_Skill(NPC &n) {
	if (this->Blue - 20 < 0) {
		cout << "hero蓝量不足,释放火球失败,未对monster造成伤害!" << endl;
	}
	else {
		this->Blue -= 20;
		cout << "hero释放了豪火球之术,对monster造成了30点伤害!" << endl;
		n.Health -= 30;
	}
}

void Hero::Restoration_Skill(NPC &n) {
	if (this->Blue - 10 < 0) {
		cout << "hero蓝量不足,释放回复技能失败,未回复生命值!" << endl;
	}
	else {
		this->Blue -= 10;
		cout << "hero释放了圣光沐浴,回复生命值50!" << endl;
		this->Health += 50;
	}
}
int main() {
	int i = 1; //记录回合数
	int attack;    //匹配攻击方式
	Hero hero(100,50,10);  //NPC属性初始化
	NPC monster(100, 20);
	cout << "战斗开始"<<endl;
	while (1) {
		cout << "<*******************************第" << i << "回合*******************************>" << endl;
		cout << "请选择指令:" << endl;
		cout << "1:普通公鸡  2:豪火球之术(攻击技能)  3:圣光沐浴(回复技能)  4: 撤退" << endl;
		cin >> attack;
		while(attack != 1 & attack != 2 & attack != 3 & attack != 4) {
		cout << "输入指令无效,请重新输入!" << endl;
		cin >> attack;
		}
		if (attack == 1) {
			hero.Normal_Attackl(monster);
		}
		if (attack == 2) {
			hero.Attack_Skill(monster);
		}
		if (attack == 3) {
			hero.Restoration_Skill(monster);
		}
		if (attack == 4) {
			break;
		}
		if (monster.Health <= 0) {  //判断怪兽死否死亡,若死亡退出循环
			break;
		}
		cout << "monster使用了普通公鸡,对hero造成了20点伤害!" << endl;
		hero.Health -= monster.Attack;

		if (hero.Health <= 0) {     //判断英雄死否死亡,若死亡退出循环
			break;
		}
		cout << "第" << i << "回合结束" ;
		i++;
		hero.Blue += 50 / (10 - i);  //蓝量回复力随着回合的增加而增强
		cout << "    hero当前血量:" << hero.Health << "  蓝量:" << hero.Blue;
		cout << "    monster当前血量:" << monster.Health << endl;
		cout << endl;
	}
	
	if (attack == 4) {
		cout << "撤退成功,战斗结束!" << endl;
	}
	else if (hero.Health > monster.Health) {
		cout << "monster死亡,对战结束,hero胜利!" << endl;
	}
	else {
		cout << "hero死亡,对战结束,monster胜利!" << endl;
	}
	
	return 0;
}

 

Improving C++ is an object-oriented (OOP) language. In the above task, the functions and attributes are only packaged using Class, which cannot be called OOP (it should be called ADT). OOP has three properties:

  • Encapsulation: Encapsulation has been attempted in the above process
  • inherit:? ?
  • Polymorphism: ? ?

Think about how inheritance and polymorphism can be added to the code above.

Tips: Heroes and monsters have HP and attack power. Is it understandable that they can have the same parent class. If they belong to the same parent class, how does polymorphism apply? ?

 

Guess you like

Origin blog.csdn.net/weixin_44406127/article/details/131585212