Intro to Game Dev -Understanding the “GAME LOOP”

M.R.M Abdullah
4 min readMar 20, 2023

If you are a game developer, you are likely familiar with the concept of the game loop. But if you are new to game development, or if you are interested in learning more about how games work, it is important to understand what the game loop is and how it functions.

At a high level, the game loop is a continuous process that controls the flow of a game program. It is responsible for updating the game state, processing user input, and generating outputs such as graphics and audio. In other words, it is the backbone of any game program.

When we play a video game, we interact with a type of program that is quite different from other computer programs. One of the biggest differences is that video games require frequent updates to keep up with the user’s input and provide a seamless experience. This means that a game must update many times per second for as long as it runs.

To ensure this high update rate, video games use something called a game loop. The game loop is a repeating set of instructions that control the flow of the game from start to finish. Like any other loop in programming, the game loop has code that executes on every iteration, as well as a loop condition that determines when to stop.

In the case of a game loop, the loop continues running as long as the player has not quit the game program. Each time the loop runs, it is known as an iteration, and in the context of video games, we refer to each iteration as a frame.

The number of times the game loop runs per second is known as the frame rate, often abbreviated as FPS. For example, if a game runs at 60 frames per second, this means that the game loop completes 60 iterations every second. Many real-time video games run at either 30 or 60 FPS, and by running the game loop this many times per second, the game gives the illusion of continuous motion, even though it’s only updating at periodic intervals.

It’s worth noting that the terms “frame rate” and “frames per second” are interchangeable. Therefore, a frame rate of 60 means the same thing as 60 FPS. By understanding the basics of game loops and frame rates, we can appreciate the intricacies of video game development and how these concepts contribute to the overall gaming experience.

In order to understand how a game loop works, it is helpful to break down the process of a game loop into its individual steps. At a high level, a game goes through three main steps on each frame: processing inputs, updating the game world, and generating outputs.

Processing inputs involves detecting any inputs from devices such as a keyboard, mouse, or controller. However, depending on the type of game and the platform it runs on, there may be other inputs to consider, such as data received over the Internet in an online multiplayer game or GPS information in a mobile game.

Updating the game world means going through every object in the game world and updating it as needed. This includes characters in the game world, parts of the user interface, and other objects that affect the game, even if they are not visible.

Generating outputs involves producing graphics, audio, and other effects based on the state of the game world. This could include force feedback effects on a console controller or data sent to other players in an online multiplayer game.

To illustrate how these steps work in practice, we can consider a simplified version of the classic arcade game Pac-Man. In this simplified version of the game, Pac-Man is immediately placed in a maze and the game continues until he either completes the maze or dies.

In this case, the process inputs step only needs to read in the joystick input. The update game world step then updates Pac-Man based on the joystick input, as well as the four ghosts, pellets, and the user interface. This update code must determine whether Pac-Man runs into any ghosts, and whether he eats any pellets or fruits. The ghosts, which are fully AI controlled, must also update their logic. Finally, the user interface may need to update what data it displays based on Pac-Man’s actions.

This type of game loop is single-threaded, which means it does not take use of current CPUs, which can operate several threads at the same time. Creating a game loop that supports several threads is difficult and unnecessary for smaller-scale games.

In the classic version of Pac-Man, the only outputs that are generated in the “generate outputs” phase are audio and video. Here’s a pseudocode representation of what the game loop might look like for this simplified version of Pac-Man:

void Game::RunLoop() {
while (!mShouldQuit) {

// Process Inputs
JoystickData j = GetJoystickData();
// Update Game World
UpdatePlayerPosition(j);
for (Ghost & g: mGhost) {
if (g.Collides(player)) {
// Handle Pac-Man colliding with a ghost
} else {
g.Update();
}
}

// Handle Pac-Man eating pellets
// ...


// Generate Outputs
RenderGraphics();
RenderAudio();
}
}

In the next episode of this article series, we will dive deeper into game programming concepts . Thank you for reading, and see you in the next episode!

--

--

M.R.M Abdullah

Software Engineering undergraduate at University of Kelaniya.