简体   繁体   English

在基本的C ++攻击游戏中需要帮助实现Health

[英]Need help implementing Health in basic C++ attack game

The main value of this extremely simple attack game (for me) is to familiarize myself with simple polymorphism, and to practice using pointers. 这个极其简单的攻击游戏(对我来说)的主要价值在于熟悉简单的多态性,并练习使用指针。 With that said, I would love to add onto it. 话虽如此,我很乐意添加它。 Looking at my program, I'm not sure If I should create a separate "Hero" class, and to just have that inherit from the "Character" class, or if I should just assume that the Hero IS the "Character" class, which the enemies inherit from. 看看我的程序,我不确定如果我应该创建一个单独的“Hero”类,并且只是从“Character”类继承,或者我应该假设Hero是“Character”类,敌人继承的是哪个。 The only thing I want to do right now is to implement something where the Hero's health decreases after attack. 我现在唯一想做的就是实施一些攻击后英雄健康状况下降的东西。 Sorry if it's very rudimentary, I'm just trying to get the basics down. 对不起,如果它非常简陋,我只是想把基础知识搞定。

Thanks. 谢谢。

#include <iostream>
#include "Character.h"

using namespace std;

int main() {

    Ninja n;
    Dragon d;

    Character *enemy1 = &n;
    Character *enemy2 = &d;

    enemy1->setAttackPower(20);
    enemy2->setAttackPower(40);

    n.attack();
    d.attack();

    return 0;
}


//Character.h
#include <iostream>

using namespace std;

class Character 
{
    protected:

    int Health = 100;
    int attackpower;

    public:

    void setAttackPower(int attack) {
        attackpower = attack;
    }
};

class Ninja: public Character
{
    public:

    void attack() {
        cout << "Ninja attacks your character! - " << attackpower << " Points!" << endl;
    }
};

class Dragon: public Character
{
    public:

    void attack() {
        cout << "Dragon attacks your character! - " << attackpower << " Points!" << endl;
    }
};

I am having some trouble identifying specific questions in your post, but there are some suggested questions. 我在发布帖子中的具体问题时遇到了一些问题,但是有一些建议的问题。 Namely, how do you make a hero character and have him 'fight' these other characters. 也就是说,你如何制作一个英雄角色并让他“打”这些其他角色。 For one, you need a hero character. 首先,你需要一个英雄角色。 This can indeed just be another child of Character. 这确实只是Character的另一个孩子。 Second, attack actually needs to specify a target. 其次,攻击实际上需要指定一个目标。

n.attack()

is all well and good, but what is he attacking? 一切都很好,但是他在攻击什么? That isn't an issue right now because all attack does is print, but you want it to do more. 这不是一个问题,因为所有攻击都是打印,但你希望它做得更多。 You want it to attack a particular character, so you should make it take a character as an argument 你希望它攻击一个特定的角色,所以你应该把它作为一个参数

void attack ( Character &char )
or
void attack ( Character *char )

for instance. 例如。

Now, there are two big issues. 现在,有两个大问题。 It seems you made this for polymorphism testing, but you're not really fully exploiting it. 看来你是为多态性测试而做的,但你并没有真正充分利用它。 First, you should promote attack to be an abstract virtual function in Character: 首先,你应该将攻击提升为Character中的抽象虚函数:

class Character {
public:
  virtual void attack ( Character *char ) = 0;

};

class Ninja {
public:
  virtual void attack ( Character *char) 
  {

  }
};

Also, now you can use polymorphism when calling attack on your characters: 此外,现在您可以在对角色进行攻击时使用多态性:

enemy1->attack(Hero);
enemy2->attack(Hero);

Change your attack function to take a pointer to the targeted character, then you can reduce the targeted character's health. 更改攻击函数以获取指向目标角色的指针,然后可以减少目标角色的健康状况。 You'll be able to access the protected health data of the target directly. 您将能够直接访问目标的受保护健康数据。

void attack(Character* target)
{
  //specific printouts...
  target->Health -= attackpower;
}

Another pattern to use is to keep the generic code in the base class and then from there call a derived function that does character specific things, like: 另一种使用的模式是将通用代码保存在基类中,然后从那里调用一个派生函数来执行特定于字符的事情,例如:

class Character
{
  //...
  void attack(Character* target)
  {
    target->Health -= attackpower;
    DerivedAttack();
  }

  virtual void DerivedAttack() = 0;

};

class Dragon:public Character
{
  //...
  void DerivedAttack()
  {
    cout << "Dragon attacks your character! - " << attackpower << " Points!" << endl;
  }
};

I think that the best use of polymorphism is the one that matches what we think in real life. 我认为多态性的最佳用途是与我们在现实生活中的想法相匹配的那种。

So, I would suggest you have the Character class which has all the common features of a hero and an enemy. 所以,我建议你有一个Character类,它具有英雄和敌人的所有共同特征。 Then you have two separate classes (the Hero class and the Enemy class) both inherit from Character . 然后你有两个独立的类( Hero类和Enemy类)都继承自Character However, each implements its own specific methods unique to Heros and Enemies. 但是,每个都实现了Heros和Enemies独有的特定方法。

Example of things in Character class: attack power and health (like you have in your code) Character类中的事物示例:攻击强度和健康状况(就像您的代码中一样)

Example of things in Hero and Enemy class: image of the enemy or hero (or special unique weapon for the hero), etc. HeroEnemy类中的事物示例:敌人或英雄的图像(或英雄的特殊武器)等。

Hence, the code you provided with the Ninja and Dragon makes a lot of sense to me. 因此,你为Ninja和Dragon提供的代码对我来说很有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM