Creating your own Tetris game with VB.NET - Part 3
Introduction
If you have been following this series up to this point, I know what you are thinking: When are we going to start with the physical shapes and game play? Well, wait no more :) In this part of my series, I am going to cover the whole creation of all the shapes, and the game playing methods. if you haven't yet read the previous 2 parts of this article series, I suggest going through them now, as this part is where everything happens!
Logic / Explanation
As mentioned in the first part, Tetris makes use of tetrominoes. A tetromino, also spelled tetramino or tetrimino, is a geometric shape composed of four squares, connected orthogonally. This is a particular type of polyomino, just like dominoes and pentominoes. Sometimes the term is generalized to apply to configurations of four orthogonally connected cubes. There are seven possible shapes :
| Shape | Picture |
| I | |
| O | |
| T | |
| L | |
| J | |
| S | |
| Z |
We are going to create these shapes now. We will also make each shape move and change, so that each shape behaves as it should, in any other Tetris game. With the help of pressing some keys during game play, the shapes must be flipped over, or turned, but more on that a little bit later, we should create the shapes first, so let us now start.
We do not need to change or add anything on our Form's design, so we can start with the code directly.
Coding
The first thing we have to do is to create our Shapes class. Add a new class to your project, and name it clsTetShapes
clsTetShapes
Add the following declarations to your class :
Public rctShape() As Rectangle 'Shape
Public blnMoving As Boolean 'Is The Shape Moving
Private blnIsNextShape As Boolean = False 'Image In The Next Shape Window
Private intStartX As Integer 'Starting Horz Loc
Private intCurrShape As Integer = 1 'Current Shape
Private intCurrShapePos As Integer = 1 'Shape Location
Private intBlockX(4) As Integer 'Each Part Of Tetri, Horz
Private intBlockY(4) As Integer 'Each part Of Tetri, Vert
Private intXPos(4) As Integer 'Starting Horz Loc
Private intPanelWidth As Integer 'Panel Width
Private intPanelHeight As Integer 'Panel height
These variables are used throughout the class. The width & height of the drawing area(s), the size of the shapes are all stored in the above variables
Add the constructor
Public Sub New(ByVal intShapeType As Integer, ByVal intScreenW As Integer, ByVal intScreenH As Integer, ByVal blnNextShape As Boolean)
intStartX = CInt((intScreenW - 1) / 2) 'Start In Center Of X Axis
intPanelWidth = intScreenW 'Canvas Width
intPanelHeight = intScreenH 'Canvas Height
blnIsNextShape = blnNextShape ' Is The Next Shape Shown
intCurrShape = intShapeType 'Set Current Shape
ShapeStart() 'Call To ShapeStart Gives Initial values To All Blocks Of All Shapes
End Sub
In the constructor, we obtain the width of our game area, get a random shape, and calls the ShapeStart sub procedure which basically creates our shapes
The next sub procedure, Build is used to build the four blocks of each shape, depending on which shape we are busy with, enter the following:
Private Sub Build(ByVal intShapeType As Integer)
rctShape = New Rectangle(4) {} 'Each Block Of Shape
'Create 4 Blocks 10 x 10
rctShape(0) = New Rectangle(intBlockX(0), intBlockY(0), 10, 10)
rctShape(1) = New Rectangle(intBlockX(1), intBlockY(1), 10, 10)
rctShape(2) = New Rectangle(intBlockX(2), intBlockY(2), 10, 10)
rctShape(3) = New Rectangle(intBlockX(3), intBlockY(3), 10, 10)
End Sub
We utilise the rctShape array, and give each of the four blocks a starting value

Comments
There are no comments yet. Be the first to comment!