Tetris game

Tetris

Capstone project for Udacity Nanodegree program CppND.Project implements the game Tetris

Gameplay

Player should place the falling figures in a way that the pile on the bottom has as less as possible empty cells. When line in the pile has no empty cells, it is deleted. Player can rotate figures, accelerate their fall. Player fails, when at least one figure exaggerates the game field on the top. For one deleted line player receives 10 scores, for two deleted at once lines – 40, three – 80, four – 160.

  • left arrow – move figure left
  • right arrow – move figure right
  • up arrow – rotate counter clock wise
  • up down – rotate clock wise
  • space – accelerate falling of figure
  • q or esc – leave game
Game field

Dependencies for Running Locally

Basic Build Instructions

  1. Clone this repo.
  2. Make a build directory in the top level directory: mkdir build && cd build
  3. Compile: cmake .. && make
  4. Run it: ./tetris.

Notes

Project description

Program is logically divided into three parts Model – View – Controller. Model stands for Gameplay. View represents graphically game. View knows quite nothing about game and receives commands from Controller. Controller implements interface between model (game) and view (SDL). In case view sdl is changed with another graphical library (QT eg) only few changes should be done in Controller.
Files and classes of Model:

  • model.cpp model.hpp (class Model) – main gameplay is done here. Creates and uses figures, creates and uses class Pile, provides points to draw to Controller. Knows nothing about graphic libraries.
  • figure.cpp figure.hpp (Abstract class Figure) – declare abstract class, which represents figure. Figure has forms. Form is the set of points with definite color. Figure has several forms, as it is rotated.
  • figure1.cpp, figure1.hpp – class Figure1 inherits abstract class Figure. Figure1 is a figure which is central-symmetrical and that’s why has only one form.
  • figure2.cpp, figure2.hpp – class Figure2 inherits abstract class Figure. Figure2 is a figure which is horizontally symmetric and has two forms.
  • figure4.cpp, figure4.hpp – class Figure4 inherits abstract class Figure. Figure4 is figure which has four different rotational forms.
  • pile.cpp, pile.hpp – class Pile implements Pile of figure on the bottom. Main functionality is to accept new figures and check if there are lines to be deleted.
    Files and class of Controller:
  • point.cpp point.hpp (class Point) – class Point implements 2D point with colors. Is used actively by Figure and Pile.
  • controller.hpp, controller.cpp (class Point) – class Controller uses methods of View and Model classes. It organizes Input-Update-Render loop, create object of classes View and Model. Class Controller processes commands from player.
    Files and class of View:
  • view.hpp, view.cpp – class View is graphical representation of the game. It create main game window, draw objects, which are provided by controller, process keyboard commands.

The following rubric points are addressed

  1. Class constructors utilize member initialization lists (All class members that are set to argument values are initialized through member initialization lists) – ./src/pile.cpp:8
  2. Derived class functions override virtual base class functions (One member function in an inherited class overrides a virtual base class member function) – src/Figure2.hpp:24, src/Figure2.cpp:10
  3. Classes use appropriate access specifiers for class members (All class data members are explicitly specified as public, protected, or private.) – src/Figure.hpp:11 and src/Figure1.hpp:10
  4. Classes encapsulate behavior (Appropriate data and functions are grouped into classes. Member data that is subject to an invariant is hidden from the user. State is accessed via member functions) – ./src/Model.hpp
  5. The project makes use of references in function declarations (At least two variables are defined as references, or two functions use pass-by-reference in the project code) ./src/model.hpp:26, ./src/figure.hpp:23

Class Description

Commands

 Class contains user commands.

Controller

 Class takes over interacting between game and its graphical representation.

Controller::Controller(size_t target_frame_duration, size_t screen_height)

 Constructor

[in] target_frame_duration duration of game loop [in] screen_height height of the window

Controller::Run()

 Game loop

Controller::Input()

 Get user command from keyboard.
 Key code

Controller::Update(Commands command)

 Update game state

[in] command command from user

Controller::Render()

 Draw the figure

[in] space point to draw

Model

 Class implements game model

Model::Model()

 Constructor

Model::GetOccupiedSpace()

 Receive points, which represent occupied by figure space
 returns points of occupied space

Model::UpdatePosition(Point &&point)

 Update position of figure

[in] point difference between new position and previous one

Model::GetTimeFall()

 Get fall time (s)

Model::RotateCounter()

 Rotate counter clock-wise figure

Model::Rotate()

 Rotate clock-wise figure

Model::Accelerate()

 Accelerate falling figure

Model::GetScore()

 Get player score
 returns player score

Model::IsGameOver()

 Check if game over
 returns true if game over has happened, otherwise returns false

Model::GetGameFieldWidth()

 Get game field width
 returns game field width

Model::GetGameFieldHeight()

 Get game field height
 returns game field height

Model::CheckBoundaries()

 Check if figure is in boundaries of game field

Model::UpdateFallingFigureSpace()

Model::FigureGenerator()

 Get new figure

Model::ComputeScore(unsigned num_del_lines)

 Compute score

[in] num_del_lines number of deleted lines

Pile

 Class implements bottom of the game field

Pile::Pile(int width, int height)

 Default constructor

[in] width width of game field [in] height height of game field

Pile::IsTouched(const Figure *figure)

 Checks if figure touches bottom

[in] figure figure to check if touches pile

 returns true if figure touches bottom

Pile::AddFigure(const Figure *figure)

 Add figure to pile

[in] space points of figure

 returns number of deleted lines

Pile::GetPile()

 Get points of pile
 returns points of pile

Pile::IsOverloaded()

 Get information on pipe overload
 returns true if pipe is overloaded, otherwise returns false

Pile::ClearLine()

 Search for line to clear and clear it
 returns number of deleted lines

Pile::ComputePilePointsSpace()

 Compute points of pile

Figure1

 Class Figure1 represents the game figure with rotation 1 state

Figure1::Figure1(Point &&p, Color c)

 Constructor

[in] p point initial position of the figure [in] c color of the figure

Figure1::RotateCounter()

 Rotate counter clock-wise

Figure1::Rotate()

 Rotate clock-wise

Figure1::make_square(Point &&p)

 Makes left-s figure

[in] p point initial position of figure

 returns Figure1 object, which is initialized as square

Figure2

 Class Figure2 represents the game figure with rotation 2 states

Figure2::Figure2(Point &&p, Color c)

 Constructor

[in] p point initial position of the figure [in] c color of the figure

Figure2::RotateCounter()

 Rotate counter clock-wise

Figure2::Rotate()

 Rotate clock-wise

Figure2::make_ls(Point &&p)

 Makes left-s figure

[in] p point initial position of figure

 returns Figure2 object, which is initialized as left-s figure

Figure2::make_rs(Point &&p)

 Make right-s figure

[in] p point initial position of figure

 returns Figure2 object, which is initialized as right-s figure

Figure2::make_stick(Point &&p)

 Make stick figure

[in] p point initial position of figure

 returns Figure2 object, which is initialized as stick figure

Figure4

 Class Figure4 represents the game figure with rotation 4 states

Figure4::Figure4(Point &&p, Color c)

 Constructor

[in] p point initial position of the figure [in] c color of the figure

Figure4::RotateCounter()

 Rotate counter clock-wise

Figure4::Rotate()

 Rotate clock-wise

Figure4::make_lhook(Point &&p)

 Makes left hook figure

[in] p point initial position of figure

 returns Figure4 object, which is initialized as left-hook figure

Figure4::make_rhook(Point &&p)

 Make right hook figure

[in] p point initial position of figure

 returns Figure4 object, which is initialized as right-hook figure

Figure4::make_t(Point &&p)

 Make T figure

[in] p point initial position of figure

 returns Figure4 object, which is initialized as T figure

View

 Provides graphical part of the game.

Color

 Contains figure color

Color::Point()

 Struct Point implements operations with points

Color::Point(int xx, int yy, Color color = Color::kBlack)

 Constructor

[in] xx x-coordinate [in] yy y-coordinate [in] cc Color

Color::GetRgba()

 Get tuple of rgba

View::View()

 Constructor

[in] screen_width width of the screen [in] screen_height height of the screen [in] cell_size size of quadratic cell

View::Render(const std::vector &x, const std::string &title)

 Draw the points

[in] x points to draw [in] title window title

View::GetHeight()

 Getter for height of the game field
 returns height of the game field

Description

Model-View-Controller realized on C++17. SDL library is used