Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C++

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

Rate me:
Please Sign up or sign in to vote.
4.40/5 (9 votes)
23 Jul 2007CPOL2 min read 54.7K   1.4K   21   7
Small game programming tutorial using the CImg Library
Screenshot - jawbreaker.jpg

Introduction

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.

Background

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.

History

  • 2007/07/24 : Initial version

License

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


Written By
France France
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionYa Rly? Pin
krischan1112-Aug-07 10:27
krischan1112-Aug-07 10:27 
AnswerRe: Ya Rly? Pin
Ronounours11-Aug-07 8:41
Ronounours11-Aug-07 8:41 

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.