Now let’s start to draw the block on the game surface. As it was said before, in the majority of cases the block is situated on the game field surface, that is why it should be drawn after the game field is drawn. The drawing of a block has no differences from the drawing of cells, though it has some peculiarities. In particular, the block can be in 5 positions (but as it is possible to get an opposite position in a horizontal plane by the mirror reflection, then we can think that there are 3 positions):
- Vertical (gameLogic.IsBlockVertical());
- Horizontal : direction from the South to the North (gameLogic.IsNorth()), let’s call it North position;
- Horizontal : direction from the West to the East, let us call it East position.
When we talk about the direction then we mean a vector, the start of which is the second cube of the block, and its end showing the direction is the first cube of the block. If to track always when the block moves and, if necessary, change 2 block cubes places, then it may simplify the drawing up to the implementation presented below.
Besides, a block, unlike cells has a height, which has to be considered, in order to create an effect of block moving on the game field surface.
A block is drawn in the main application’s Activity by DrawBlock function, called from onDraw, but after DrawCell function.
private void DrawBlock(Canvas canvas, Paint foreground)
{
int leftOnScreen = 0;
int topOnScreen = 0;
int x = 0;
int y = 0;
Bitmap bitmapToDraw = null;
if (gameLogic.IsBlockVertical())
{
// getting coordinates and calculating of location on the screen
// of a vertical block
bitmapToDraw = vertical;
x = gameLogic.GetFirstBlockPosition().GetX();
y = gameLogic.GetFirstBlockPosition().GetY();
leftOnScreen = (int) (x * width - horCorrection * x + horLineCorrection * y);
topOnScreen = (int) (y * height - verCorrection * y - verLineCorrection * x - verticalBlockHeight);
}
else
{
// calculation of horizontal block coordinates depending on its location
if (gameLogic.IsNorth())
{
bitmapToDraw = north;
x = gameLogic.GetFirstBlockPosition().GetX();
y = gameLogic.GetFirstBlockPosition().GetY();
leftOnScreen = (int) (x * width - horCorrection * x + horLineCorrection * y);
topOnScreen = (int) (y * height - verCorrection * y - verLineCorrection * x - northBlockHeight);
}
else
{
bitmapToDraw = east;
x = gameLogic.GetSecondBlockPosition().GetX();
y = gameLogic.GetSecondBlockPosition().GetY();
leftOnScreen = (int) (x * width - horCorrection * x + horLineCorrection * y);
topOnScreen = (int) (y * height - verCorrection * y - verLineCorrection * x - eastBlockHeight);
}}
// block drawing
canvas.drawBitmap(bitmapToDraw, leftOnScreen, topOnScreen, foreground);}
From the code, we can see that to draw the block first it is necessary to define in what position it is, it is defined by comparing coordinates of 2 cubes, composing the block. leftOnScreen coordinate is received by an analogous way for cells, as horCorrection, horLineCorrection, verCorrection and verLineCorrection constants remain the same. topOnScreen coordinate is received by shifting of a block sprite left upper corner top, on practice it means that Y coordinate should be reduced to the height of a corresponding current position of block sprite. At that, blocks in different states have different height and that is why verticalBlockHeight, northBlockHeight and eastBlockHeight constants differ from one another.
In order to understand what should be the values of verticalBlockHeight, northBlockHeight and eastBlockHeight constants it is necessary to look at picture:
On the image you can see that these constants are equal in the length of a vertical rib of a corresponding block adjoining the upper left corner of the sprite.
Conclusion
Thus, not implementing 3D graphics on OpenGL it is possible to create “almost” 3D scene, without shortages of the implementation of 3D version. Of course, it is necessary to do a lot to get a full-scale game. For example, write logic of block moving including the situation of falling and block impact on special cells, processing of special field cells reaction, animation of falling and moving on the surface of the game field, saving and loading of the game, graphical user interface and different ways to control the block etc.
If this article was interesting, we will continue the series of articles on this topic.
Let us know your opinion in comments. Thanks.















So cool, thnx guys. Excelent topic, awesome approach. This is really helpful.
I like the articles about Pseudo 3D Graphics, could you share your source code and the drawables in a package?
Dear Sir,
I found ur article very helpful to my application since we are developing the game which u took as example to explain..please i wanted few more codes and design of layouts for further development of our application waiting for ur reply..
thanks in advance