Preparation
We need to prepare pre-rendered sprites of all game objects. Usually, graphical packages like 3DMax, allowing working with 3D scenes, are used for that. To get a rectangular isometric projection we need to turn a camera, aligned parallel the floor and coordinate axes, down to ≈35.264° around the horizontal axis and ±45° around the vertical axis. The game conception means that it is necessary to have pre-rendered sprites of all possible forms of cells, which comprise the surface over which the block moves as well as sprites of the block itself in three states (in a vertical and 2 horizontal ones). If block sprite enables a semi transparent shadow, then it will be realistically cast on corresponding cells. We will get sprites, like those shown on the picture down below.
East block position
Different Types of Cells
Programming Game Field
Now we have necessary sprites and we can start writing the program logic of drawing these sprites. First, it is necessary to create a class for presenting a cell of a game field:
// class presenting a cell of a game field
public class Cell
{
// coordinates in the massive of cells (correspond to the coordinates on //the game field)
private int x;
private int y;
// cell type
private CellType cellType;
// number of the image in the set of cells images
private int bitmapNumber = 1;
// Constructor. Cell type and its location in the array of cells is //transmitted into it
public Cell(CellType cellType, int x, int y)
{
...
}
...
}
As it is seen from the definition of a class each cell should have:
- cell coordinates on the plane of a game field (x and y);
- CellType defining not only the view of a cell but other peculiarities, e.g. cell behavior or other properties;
- Bitmap number in a common set of bitmaps (bitmapNumber).
Each cell object is instanced by the constructor with three parameters (cell type and its coordinates on the plane) Now it is necessary to store all this set of cells of the game field somewhere. As the set of cells is determined by the level on which the game happens, then it is logical to place this set in Level class as an array of Cell type values
:
//class for a level on which the game process takes place
public class Level
{
// length and width of a level in the number of cells
private int fieldLength = 15;
private int fieldHight = 10;
// level number
private int number;
// block coordinates
private PointCoordinates firstBlockPosition;
private PointCoordinates secondBlockPosition;
// array of cells determining a game field
private Cell[][] gameField;
// constructor for creating a Level by a level's number
// number - level's number which has to be created
public Level(int number)
{
this.number = number;
CreateLevel(number);
}
private void CreateLevel(int level)
{
//Creates a corresponding level by the initialization of //variables, class members and gameField massive fill in
}
...
}
This class determines all necessary level parameters, in particular:
- Its size (fieldLength x fieldHight);
- Its number (number);
- Block coordinates on a plane firstBlockPosition and secondBlockPosition. As a block consists of 2 cubes, then the coordinates of the whole block are defined by the coordinates of cubes it consists of;
- Set of this level’s gameField cells.
All these parameters are known and are initialized by CreateLevel (int level) method for each certain level defined by its number. In particular, it is possible to implement reading of level parameters from the file which will be formed by some constructor of levels which is an exterior utility. In order to start the drawing we need to create a presentation for the main application’s Activity. It is possible to do that in the following way:
public class MainActivity extends Activity
{
// link to view in which the drawing is implemented
private MainView mainView;
...
/**Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
...
// connect Activity with View
mainView = new MainView(this);
setContentView(mainView);
super.onCreate(savedInstanceState);
}
...
}
, where MainView class is a class derivative from View:
public class MainView extends View
{
MainActivity mainActivity;
...
public MainView(Context context)
{
super(context);
this.mainActivity = (MainActivity) context;
...
}
@Override
protected void onDraw(Canvas canvas)
{
Paint foreground = new Paint(Paint.ANTI_ALIAS_FLAG);
// draws cells
DrawCells(canvas, foreground);
//draws the block
DrawBlock(canvas, foreground);
...
}
...
}
The next post “Drawing Game Cell” is coming soon. Sign up for RSS feed or follow us on twitter to be posted about updates!












