Click here to Skip to main content
15,861,168 members
Articles / Desktop Programming / MFC

A C++ Breakout Game Clone in Less than 64 Lines

Rate me:
Please Sign up or sign in to vote.
3.94/5 (20 votes)
23 Feb 2007GPL32 min read 88.6K   2.6K   31   18
I propose a simple version of a Breakout (or Arkanoid) game in few C++ lines.
Screenshot - CImgBreakout.jpg

Introduction

I propose here a cross-platform C++ version of the well known Breakout game (or Arkanoid if you prefer). The interesting thing here is that it is coded in less than 64 lines of C++ code. While the code is quite compact, it stays easily readable. The secret ? I use the CImg Library to perform all necessary operations on images. The CImg Library is an easy-to-use C++ template image processing library that has the particularity to entirely fit in one single header file. It means that with a single include, you are able to deal with images and perform many operations on it (display, filtering, drawing, ...).

This work follows my previous submission on "Tetris in less than 128 lines of code", already proposed on CodeProject.

Explanation

The code is divided into two distinct parts. The first one creates games graphics (colored bricks, ball, racket and background image) from scratch, using the primitive drawing functions of the CImg Library.

The second part consists of the main event loop, catching the user's events and computing the ball/racket motion and the possible collisions. In fact, there's nothing specially difficult here, but all is coded in quite a compact way.

This is a good way to see how simple it is to code small animations using the CImg Library. It is also worth noticing that the source is cross-platform and you may compile it on Unix or Mac OS X without modifying a single line. I hope this may give some idea for people that are discouraged because of the relative code complexity needed when one wants to do basic image operations inside C++ source codes.

Proposed Archive

The proposed archive contains the small source code of the game (file 'breakout.cpp'), the CImg Library header file 'CImg.h' needed for the compilation, as well as the compiled executable (Win32) and the Visual Express 2005 solution used to compile. So, the archive should be self-contained.

Hope you'll enjoy it.

History

  • 23rd February, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


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

 
GeneralMy vote of 2 Pin
Destiny77712-May-13 14:59
Destiny77712-May-13 14:59 
SuggestionObviously the code works... But... Pin
Destiny77712-May-13 14:56
Destiny77712-May-13 14:56 
QuestionAny way to get ride of empty console window on this? Pin
Fremont Knight13-May-08 9:26
Fremont Knight13-May-08 9:26 
GeneralRe: Any way to get ride of empty console window on this? Pin
Member 803626723-Nov-11 4:01
Member 803626723-Nov-11 4:01 
QuestionUnder 64 lines ? I beg to differ. Pin
L.Denninger13-May-07 6:29
L.Denninger13-May-07 6:29 
AnswerRe: Under 64 lines ? I beg to differ. Pin
NevetsRetxab7-Jul-07 18:40
NevetsRetxab7-Jul-07 18:40 
GeneralProduction level Pin
Bjornar1-Mar-07 1:41
Bjornar1-Mar-07 1:41 
GeneralRe: Production level Pin
MarkusS4-Mar-07 6:34
MarkusS4-Mar-07 6:34 
GeneralRe: Production level Pin
Ronounours11-Mar-07 12:39
Ronounours11-Mar-07 12:39 
GeneralRe: Production level Pin
CoreyCooper5-Jun-07 11:14
CoreyCooper5-Jun-07 11:14 
GeneralRe: Production level Pin
cosmiKcosmiK11-Jan-09 16:26
cosmiKcosmiK11-Jan-09 16:26 
GeneralRe: Production level Pin
xMYx4-Feb-10 2:50
xMYx4-Feb-10 2:50 
GeneralRe: Production level Pin
xComaWhitex8-Nov-10 19:42
xComaWhitex8-Nov-10 19:42 
GeneralAwesome! Pin
Ravi Bhavnani24-Feb-07 5:56
professionalRavi Bhavnani24-Feb-07 5:56 
GeneralRe: Awesome! Pin
PJ Arends24-Feb-07 10:07
professionalPJ Arends24-Feb-07 10:07 
GeneralRe: Awesome! Pin
Ronounours24-Feb-07 10:54
Ronounours24-Feb-07 10:54 
GeneralRe: Awesome! Pin
PJ Arends24-Feb-07 11:17
professionalPJ Arends24-Feb-07 11:17 
GeneralRe: Awesome! Pin
OscarTV26-Feb-07 21:02
OscarTV26-Feb-07 21:02 

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.