How Do Video Games Work?
Their Basic Architecture
Guides
Recommended
How to Play
How do video games work under the hood? What is their basic architecture?
Well, let’s explain how it all works. In every game, from Tetris to Final Fantasy, the basic architecture is the same: they're built around a game loop. A game loop is a piece of code that runs on a loop 30 to 60 times per second and tells the hardware what to draw on the screen. Almost all games are structured in this way.
Frames Per Second (fps)
The human eye and brain perceives images as separate if fewer than 10 or 12 are shown in sequence on the screen per second. If you start showing the images at a faster rate, the brain no longer perceives individual images but instead starts to perceive them as a continuous animation. For example, movies appear as continuous motion on the screen by having images shown at 24 images, or frames, per second (fps). This is known as the motion picture phenomenon.
A game only needs to update the screen 12 times per second to trick the brain into seeing continuous motion, but it's become standard to use 30 or even 60 fps. At 30 fps, the image on the screen is updated every 0.0333 seconds. At 60 fps, it is updated every 0.01666 seconds.
The higher the frame rate, the smoother animations appear and the more thoroughly the brain is convinced by them.
The Game Loop: From Code to Screen
So, now we have a general sense that games are similar to movies in that they display lots of pictures per second to provide a sense of motion or animation. But what's the role of the game code and the game loop?
Well, somewhere in the game code, there’s a function that looks like this:
function update()
handle_input() // keyboard, gamepad etc
update_world() // ai, physics, hit detection etc
render() // draw to the screen
end
This is the game loop. This update function gets called over and over again in quick succession while the game is being played. When this function runs, it updates the picture (frame) on the screen. The game calls this loop 30 or 60 times a second, depending on the fps of the game.
Note, however, that games are more than just graphics; they are interactive, too. That is why the first method call in the game loop is used to detect player input from things like a keyboard and mouse, a game controller, or a touch screen. The second method then updates the world based on this user input, and it also updates based on game AI, motion physics, collision detection, and more!
In a Nutshell
The game loop lies at the center of every game you've ever played. It is like the beating heart driving the game forward. The game loop function, which:
- Takes input from the player,
- Updates the world, and
- Redraws the screen,
is programmed to run 30 times (30 fps) or 60 times (60 fps) per second, inducing the motion picture phenomena. Any faster will put too much load on the system, and any slower will start to make the motion and animation look choppy.
Takeaways
We hope you now have a beginner’s understanding of the basic structure of video games and the role of the game loop. This should provide a framework into which you can fit future knowledge about the structure and execution of games if you want to dive deeper into the world of game design.