Trying to figure out the hierarchy of classes in m

2019-09-02 19:55发布

问题:

I am still kinda new to developing games in XNA and using classes with C#, but I want to start a semi-decent game project where I can learn without throwaway projects.

I want to devide my game up into classes. For example a main game class, a player class (i want two players, and they battle against each other), a level class (for drawing the world) and maybe later on things like a power up class and a networking class so I can make everything multiplayer.

But I don't know the best way to start using classes like this in an XNA game. Do I make a class like this:

namespace MyGame
{
    class Player : MyGame
    {
        // whatever
    }
}

or like this:

namespace MyGame
{
    class Player : Microsoft.Xna.Framework.Game
    {
        // whatever
    }
}

I want each class to have something along the lines of Initialize(), Update() and Draw() methods, and I will use the spriteBatch and GraphicsDeviceManager from my main game class passed to these classes... but I also don't know the best way to pass them in, or even if I should be passing them in over making new ones in the classes themselves.

Any help is greatly appreciated. Thanks.

回答1:

You probably want to have a hierarchy of GameObjects, so you players and your enemies etc would have a GameObject as the root. Only the main Game class would actually be deriving from Microsoft.Xna.Framework.Game.

If you follow the Xna model of GameComponents, you would have the base GameObject deriving from DrawableGameComponent. Of course the logic of the players and enemies could be extracted out into Controller classes which you can then inject into the GameObject to give each object it's in game behaviors.

I would suggest you start with some of the game samples at XNA Creators Club, it's currently down but I am sure it will be up soon. Use these samples to help you formulate your game structure.

You might also want to check out Riemer's XNA Tutorials. This is a quite a nice introduction.



标签: c# oop class xna