As it is my first game let’s assume from the outset that it will not be commercial. Even though XNA games can easy support the XBox360 and Windows Phone 7, mine is going to target only the PC. I don’t own either of these and I they would just slow me down without really adding anything new.
Creating a revolutionary new concept from scratch is probably a bit extreme for a first outing so it will be based on some classic favorites of gaming.
The concept is that the man has fallen down to the bottom of the well has to jump around to stay on top of the falling bricks, before fighting his way out and raising the flag. Otherwise he will be overwhelmed and crushed to death.
An hour in Photoshop creates a few images to work with.
I'm not going to try to teach you all of XNA but here are a few bits I found interesting. XNA follows a cycle, similar to the ASP.NET page life-cycle. Game are made of "GameComponents", such as images and audio files. When you want to create a new image you just need to inherit from DrawableGameComponent and override four methods. The framework then will call these at the appropriate times.
public virtual void Initialize() protected virtual void LoadContent() public virtual void Update(GameTime gameTime) public override void Draw(GameTime gameTime)Time for some code. Take a quick look at my next post for an example class of our Hero character. You don't have to read it all, but just take a quick look and see how small the file is. Pretty small considering he can run, jump and fall down with realistic gravity. He also has five different poses to give the appearance of movement.
If you do read it, you'll see that nothing here is really that hard. Most of it is regular .NET. The only confusing bits are the new types; Texture2D and SpriteBatch. But you don't really have to interact with these very much. You could just put these two into a base class shared by all your images, and then forgot about them. You can't even tell that we are using DirectX.
To highlight the Update method:
private ElapsedTime UpdateElapsedTime = new ElapsedTime(100); public override void Update(GameTime gameTime) { // If enough time has passed then we are ready to update the hero if (UpdateElapsedTime.Ready(gameTime)) { // Do the speed and position etc.... } }
This is interesting because it means that the updating code will not run at every opertunity. I will only run every 100ms (or ten times per second). So if the computer is very powerful the game will not runaway and become too fast. Similarly if the computer temporarily runs slowly and falls behind on its updates; then it will later update more frequently to compensate. This means that you get a consistent gaming experiance, dispite the level of your machine.
It was a little tricky to find the optimum length of time to wait. At one point I thought making this value smaller would make the game more responsive. However, this seams to not always be the case. When I set it to update every 10ms the game started to look jerky. I think Update() was using up too much time, leaving the XNA framework with no time left to call my Draw() method. By increasing the wait to 100ms, I found that this was less demanding and allowed Draw() to be called more frequently.
To highlight gravity:
// Apply the force of gravity VelocityY += 100f;That's it. To make your monster fall realistically only takes one line of code. Every time Update() is executed his vertical velocity will increase, resulting in him falling in realistic arc.
0 comments:
Post a Comment