|  | Home · Overviews · Examples

# Qt Tutorial 13 - Game Over

In this example we start to approach a real playable game with a score. We give `MyWidget` a new name (`GameBoard`) and add some slots.

We put the definition in `gameboard.h` and the implementation in `gameboard.cpp`.

The `CannonField` now has a game over state.

The layout problems in `LCDRange` are fixed.

## Line by Line Walkthrough

### t13/lcdrange.cpp

t13/lcdrange.cpp Missing snippet: tutorials/tutorial/t13/lcdrange.cpp. We set the size policy of the QLabel to (Preferred, Fixed). The vertical component ensures that the label won't stretch or shrink vertically; it will stay at its optimal size (its sizeHint()). This solves the layout problems observed in Chapter 12.

### t13/cannonfield.h

t13/cannonfield.h The `CannonField` now has a game over state and a few new functions. Missing snippet: tutorials/tutorial/t13/cannonfield.h. This function returns `true` if the game is over, `false` if a game is going on. Missing snippet: tutorials/tutorial/t13/cannonfield.h. Here are two new slots: `setGameOver()` and `restartGame()`. Missing snippet: tutorials/tutorial/t13/cannonfield.h. This new signal indicates that the `CannonField` is in a state where the `shoot()` slot makes sense. We'll use it below to enable or disable the Shoot button. Missing snippet: tutorials/tutorial/t13/cannonfield.h. This private variable contains the game state; `true` means that the game is over, and `false` means that a game is going on.

### t13/cannonfield.cpp

t13/cannonfield.cpp Missing snippet: tutorials/tutorial/t13/cannonfield.cpp. This line has been added to the constructor. Initially, the game is not over (luckily for the player :-). Missing snippet: tutorials/tutorial/t13/cannonfield.cpp. We added a new `isShooting()` function, so `shoot()` uses it instead of testing directly. Also, shoot tells the world that the `CannonField` cannot shoot now. Missing snippet: tutorials/tutorial/t13/cannonfield.cpp. This slot ends the game. It must be called from outside `CannonField`, because this widget does not know when to end the game. This is an important design principle in component programming. We choose to make the component as flexible as possible to make it usable with different rules (for example, a multi-player version of this in which the first player to hit ten times wins could use the `CannonField` unchanged).

If the game has already been ended we return immediately. If a game is going on we stop the shot, set the game over flag, and repaint the entire widget. Missing snippet: tutorials/tutorial/t13/cannonfield.cpp. This slot starts a new game. If a shot is in the air, we stop shooting. We then reset the `gameEnded` variable and repaint the widget.

`moveShot()` too emits the new `canShoot(true)` signal at the same time as either `hit()` or `miss()`.

Modifications in CannonField::paintEvent(): Missing snippet: tutorials/tutorial/t13/cannonfield.cpp. The paint event has been enhanced to display the text "Game Over" if the game is over, i.e., `gameEnded` is `true`. We don't bother to check the update rectangle here because speed is not critical when the game is over.

To draw the text we first set a black pen; the pen color is used when drawing text. Next we choose a 48 point bold font from the Courier family. Finally we draw the text centered in the widget's rectangle. Unfortunately, on some systems (especially X servers with Unicode fonts) it can take a while to load such a large font. Because Qt caches fonts, you will notice this only the first time the font is used. Missing snippet: tutorials/tutorial/t13/cannonfield.cpp. We draw the shot only when shooting and the target only when playing (that is, when the game is not ended).

### t13/gameboard.h

t13/gameboard.h This file is new. It contains the definition of the `GameBoard` class, which was last seen as `MyWidget`. Missing snippet: tutorials/tutorial/t13/gameboard.h. We have now added four slots. These are protected and are used internally. We have also added two QLCDNumbers (`hits` and `shotsLeft`) that display the game status.

### t13/gameboard.cpp

t13/gameboard.cpp This file is new. It contains the implementation of the `GameBoard` class, which was last seen as `MyWidget`.

We have made some changes in the `GameBoard` constructor. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. `cannonField` is now a member variable, so we carefully change the constructor to use it. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. This time we want to do something when the shot has hit or missed the target. Thus we connect the `hit()` and `missed()` signals of the `CannonField` to two protected slots with the same names in this class. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. Previously we connected the Shoot button's `clicked()` signal directly to the `CannonField`'s `shoot()` slot. This time we want to keep track of the number of shots fired, so we connect it to a protected slot in this class instead.

Notice how easy it is to change the behavior of a program when you are working with self-contained components. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. We also use the `cannonField`'s `canShoot()` signal to enable or disable the Shoot button appropriately. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. We create, set up, and connect the New Game button as we have done with the other buttons. Clicking this button will activate the `newGame()` slot in this widget. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. We create four new widgets. Note that we don't bother to keep the pointers to the QLabel widgets in the `GameBoard` class because there's nothing much we want to do with them. Qt will delete them when the `GameBoard` widget is destroyed, and the layout classes will resize them appropriately. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. The top-right cell of the QGridLayout is starting to get crowded. We put a stretch just to the left of the New Game button to ensure that this button will always appear on the right side of the window. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. We're all done constructing the `GameBoard`, so we start it all using `newGame()`. Although `newGame()` is a slot, it can also be used as an ordinary function. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. This function fires a shot. If the game is over or if there is a shot in the air, we return immediately. We decrement the number of shots left and tell the cannon to shoot. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. This slot is activated when a shot has hit the target. We increment the number of hits. If there are no shots left, the game is over. Otherwise, we make the `CannonField` generate a new target. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. This slot is activated when a shot has missed the target. If there are no shots left, the game is over. Missing snippet: tutorials/tutorial/t13/gameboard.cpp. This slot is activated when the user clicks the New Game button. It is also called from the constructor. First it sets the number of shots to 15. Note that this is the only place in the program where we set the number of shots. Change it to whatever you like to change the game rules. Next we reset the number of hits, restart the game, and generate a new target.

### t13/main.cpp

t13/main.cpp This file has just been on a diet. `MyWidget` is gone, and the only thing left is the `main()` function, unchanged except for the name change.

## Running the Application

The cannon can shoot at a target; a new target is automatically created when one has been hit.

Hits and shots left are displayed and the program keeps track of them. The game can end, and there's a button to start a new game.

## Exercises

Add a random wind factor and show it to the user.

Make some splatter effects when the shot hits the target.

Implement multiple targets.