The constants file

Rather than have confusing magic numbers sprinkled throughout the code base, I opted for a header file containing the constants we'll be using (constants.h). The contents of this file are shown here:

#ifndef TETRIS_CONSTANTS_H
#define TETRIS_CONSTANTS_H

namespace Constants {
const int BoardColumns = 10;
const int BoardHeight = 720;
const int BoardRows = 20;
const int BoardWidth = 360;
const int Offset = BoardWidth / BoardColumns;
const int PieceSize = 4;
const int ScreenHeight = BoardHeight + 50;
}

#endif // TETRIS_CONSTANTS_H

The #ifndef statement in the first line of the file is an #include guard, which prevents the header file from being included multiple times during compilation. These guards are used in all of the application's header files. The purpose of each of these constants will become clear when we step through each of the classes. I included it first to provide context around the various element sizes and how they relate to each other.

Let's move on to the various classes that represent aspects of the game. The Piece class represents an object at the lowest level, so we'll start there and work our way up to the Board and Game classes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset