Create a folder called ‘objects’ in the library ‘Tetris parts’. In it, create a code file ‘square’. This file should contain a class square in which we describe a square.
A square must have a position. Because the playing field in Tetris is a grid (we can’t put a square anywhere we want!), we will use another VecI2 to store integer positions only. Furthermore, the square must have a block type to determine in what color it should be drawn. We have already defined the types of blocks in the ‘enumerations’ file.
The class square will also need some methods. We provide a create method to set the position and type of the block. In addition, we need a move method to move the block in a certain direction. (That direction will be the second enum we declared.) We also need a method to retrieve the current position, and a method to change the block’s position directly. And finally, we want a method to draw a square on the screen.
The class looks like this:
class square
{
private:
VecI2 pos ;
5 BLOCK_TYPE type;
public:
void create (C VecI2 & pos, BLOCK_TYPE type) {}
void move (DIRECTION dir) {}
10 VecI2 getPos( ) C { }
void setPos (C VecI2 & pos) { }
void draw ( ) C { }
}
Add the above code to your project.