The Game class

The Game class contains the looping function that enables you to move pieces around the board with key presses. Here's the contents of the header file (game.h):

#ifndef TETRIS_GAME_H
#define TETRIS_GAME_H

#include <SDL2/SDL.h>
#include <SDL2/SDL2_ttf.h>
#include "constants.h"
#include "board.h"
#include "piece.h"

class Game {
public:
Game();
~Game();
bool loop();

private:
Game(const Game &);
Game &operator=(const Game &);

void checkForCollision(const Piece &newPiece);
void handleKeyEvents(SDL_Event &event);

SDL_Window *window_;
SDL_Renderer *renderer_;
TTF_Font *font_;
Board board_;
Piece piece_;
uint32_t moveTime_;
};

#endif // TETRIS_GAME_H

The loop() function contains the game logic and manages state based on events. The first two lines under the private: header prevent more than one instance of the game from being created, which could cause a memory leak. The private methods reduce the amount of code lines in the loop() function, which simplifies maintenance and debugging. Let's move on to the implementation in game.cpp.

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

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