Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Tetris in less than 128 lines of code

0.00/5 (No votes)
6 Jul 2004 1  
A cool tutorial on game programming using the CImg Library

Introduction

I propose here a version of the Tetris game that is less than 128 lines of code. This is possible thanks to the use of the CImg Library, which is a portable and easy to use image processing library, that is used in our case to display animated graphics in a window.

The code is easy to read : this is not a bunch of unreadable code lines, and the game is fully functional. I think it can be interesting for beginners who don't want to bother with how to display images or catch keyboard events. This is also a good overview of what one can do with the CImg Library.

Note that the code is also compiling on Unix OS, without changing a single instruction.

Using the code

Detailed comments can be found in the source archive. Actually, the code is very simple to understand. It is provided as a Visual Studio project, with two files : CImg.h (the source file of the CImg Library which does the hard work), and tetris.cpp (the source of the game, less than 128 lines of code). The project is basically a console-based project. If you are using another compiler, you will have to link the code with the standard C++ libraries : user32.lib and gdi32.lib in order to make it working.

Feel free to add features or embedding it with a nice interface (MFC). This is just a two hours project and my first submission to CodeProject, so be indulgent :)

How to play

Just use the arrows to go left, right and down, and rotate the object (up key).

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here