Jawbreaker Game (C++)



Here, I propose a C++ version of the Jawbreaker game that fits in less than 128 lines of C++ code. This is possible thanks to the use of the CImg Library, a small and very useful C++ library dedicated to image processing operations.
In our case, we just use some functions of the CImg Library to handle images, display animated graphics in a window, and retrieve user events (mouse and keyboard).
Note that CImg is multi-platform, so that the proposed jawbreaker code will compile on Linux, BSD or MacOSX computers.
The Jawbreaker game is a simple and very funny game featuring colored balls. Clicking on a set of (at least two) colored balls remove them. The bigger the set is, the bigger the score will be. This is a classical game found in Windows Mobile based PDA.


This article follows these two previous articles explaining how to code small games using the CImg Library:
These are not necessary, but the three articles together give a good view of how to proceed with CImg to create some small games.

Using the Code

The source code is quite small. The file jawbreaker.cpp has 156 lines, but the first code line starts at line 37, after the comments, so the code itself is contained in 120 lines. Of course, the coding style is quite compact, but not unreadable: There are comments and no code obfuscation.
How does it work ? First we start by creating game graphics from scratch, which means that no extra graphical data is needed. Basically for this game, we need to create colored balls sprites and a background.
Then, the code enters the main event loop that displays the game board using the balls created before, and handles user events (mouse and keyboard).
The main difficulty comes from the selection of colored balls, and the computation of a modified game board if necessary. But as you may notice, this is not such a huge step at all.

How to Play?

To control the game, just use the mouse: left click to select a set of connected balls, left click to confirm its suppression. Use key 'BACKSPACE' for an UNDO mode, and 'ESC' to end the game.



Post a Comment