Tetris in C# .NET WPF
I programmed a classic game - Tetris, in C# .NET. I used WPF for graphical interface. By making this game I practiced inheritance, interfaces and threads usage, and working with collections and WPF.
A brief description of the solution
GUI
The game square grid is made of grid with 10 columns and 20 rows. Inside each grid cell, there is a Rectangle object that changes its color during play.
Objects (blocks)
Each type of block, consisting of maximum of 4 sub-blocks, has its own class. It inherits from the BaseShapeObject class, which implements moving to sides and contains the points collection. All classes implement the IObject interface, in which the Create method is defined, that creates a block (shape / position).
Game logic
During controlling of the movement of the block, the coordinates of the block points are checked for whether they're in the playing area, otherwise the movement isn't performed. Collisions and all motion methods are handled using the generic List functions.
E.g. move left. The condition is whether there is no point that is less than the origin of the X axis (zero). Another condition solves collision and checks whether there is no point in the new position. If not, all points of the block are moved.
if (!points.Exists(p => (p.X - 1) < 0)) { if (!listOfOtherObjects.Exists(lo => points.Exists(p => (p.X - 1) == lo.X && p.Y == lo.Y))) { points.ForEach(p => { p.X--; }); } }
There is a thread running in the background, that measures the block down movement time. The value starts at 1 second. It's reduced by 20% every round. The scoring is as follows (n is the current round):
Number of removed rows | Score |
---|---|
One | n * 40 +40 |
Two | n * 100 + 100 |
Three | n * 300 + 300 |
Four (tetris) | n * 1200 + 1200 |
I'll be happy to answer any questions or other solutions
Gallery
Download
By downloading the following file, you agree to the license terms
Downloaded 131x (1.46 MB)
Application includes source codes in language C# .NET