0
votes

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
I want to start a semi-decent game project where I can learn without throwaway projects - I'd just like to point out what a terrible plan this is. By all means create a complete project from end-to-end. But make it incredibly simple. Tic-Tac-Toe is probably a good starting point - you shouldn't even need to create any classes beyond MyGame. After that make (for example) Pong - where you can have a Paddle class and an instance for each paddle (you might want to read stackoverflow.com/questions/3879204 this question). Save "semi-decent" until after you can make "crap".Andrew Russell

1 Answers

3
votes

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.