Winter sales2 C/C++ week
Get up to 60 % extra points for free! More info
Save up to 80 % on our C/C++ e-learning courses. Only this week!

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
You can pause the game at any time.

I'll be happy to answer any questions or other solutions :)


Gallery

Game was created in 2013.

 

Download

Downloaded 0x (1.46 MB)
Application includes source codes in language C# .NET

 

 

Game has been written for you by Petr Domes (petrds)
Avatar
Do you like this article?
No one has rated this quite yet, be the first one!
All articles in this section
C# .NET Community Projects - WPF Form Applications
Activities (2)

 

 

Comments

To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

No one has commented yet - be the first!