
Each piece has a position, and an array of positions that represent the position of each block. Method #2: Representing Block Positions as OffsetsĪ second approach would be to store all the information more explicitly. This also gives the CPU less work to do, but this comes at the expense of using more memory. We don’t have to worry about having to calculate the rotations for the blocks.

The main advantage of this method is that it’s simple. In this array we could have two values, 0 and 1 where 1 represents a block of the Tetris piece and 0 represents an area not to draw. The first option is that we could use a 2D array to represent block positions and rotations. Method #1: Hard Coding Rotation and Block Positions The Different Ways to Represent a Tetris PieceĪlthough we have a set requirement on the information we need to store for each Tetris piece, there are several ways in which we can store this information. We can then create 7 objects, each to represent a different shape. We also need to specify the character we want to be drawn, which will represent a block.įollowing object-orientated programming principles we can create a class that stores all of this information. What is a Tetris Piece?Ī Tetris piece contains four blocks, arranged in one of the seven allowed shapes: I, J, L, O, S, Z, and T.Īs well as the blocks themselves, the shape is also going to have a position to determine where in the screen buffer it gets drawn, and an angle that represents the rotation of the piece.
Tetris pieces code#
This time round we are going to continue on with the theme of drawing by looking how we can represent a Tetris piece in code and then draw it to the console.
Tetris pieces how to#
In part 2 we looked at how to draw to the console.

Hello, and welcome back to part 3 of this series on how to make Tetris from scratch.
