Game Programming basics – Time

One of the most important lessons I learned as a young game maker was how to harness Time for your games. Before that me and my friends had been mostly doing simple QBASIC text adventures or other games where the game logic was:
[ show text/graphic -> wait for player input -> run through a if/switch clause to determine next step -> show text/graphic… ]
Around this time I also once created a new project in QBASIC and created a new Subroutine called Football and tried to run it hoping for some bare-bones sports game to appear. Starting programming before having the internet or almost any English skills was definitely interesting and very rewarding since every little discovery felt like a huge Eureka! moment.

For game development the single most important concept in my opinion is time. Without time you are extremely constrained in what you can do. How time works in games at it’s core is you have a game loop that gets called 30-60 times a second. In the game loop you can give life to everything that’s inside the game. In this pseudo-code example a spaceship flies from left to right (along the x-axis). Once you have that tool, it’s easy to build upon it to create player controls, enemy AI, graphical effects, pretty much anything in a game.

1
2
3
4
function gameLoop()
{
  spaceship.x++;
}
function gameLoop()
{
  spaceship.x++;
}

If you run this code once, the ship will simply move one pixel to the right. If you run it continuously, the ship gains a life of it’s own and will fly further right “forever”. As a beginning young programmer this really blew my mind when I saw it happen on my screen.

Everything that happens in a game looks smooth and real-time but it is actually just a second that can be sliced into 60 pieces of incremental action.

  • http://simonewebdesign.it/ Simone

    Hey Lauri, thanks for this. You just inspired me.
    I’m web programmer but I never did game development yet; I’m just going to try building something awesome within a JavaScript loop. 😉