Introduction #
In this tutorial we will learn some of the fundamental concepts of computer video game programming.
Concepts #
There are a few things to learn about before diving into writing our first line of code:
Flag #
A flag represents a quality or condition of something. For example, a flag could represent whether a game has started yet, or whether a 2nd player has joined a game.
Flags are typically binary, meaning they are either false (representing the inactive state) or true (representing the active state). Boolean variables are typically used to implement flags.
Game State #
The game state can be thought of as a snapshot of all of the elements of a game at a particular moment in time. Some examples of possible elements are the coordinates of the player on the screen, the coordinates of an enemy and how many coins have been inserted. Games will typically read user input and then modify the game state based on which buttons are (or aren’t) pressed.
For example, when a player presses a button responsible for jumping, the press is detected and handled in the next game loop iteration (snapshot). When the game loop sees the jump button is pressed, a “jumping” flag could be set to true on a character, and the character would start their ascent.
When a player releases the button responsible for jumping, on the next game loop iteration the release of the button is detected and then handled. The “jumping” flag could be set to false when the button is released, and in some games, this would affect how high the character jumps.
The “jumping” flag and the current position and velocity of the player character are all examples of game state.
Game Loop #
Most games have a game loop which runs many times per second. A typical game loop might:
- Read user input.
- Then process and update the game state.
- Then draw the game on the screen.
Each time the loop completes, it returns to the first statement, and starts again.
Games will sometimes limit the number of times this happens per second, to reduce the amount of system resources used. This can be useful as above a certain number of updates/frames per second, adding more updates/frames makes no perceivable difference.
Input Devices #
What kinds of input devices do people use to play games?
- Keyboard
- Mouse
- Touch screen
- Gamepads (including steering wheels and joysticks)
When creating a game, it’s important to consider which types of input you want to support. If you intend to release your game on mobile devices, supporting touch screen input will likely be a necessity. If you intend to release on multiple platforms, like desktop, web and mobile, you will need to support multiple types of user input.
Drawing #
While we all learned about X/Y coordinates in school, we were taught that positive Y
values are ascending, and negative Y values are descending. We also usually see 0,0
as being placed in the center.
When it comes to computer screens, the Y direction is actually flipped, and 0,0
is
the top leftmost pixel of the screen. 0,1
is thus the pixel directly below 0,0
.
On a screen that is 1920x1080
(1920 pixels wide, 1080 pixels high), 0,1079
is the
bottom leftmost pixel, because the coordinates start at zero. 1919,0
is the top
rightmost pixel, and 1919,1079
is the bottom rightmost pixel.
Click here to proceed to the next tutorial, Getting Started with Go.
Please consider donating if you found this tutorial helpful.