Friday 6 December 2013

An update on the code – Shiny Spice


First things first, we wanted to a create a solid platformer framework: one where the player can walk around and jump, collision with tiles is detected and levels can be loaded from a selected file. Now we have finished that, we can use that as a starting point and more easily divide work. 

The first thing we did was getting the player on the screen and making it move. Fortunately, it can inherit from the GameObject and SpriteGameObject class from the GameManagement library. That gives it a position  and a velocity variable. The position simply announces where the sprite of the player has to be drawn, and the velocity updates the position every frame. All what needs to be added is reacting to the player’s input, and moving the player is done.

A second step for the movement is making is realistic, you can’t just make your character move in any direction. Moving left and right is easy, but moving up and down goes a little differently: moving up goes by jumping and most people, including our main character, can’t actually jump while already moving up. So, that must be accounted for. Also, moving down goes automatically unless something else is in the way, because of this thing called “gravity”. When this is all implemented, the part of moving the player is finished and looked a little like this:



Next up is getting a level to load from a file. Obviously programming in every single level isn’t going to work, so a better option is to add a folder to the Content which contains all the levels in a text file. The harder part is getting these levels read and loaded. The way to do that is with a StreamReader: it reads the entire textfile, analyses the width and length of the text and checks every character. If it’s a ‘.’ Or an ‘F’, it creates a background tile or an actual block tile.

Then, the collision with those tiles needs to be handled. Something to be noted is that all those tiles are also saved in a GameObjectGrid class, which is a two-dimensional array that saves the current level. This can then be accessed in the Player class and compared with the player’s current position.  When the player is currently positioned at a block, it stops falling. All of this combined, it looks like this:



And that’s all I have to say about that.

Shiny Spice, out.

1 comment: