[2] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
[846] | 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
[561] | 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
[2] | 6 | **
|
---|
| 7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
| 8 | **
|
---|
[846] | 9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
[2] | 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
[846] | 13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
| 14 | ** written agreement between you and Nokia.
|
---|
[2] | 15 | **
|
---|
[846] | 16 | ** GNU Free Documentation License
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
| 18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
| 20 | ** file.
|
---|
[2] | 21 | **
|
---|
[561] | 22 | ** If you have questions regarding the use of this file, please contact
|
---|
| 23 | ** Nokia at [email protected].
|
---|
[2] | 24 | ** $QT_END_LICENSE$
|
---|
| 25 | **
|
---|
| 26 | ****************************************************************************/
|
---|
| 27 |
|
---|
| 28 | /*!
|
---|
| 29 | \example widgets/tetrix
|
---|
| 30 | \title Tetrix Example
|
---|
| 31 |
|
---|
| 32 | The Tetrix example is a Qt version of the classic Tetrix game.
|
---|
| 33 |
|
---|
| 34 | \image tetrix-example.png
|
---|
| 35 |
|
---|
| 36 | The object of the game is to stack pieces dropped from the top of the
|
---|
| 37 | playing area so that they fill entire rows at the bottom of the playing area.
|
---|
| 38 |
|
---|
| 39 | When a row is filled, all the blocks on that row are removed, the player earns
|
---|
| 40 | a number of points, and the pieces above are moved down to occupy that row.
|
---|
| 41 | If more than one row is filled, the blocks on each row are removed, and the
|
---|
| 42 | player earns extra points.
|
---|
| 43 |
|
---|
| 44 | The \gui{Left} cursor key moves the current piece one space to the left, the
|
---|
| 45 | \gui{Right} cursor key moves it one space to the right, the \gui{Up} cursor
|
---|
| 46 | key rotates the piece counter-clockwise by 90 degrees, and the \gui{Down}
|
---|
| 47 | cursor key rotates the piece clockwise by 90 degrees.
|
---|
| 48 |
|
---|
| 49 | To avoid waiting for a piece to fall to the bottom of the board, press \gui{D}
|
---|
| 50 | to immediately move the piece down by one row, or press the \gui{Space} key to
|
---|
| 51 | drop it as close to the bottom of the board as possible.
|
---|
| 52 |
|
---|
| 53 | This example shows how a simple game can be created using only three classes:
|
---|
| 54 |
|
---|
| 55 | \list
|
---|
| 56 | \o The \c TetrixWindow class is used to display the player's score, number of
|
---|
| 57 | lives, and information about the next piece to appear.
|
---|
| 58 | \o The \c TetrixBoard class contains the game logic, handles keyboard input, and
|
---|
| 59 | displays the pieces on the playing area.
|
---|
| 60 | \o The \c TetrixPiece class contains information about each piece.
|
---|
| 61 | \endlist
|
---|
| 62 |
|
---|
| 63 | In this approach, the \c TetrixBoard class is the most complex class, since it
|
---|
| 64 | handles the game logic and rendering. One benefit of this is that the
|
---|
| 65 | \c TetrixWindow and \c TetrixPiece classes are very simple and contain only a
|
---|
| 66 | minimum of code.
|
---|
| 67 |
|
---|
| 68 | \section1 TetrixWindow Class Definition
|
---|
| 69 |
|
---|
| 70 | The \c TetrixWindow class is used to display the game information and contains
|
---|
| 71 | the playing area:
|
---|
| 72 |
|
---|
| 73 | \snippet examples/widgets/tetrix/tetrixwindow.h 0
|
---|
| 74 |
|
---|
| 75 | We use private member variables for the board, various display widgets, and
|
---|
| 76 | buttons to allow the user to start a new game, pause the current game, and quit.
|
---|
| 77 |
|
---|
| 78 | Although the window inherits QWidget, the constructor does not provide an
|
---|
| 79 | argument to allow a parent widget to be specified. This is because the window
|
---|
| 80 | will always be used as a top-level widget.
|
---|
| 81 |
|
---|
| 82 | \section1 TetrixWindow Class Implementation
|
---|
| 83 |
|
---|
| 84 | The constructor sets up the user interface elements for the game:
|
---|
| 85 |
|
---|
| 86 | \snippet examples/widgets/tetrix/tetrixwindow.cpp 0
|
---|
| 87 |
|
---|
| 88 | We begin by constructing a \c TetrixBoard instance for the playing area and a
|
---|
| 89 | label that shows the next piece to be dropped into the playing area; the label
|
---|
| 90 | is initially empty.
|
---|
| 91 |
|
---|
| 92 | Three QLCDNumber objects are used to display the score, number of lives, and
|
---|
| 93 | lines removed. These initially show default values, and will be filled in
|
---|
| 94 | when a game begins:
|
---|
| 95 |
|
---|
| 96 | \snippet examples/widgets/tetrix/tetrixwindow.cpp 1
|
---|
| 97 |
|
---|
| 98 | Three buttons with shortcuts are constructed so that the user can start a
|
---|
| 99 | new game, pause the current game, and quit the application:
|
---|
| 100 |
|
---|
| 101 | \snippet examples/widgets/tetrix/tetrixwindow.cpp 2
|
---|
| 102 | \snippet examples/widgets/tetrix/tetrixwindow.cpp 3
|
---|
| 103 |
|
---|
| 104 | These buttons are configured so that they never receive the keyboard focus;
|
---|
| 105 | we want the keyboard focus to remain with the \c TetrixBoard instance so that
|
---|
| 106 | it receives all the keyboard events. Nonetheless, the buttons will still respond
|
---|
| 107 | to \key{Alt} key shortcuts.
|
---|
| 108 |
|
---|
| 109 | We connect \l{QAbstractButton::}{clicked()} signals from the \gui{Start}
|
---|
| 110 | and \gui{Pause} buttons to the board, and from the \gui{Quit} button to the
|
---|
| 111 | application's \l{QApplication::}{quit()} slot.
|
---|
| 112 |
|
---|
| 113 | \snippet examples/widgets/tetrix/tetrixwindow.cpp 4
|
---|
| 114 | \snippet examples/widgets/tetrix/tetrixwindow.cpp 5
|
---|
| 115 |
|
---|
| 116 | Signals from the board are also connected to the LCD widgets for the purpose of
|
---|
| 117 | updating the score, number of lives, and lines removed from the playing area.
|
---|
| 118 |
|
---|
| 119 | We place the label, LCD widgets, and the board into a QGridLayout
|
---|
| 120 | along with some labels that we create with the \c createLabel() convenience
|
---|
| 121 | function:
|
---|
| 122 |
|
---|
| 123 | \snippet examples/widgets/tetrix/tetrixwindow.cpp 6
|
---|
| 124 |
|
---|
| 125 | Finally, we set the grid layout on the widget, give the window a title, and
|
---|
| 126 | resize it to an appropriate size.
|
---|
| 127 |
|
---|
| 128 | The \c createLabel() convenience function simply creates a new label on the
|
---|
| 129 | heap, gives it an appropriate alignment, and returns it to the caller:
|
---|
| 130 |
|
---|
| 131 | \snippet examples/widgets/tetrix/tetrixwindow.cpp 7
|
---|
| 132 |
|
---|
| 133 | Since each label will be used in the widget's layout, it will become a child
|
---|
| 134 | of the \c TetrixWindow widget and, as a result, it will be deleted when the
|
---|
| 135 | window is deleted.
|
---|
| 136 |
|
---|
| 137 | \section1 TetrixPiece Class Definition
|
---|
| 138 |
|
---|
| 139 | The \c TetrixPiece class holds information about a piece in the game's
|
---|
| |
---|