Reader small image

You're reading from  C++ Windows Programming

Product typeBook
Published inSep 2016
Reading LevelIntermediate
PublisherPackt
ISBN-139781786464224
Edition1st Edition
Languages
Right arrow
Author (1)
Stefan Björnander
Stefan Björnander
author image
Stefan Björnander

Stefan Björnander is the author of the books Microsoft Windows C++ and C++ Windows Programming. He holds a Master of Engineering and a Licentiate in Computer Science. He has worked as a software developer and as a teacher in computer science and mathematics for many years.
Read more about Stefan Björnander

Right arrow

Chapter 3. Building a Tetris Application

In this chapter, we develop a classic Tetris game. We look further into the Window class, including text writing and drawing figures that are more complex. We look also into timing, random numbers, and graphical updates such as falling figures and flash effects. An illustration of it is shown next:

The MainWindow function


The MainWindow function is similar to the methods in Chapter 2, Hello, Small World!. It sets the application name and returns a pointer to the main window, which, in this case, is an instance of the TetrisWindow class. As stated in Chapter 2, Hello, Small World! the application name is used when accessing the registry, when opening or saving a file, and by the About menu item. However, none of that functionality is used in this application:

MainWindow.cpp

#include "..\\SmallWindows\\SmallWindows.h" 
#include "GameGrid.h" 
#include "TetrisFigure.h" 
#include "RedFigure.h" 
#include "BrownFigure.h" 
#include "TurquoiseFigure.h" 
#include "GreenFigure.h" 
#include "YellowFigure.h" 
#include "BlueFigure.h" 
#include "PurpleFigure.h" 
#include "TetrisWindow.h" 
 
void MainWindow(vector<String> /* argumentList */, 
                WindowShow windowShow) { 
  Application::ApplicationName() = TEXT...

The Tetris window


In this application, we do not use the StandardDocument framework from the Chapter 2, Hello, Small World!. Instead, the TetrisWindow class extends the Small Windows root class Window directly. The reason is simply that we do not need the functionality of the StandardDocument framework or its base class Document. We do not use menus or accelerators, and we do not save or load files:

TetrisWindow.h

class TetrisWindow : public Window { 
  public: 
    TetrisWindow(WindowShow windowShow); 
    ~TetrisWindow(); 

In this application, we ignore the mouse. Instead, we look into keyboard handling. The OnKeyDown method is called when the user presses or releases a key:

    bool OnKeyDown(WORD key, bool shiftPressed, 
                   bool controlPressed); 

Similar to the circle application, the OnDraw method is called every time the window's client area needs to be redrawn:

    void OnDraw(Graphics& graphics, DrawMode drawMode) const; 

The OnGainFocus...

The TetrisFigure class


In this application, there is the root figure class and one subclass for each type of falling figure. All figures can be moved sideways or rotated as a response to the user's requests. They are also moved downward by the timer.

There are seven figures, one for each color: red, brown, turquoise, green, yellow, blue, and purple. Each of them also has a unique shape. However, they all contain four squares. They can further be divided into three groups based on their ability to rotate. The red figure is the simplest one. It is a square and does not rotate at all. The brown, turquoise, and green figure can be rotated in vertical and horizontal directions, while the yellow, blue, and purple figures can be rotated in north, east, south, and west directions. For the red figure, it does not really matter since it does not rotate.

The row and col fields of the TetrisFigure class hold the center of the figure, which is marked by a cross in the illustrations of this section. The...

The GameGrid class


Finally, the GameGrid class is quite simple. It keeps track of the squares on the game board. The gridArea field is the portion of the total client area that is occupied by the grid:

GameGrid.h

const int Rows = 20, Cols = 10;  
 
class GameGrid { 
  public: 
    GameGrid(Rect gridArea); 
    void ClearGameGrid(); 
 
    Color* operator[](int row) {return gameGrid[row];} 
    void InvalidateSquare(Window* windowPtr, int row, 
                          int col, Size offsetSize); 
    void DrawGameGrid(Graphics& graphics, bool inverse) const; 



    void DrawSquare(Graphics& graphics, int row, int col, 
                    Color penColor, Color brushColor, 
                    Size offsetSize = ZeroSize) const; 
 
    Rect GridArea() const {return gridArea;} 
 
  private: 
    Rect gridArea; 
    Color gameGrid[Rows][Cols]; 
}; 

When called by the...

Summary


In this chapter, we developed a Tetris game. You looked into timing and randomization, as well as a new coordinate system, more advanced drawing, how to catch keyboard events, and how to write text.

In Chapter 4, Working with Shapes and Figures, we will develop a drawing program capable of drawing lines, arrows, rectangles, and ellipses.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
C++ Windows Programming
Published in: Sep 2016Publisher: PacktISBN-13: 9781786464224
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Stefan Björnander

Stefan Björnander is the author of the books Microsoft Windows C++ and C++ Windows Programming. He holds a Master of Engineering and a Licentiate in Computer Science. He has worked as a software developer and as a teacher in computer science and mathematics for many years.
Read more about Stefan Björnander