-2
// in general, it could be potion, armor, weapon, etc.
Item class {
   // states
   id
   name
   // behavior
   set, get of states here..

   use()
   // still thinking what is the other general behavior
} 

// in general, it could be Gun,Sword,Wand, Bow (range,melee,magic)
Weapon class extends Item{
   // gun - fire(); type: range (in the movie, the gun can be use as melee weapon too xD)
   // sword - slash(); type: melee (in the movie, the sword can be thrown :) or use as magic wand?)
   // wand - ??
   // mace - bash();
   // bow - shoot() ??
   // so what is the general behavior of this weapon?
} 

Bag class {} // parent is Item, collection of items here

// Human, Orc, etc
Character class {
   // states
   Weapon

   // behavior
   attack();
   // still thinking what is the other behavior and states
} 

// Swordsman, Magician, Archer, etc.
Hero class extends Character{
} 

上面的代码是我正在开发的角色扮演游戏的 OOP 类,我很难考虑每个类的一般状态和行为。

问题

  • 如何创建这些类(我是 OOP 的初学者)。我听说过一些封装,多态等。
  • 我不知道如何正确使用接口、抽象等。
  • 思考类的一般状态和行为
  • 正如您在武器类中看到的那样。枪、剑、弓可以用作近战、射程甚至魔法。我应该称这些武器类型吗?(范围,近战,魔法)
4

2 回答 2

2

让我们逐行回答您的问题

如何创建这些类(我是 OOP 的初学者)。我听说过一些封装,多态等。

如果不彻底了解 OOPS,就无法开发游戏。花一些时间了解抽象/封装/Polymerphsim/继承。

我不知道如何正确使用接口、抽象等。

也许如果您知道这些意味着什么,您将能够决定何时以及如何使用它们(如果您需要它们)。

考虑类的一般状态和行为正如您在武器类中看到的那样。枪、剑、弓可以用作近战、射程甚至魔法。我应该称这些武器类型吗?(范围,近战,魔法)

在这种情况下,您可以拥有一个名为 Weapons 的接口,其类型为抽象方法。您可以继承它们并为每个武器等进行武器属性更改,例如攻击力、强度等。当然,您需要再次了解 OOPS 概念. 如果您是一个快速学习者,请从观看此视频开始。

于 2016-07-08T11:47:36.627 回答
0

对于像游戏这样的复杂设计,我建议你学习更好的接口和抽象类。那我建议你画一个你的游戏模型的UML图。Weapon 可以是一个带有 fire() 方法的接口,所有实现该接口的武器都有自己的 fire() 方法。

对于一个好的设计,我还建议很好地学习设计模式。如果你把你的游戏设计得很好,将来会很容易地用新的功能扩展你的游戏!

于 2016-07-08T11:55:46.323 回答