Friday, November 23, 2007

An easy way to make stages for fighting games.

I've been working long on Pipe Fighter and last night I came across something I haven't thought of the first time I made Pipe Fighter (I have an old version that I stopped making because I decided to make the RayEngine and make it easier on me). That thing was designing the structure of stages. The first time through, I hardcoded the positions of all the little parts of the background (like houses and the ground). The only thing I didn't hardcode was the lifebars. If you want a tutorial on how to make lifebars, you can go to George Clingerman's XNA website (XNADevelopment) where his tutorial is what I used.

My structure for my stages is pretty simple. I have a class called Level that has 2 main fields (my level class has 4 fields for more stuff that I can add later on for effects).

private int ground
private BackgroundScreen background

I also made properties out of the ground field. The backgroundscreen is basically just an image that is stretched to the size of the screen but has the properties of the Screen class that can be found in the XNA starterkit. The constructor is null. That's the end of the Level class, simple huh?

After we have that done, now it's time to make a specifice level. In PipeFighter, I called it JapanStage, but let's make two stages. In the constructor you have to initiate the ground to a number. You also have to initiate the background.

public JapanStage()
ground = 400;
background = new BackgroundScreen("Content/Graphics/Stages/JapanStage");

public ChinaStage()
ground = 350;
background = new BackgroundScreen("Content/Graphics/Stages/ChinaStage");

My constructor for a backgroundscreen is different from the XNA starterkit, so you may have to modify your code. This is just to give you an idea of the steps to take. That's the end of the class. Easy right?

Now we aren't finished yet. The reason why we made these two classes is because we are going to use them in a class that determines which stage is going to be shown. We will call this class the FightingScreen, which is the same as a Screen in the XNA Starterkit.

public class FightingScreen : Screen
private Level Stage;

public FightingScreen(int ChosenLevel)

private void LoadStage(int whichStage)
if(whichStage == 1)
Level = new JapanStage();
Level = new ChinaStage();

Now you have an easy way to go to different screens. The ground level is the point where you don't want your characters not to go past (on fighting games, you don't want them to go beyond the floor, unless you want them to). Now let's say if you had a Select Level screen and the user wanted to choose a level, all you have to do is something like:

*Psuedo Code*

if(playerChoseLevel == 1)
new FightingScreen(1);
else if (playerChoseLevel == 2)
new FightingScreen(2);
new FightingScreen(RandomNumberBetween(1,2));

That's the basics on how I setup my levels for Pipe Fighter. You can use this approach in any typ of game. There is more you can do to your levels than it just being a background. In the base Level class, you can add a list of ParticleSystems and a List of Sprites and make them properties or public variables. Then in your level classes, you can add custom particle systems and/or sprites and make your backgrounds better. Enjoy.

No comments: