Click here to Skip to main content
13,559,658 members
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 23 Jul 2007
Licenced CPOL

A Clone of the Jawbreaker Game in Less than 128 Lines of Code

, 23 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Small game programming tutorial using the CImg Library
Screenshot - jawbreaker.jpg


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.


  • 2007/07/24 : Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

France France
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralNeed some help understanding a section of the code Pin
NikhilFreak13-May-11 22:53
memberNikhilFreak13-May-11 22:53 
GeneralI can do as good as you do! Pin
virtuPIC21-Aug-07 1:38
membervirtuPIC21-Aug-07 1:38 
GeneralSpectacular but... Pin
lallous13-Aug-07 21:54
memberlallous13-Aug-07 21:54 
QuestionYa Rly? Pin
krischan1112-Aug-07 10:27
memberkrischan1112-Aug-07 10:27 
AnswerRe: Ya Rly? Pin
Ronounours11-Aug-07 8:41
memberRonounours11-Aug-07 8:41 
Generalalmost functional approach Pin
araud30-Jul-07 23:33
memberaraud30-Jul-07 23:33 
GeneralCool Pin
joebeam24-Jul-07 3:53
memberjoebeam24-Jul-07 3:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.180527.1 | Last Updated 24 Jul 2007
Article Copyright 2007 by Ronounours
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid