C#/XNA 新手有一个设计问题

发布于 2024-10-12 03:35:03 字数 581 浏览 2 评论 0原文

我正在尝试使用 C# 和 XNA 设计一个小游戏,在开始之前我想确保这种方法有意义:

基本上,我想创建一个 GameObject 类,其中包含与我将要做的所有内容通用的所有元素在屏幕上绘制,无论是武器、怪物、人类、皮卡等。

所以我想我应该创建一个名为 GameObject 的抽象类,其属性用于设置 Texture2D、Vector2 位置、Draw() 方法和 Move() 方法。

另外,由于我有人类和怪物,我可以创建一个名为 Biped 或类似的基类,并给它一个“AttackBehavior”属性(它本身就是一个类),这样我就可以创建不同的攻击方法(人类攻击怪物,怪物攻击人类)。我将在 GameObject 类中使用 Move() 方法的抽象实现,这样我就可以为不移动的物品(武器等)覆盖它,并为我的人类和怪物提供不同的移动速度、模式等。

所以我的问题是:

  • 这种设计模式有意义吗?我一直在阅读《Head First Design Patterns》并阅读有关组合对象(因此是我的 AttackBehavior 对象)的内容,
  • 我应该在 Game1.cs 中放入什么类型的代码?这应该是 MVC 模式中的“视图”吗?

I'm trying to design a small game using C# and XNA, before I start I wanted to make sure that this approach makes sense:

Basically, I want to create a GameObject Class that contains all elements that are common with everything I am going to draw on screen, be it a Weapon, Monster, Human, Pickup, etc.

So I was thinking I should create an Abstract Class called GameObject with Properties to set the Texture2D, Vector2 Location, A Draw() Method and a Move() Method.

Also, as I have Humans and Monsters, I could create a base class called Biped or similar and give it an "AttackBehavior" property (Which is a class in itself) so then I can create different attack methods (Humans attack Monsters, Monsters attack Humans). I would use an abstract implementation of the Move() method in my GameObject class so I can override it for items that don't move (Weapons, etc) and give different Move Speeds, patterns, etc to my Humans and Monsters.

So my questions are:

  • Does this design pattern make sense? I've been reading Head First Design Patterns and reading about Composing objects (Hence my AttackBehavior object)
  • What type of code should I put in my Game1.cs? Should this be the 'View' in the MVC pattern?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情深如许 2024-10-19 03:35:03

从表面上看,是的,这个设计是有意义的……但根据我的经验(当然是 YMMV),我发现尝试从一开始就设计深层对象层次结构会导致以后的决策受到限制。由于每个游戏都是独一无二的,因此当我刚开始编码、向游戏添加元素并进行重构时,我会得到更好的结果。如果有两个具有相似功能的类,那么我要么让它们从公共基础继承,要么重构为可以重用的公共类。这让您两全其美。

对于你的第二个问题,我衷心建议你组织游戏的方式是使用游戏状态管理示例:
http://create.msdn.com/en-us/samples/gamestatemanagement

下载示例,并好好学习......这是一个经过验证的真实模式

At the surface, yes the design makes sense ... but in my experience (YMMV of course) I've found that trying to start by designing a deep object hierarchy from the get-go results in decisions that will limit you later. Since every game is unique, I have better results when I just start coding, adding elements to the game, and refactoring as I go. If there are two classes with similar functionality, then I either make them inherit from a common base, or refactor out into a common class which can be re-used. That gives you the best of both worlds.

to your second question, the way that I wholehearted suggest you organize your game is by using the game state management sample:
http://create.msdn.com/en-us/samples/gamestatemanagement

download the sample, and learn it well ... it's a tried and true pattern

早乙女 2024-10-19 03:35:03

顺便说一句,我认为武器不应该与能够移动的东西属于同一类。

还要尝试将“模型”与精灵分开(想想游戏的 MVC)。拥有一个既能让行为作为视觉扩展的基础对象可能并不总是最好的主意。但对于简单的游戏来说,这是最快(也许也是)最好的方法。您所考虑的尺寸确实很重要。 (一开始总是从小事做起)。

On a side note, I don't think that a weapon should ever be in the same class as something that is able to move.

Also try to separate your "model" from your sprites (think MVC for games). It might not always be the best idea to have a base object that both lets behaviour as visual extend. But for a simple game this is the quickest (and maybe) best approach. Really matters what size you're thinking. (Always start small in the beginning).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文