General structure of a game
Before starting to build randomly and without any specific planning, we need to have some information: what kind of game you want to build (RPG, FPS, or action-adventure), what elements will be used, and so on. The aim of this chapter is to understand the general game structure, which can be used in any kind of game. Through this part, we will study:
- The game class
- The frame rate
- The player class
- Event management
The game class
In the previous chapter, we have seen the minimal code required for a game, which contains:
- Window creation
- Creation of graphic display
- Handle user inputs
- Deal with the user inputs
- Display game objects on the screen
Instead of having one function do all the work, we will make use of object-oriented practices and define various states in different functions. Moreover, we will encapsulate the methods in a new class named Game
, and we will minimize the main
function. This Game
class will be the starting point for all our future games:
class Game { ...