NOTE: This tutorial is geared more towards the New Game Developers or experienced programmers who want to make there own movement systems.
The Game Loop:The game loop is the process that almost every game is based around.
Initializethis is the first part of the loop it initializes all the variables
UpdateThis part of the loop updates the variables that you may have declared or Initialized it is also where all the mouse and keyboard handlers are
RenderThis Part of the loop is where all the textures are handed to the rasterizer and then drawn on your screen.
After Render is finished it usually calls the update function again. This process is similar to recursion, you can see what the loop looks like below:
Velocity:Velocity is essentially the speed in any given direction e.g. in a game if your Horizontal Velocity (X Velocity) is 15. you are going 15 units to the Right on every update.
Sudo Code:
XVelocity = 0
[UPDATE] this is the update function that is called every frame
if( the Right Arrow Key is down)
{
XVelocity = XVelocity + 1;
}
if( the Left Arrow Key is down)
{
XVelocity = XVelocity - 1;
}
your character's x position = your character's x position + XVelocity
[END UPDATE]
The Code Above will make your character slide back and forth on the screen, the visual effect created is similar to ice in a side-scroller
Gravity:you may be asking "Why are you doing Velocity And Gravity?". Well this is because Simple Gravity is achieved with Vertical Velocity, and its quite simple.
In the shortest Explanation: if the character does not collide with anything then you want his Vertical Velocity to change.
Sudo Code:
YVelocity = 0
[UPDATE] this is the update function that is called every frame
if(your character is not touching any other object)
{
YVelocity = YVelocity - 1
}
your character's y position = your character's y position + YVelocity
[END UPDATE]
Thats it, now you have Gravity in your game.
you can even make your player jump by adding another if statement:
if(you press the jump button)
{
YVelocity = 15
}
this changes your character velocity to a positive value, making him jump and come back down.
I Hope Everyone Liked this Topic , feel free to comment and message me for ideas for future tutorials.
Thanks,
indieGivaway
http://hiarcgames.com/