Building a Tetris Game using VB.NET (part 1)

This weekend I felt like playing tetris game. So thought why not make my own game. To share the fun of creating the game, here is how I went about it. Since this would be a long one, I’ll post it in parts.

Note that since I haven’t referred this from any other book or online article etc., this may or may not be the best approach. It is all what came to my mind and I started with it.Feel free to comment about it if you think something could be done in a better way.

You can download the entire source code for this game from here. Feel free to use the source code whatever way you can or want to. I reserve no rights for myself.

If you are just interested in the executable (game) file, download it from here. If the game doesn’t run, it could be possible that .NET framework is not installed on your PC. You can download and install Microsoft .NET framework (version 3.5) from Microsoft’s official download page, if you don’t have it already.

[Warning: As with any other file, scan the downloaded files for viruses/malware. I may not be having the best antivirus in the world Wink ]

Basic Concepts

There are 7 different types of basic shape in our game. We name them I J L O S T Z for simplicity to match the nearest English alphabet they look like. Since the game has facility to rotate the shapes, this makes them 28 (i.e. 7×4 ) in total. See diagram below:

tetrisshapes

The image above shows the 7 basic shapes and the other variations of each one of them rotated clockwise.

Notice that all the shapes are made up of 4 cells. Also notice that except for I and O, all others can fit into a 3×3 grid. So we can take the 2×2 cell as the center and rotate the shape clockwise or anti-clockwise. I have marked the center cell with a small hole in the image above. Imagine that you can rotate the block around that fixed hole. I and O shapes are exceptions in the sense that we can’t determine a fixed center cell for them. The O-shape when rotated will remain the same, so we need not worry about it. We designate the 2×2 cell as its center. For the I-shape, we take the 2×2 cell as its center, since the next two rotations are insignificant. This makes the 2×2 cell as an ideal choice for the center for all shapes.

Now observe the I O S and Z shapes. We don’t need all of them. Some of them are duplicates. But still we keep them so that we get even falling blocks. e.g. If we keep just one O-shape, this would be least seen in the game. We don’t want this to happen. So we keep 4 for each type, even if it means duplicates.

Translating the concept into a program

To translate the concept into a program, we create three classes.

  1. TetrisBoard : The TetrisBoard class represents all the cells in the game. It is a 2 dimensional grid of cells, just like a spreadsheet. You may call it the canvas or playground for the game.
  2. TetrisCell : A TetrisCell represents each cell of the tetris board. So in effect anything in this game is ultimately made up of TetrisCells.
  3. TetrisBlock : A TetrisBlock represents a group of cells (TetrisCells) that form a meaningful block for us. Each of the shapes in the above diagram is a block.

The diagram below shows some examples of each of these class objects.

tetris-board-cells-blocks

Here’s a quick summary about the capabilities of our classes:

TetrisCell

A TetrisCell inherits from a label. Since a label has all what we want for this game, I just used it with a few additional properties I needed. However, a label isn’t designed for graphics and as a result may not be the best choice. (I plan to replace it with some more graphical object in the future. e.g. a RectangleShape etc.)

A TetrisCell knows its location (Row, Column) in the GameBoard. It knows what color it is (HighlightColor) and whether it is shown or hidden (Highlighted). It also knows whether it is empty or occupied (IsEmpty). Finally, it knows how to redraw itself when told to do so (Refresh method).

TetrisBoard

A TetrisBoard is nothing more than a collection (in fact 2D array) of TetrisCells arranged into rows and columns, with a few additional properties.

A TetrisBoard knows what container (Parent) it resides in, information about each TetrisCell it owns, their size, color and border style. It knows how many Rows and Columns it has.

It has methods to determine whether any of the rows it has is fully occupied or not (IsRowComplete method) and ways to remove any row when told to do so (RemoveRow method). When a row is removed, the rows above it fall down under gravity.

TetrisBlock

A TetrisBlock represents a group of TetrisCells on the board that forms a meaningful block. This is the heart of our game. Most of the part of the game resides in this class.

A TetrisBlock knows the cells that form the block (Cells property). It knows what shape it is (Shape property) and which TetrisBoard it is attached to (ParentBoard property).

A TetrisBlock has methods to know whether it is possible to move it to another location on the board (CanMove method) and also how to move itself if possible (Move method). It also has way to know whether it can rotate (CanRotate method) and rotate itself if it can (Rotate method). It knows how to paint itself when needed (Refresh and RefreshBackGround methods).

In part 2, we will see how we make use of these 3 classes in our game.

Advertisements

36 Responses to “Building a Tetris Game using VB.NET (part 1)”

  1. Pradeep Says:

    Oh man! more than 1000 views of this article and not a single comment from anyone. 😦

  2. branchenverzeichnis Says:

    Hi, for some reason when I place your feed into google reader, it won?t work. Can you give me the RSS link just to be sure I?m using the most appropriate one?

  3. Jack F Says:

    Great article. I can tell someone put a lot of effort into it.

    • pradeep1210 Says:

      Thanks Jack. 🙂

      Yes it required a lot of effort, but now it looks like it was worth it. From the daily hits statistics I see that it is the highest viewed post on my blog everyday. So I’ve now decided to write the next version of this game which will have more features without any increase in complexity as this one. 🙂

  4. kinjal Says:

    i need game in vb,net 2008 can you give me games with code ?
    i need it very much
    thanks a lot

  5. kinjal Says:

    my e.mail id is *****@yahoo.coin

    [Moderated: email address removed to avoid spams in your mailbox.]

  6. nash quinanola Says:

    plz send me…….tnx a lot…more power

  7. nash quinanola Says:

    i mean the code…..this is my email ***@yahoo.com…..thanks again…..

  8. ralph Says:

    hi can you send me the codes?..
    its for my proposal
    i must submit it with in this week thanx.. 😀

  9. Brandon Says:

    it is a good game , I hope you can share more games to us young programmers .

  10. Sumit Bhardwaj Says:

    Many views and less post. Bcoz article is superb and self explanatory. If some one read thoroughly not have question left. so one did not post after reading. Its really good start up

  11. Ahmed Says:

    Excellent article!

    I like how you explained every part of the program with a good amount of detail. You didn’t baby us by skimming over everything, and you didn’t use too much technical jargon.

  12. Josh Says:

    I don’t understand how you have programmed the TetrisCell to have its properties. Does it get its properties from the label? If so where is the label on the code and how does the label have a location and size property?

    • pradeep1210 Says:

      Since the TetrisCell inherits from a Label, it has all properties, methods and events a Label has. We just add a few more that we need for our game and we are done. It is a graphical object, so it can draw on a form. Normally a Label is used for displaying text. But here we are using its background and forecolor properties to display it as a block (without text). 🙂

      Just as I said in the article, a Label is not the best choice for graphics. So it would always be better to replace it with your own custom class, if possible.

  13. iluvmobilegames Says:

    nice. i love the game tetris ever since. i enjoyed playing it. you can check out some free games and software for your mobile phones, feel free to visit this site http://www.juegosenmovil.com/index_en.php

  14. Keke Says:

    Can I have the actual solution?
    Because I need the form so I can edit to tetris game my own way but I can’t because I can’t find the form.
    Thanks and the tetris game is awesome.

  15. jerson gevera Says:

    send me this game please,., include the codes and everything

  16. Kayla Says:

    me podrias ayudar con la linea de codigo…..cuando esta llena la fila se borre y bajen las fichas….xfa necesito de urgencia

  17. NeashRain ツ (@LOLOLShane) Says:

    How can I make the different types of basic shape? Pls answer ASAP. =))) Thank you. =)))

  18. hijek Says:

    can you provide me with the code for tetris in visual basic 6.0

    • pradeep1210 Says:

      While it won’t be possible for me to spare time to convert my code to VB6, if you go by the logic, you should be able to do it yourself very easily. There is no VB.NET specific code used in the project. So the conversion should be very easy for you. 🙂

  19. YJ Says:

    Hi… How do you add Sound to the game…

  20. Building a Tetris Game using VB.NET (part 2) | Pradeep1210's Blog Says:

    […] is a continuation of part 1 of the series. If you are looking for the source code and/or the executable file, you can find it […]

  21. JW Says:

    Well organized objects and code. It doesn’t get too clever or fancy for its own good. Parameters are easily modified in a single place to change behavior globally and add options. It is entirely capable of multiple instances without conflict – a sign of good code. Nice work.

  22. katerina Says:

    I am trying to open the source code but I cant. I have visual studio 2015

  23. Gaby Says:

    Thank you so muchhh!!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: