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).