Click here to Skip to main content
Click here to Skip to main content

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

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)

About the Author

Ronounours

France France
No Biography provided

Comments and Discussions

 
GeneralNeed some help understanding a section of the code PinmemberNikhilFreak13-May-11 22:53 
GeneralI can do as good as you do! PinmembervirtuPIC21-Aug-07 1:38 
GeneralSpectacular but... Pinmemberlallous13-Aug-07 21:54 
QuestionYa Rly? Pinmemberkrischan1112-Aug-07 10:27 
128 lines? Oh, you missed the 23000 in the header file.
 
End what the heck is #elif?
AnswerRe: Ya Rly? PinmemberRonounours11-Aug-07 8:41 
Generalalmost functional approach Pinmemberaraud30-Jul-07 23:33 
GeneralCool Pinmemberjoebeam24-Jul-07 3:53 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 24 Jul 2007
Article Copyright 2007 by Ronounours
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid