The third topic is Tetris game. The game algorithm is relatively simple, the main kungfu in the interface experience, such as block graphics, user control, elimination and accumulation.
We use GDI + graphic display, a total of 8 kinds of graphics, of course, can be customized according to their preferences. How do I represent graphics? We can draw on the mosaic effect. All graphics are represented by 3*3 matrices, 0 for background color, 1 for shaded display. Like what
{0,0,0},
{0,1,0}, the T-type, do you see it? Each digit position is actually a square block.
{1,1,1}
User Control? Simple KeyPress event implementation is good. Of course, there is no user control is automatic whereabouts, you can use timer implementation.
Elimination algorithm? ^_^, of course, is very simple, if there are 0 cells on a line, then the expression is not filled, there is no need to eliminate, otherwise it will be eliminated. The cumulative is the array append, elimination is the element deletion.
Of course, the elimination and accumulation of data needs to be reflected in the interface, that is, drawing. Graphics objects We all use, in this Windows Forms game, we can implement OnPaint events. Whenever you need to redraw, such as user control, elimination, and so on, call form refresh, then naturally triggers the paint event. The following is a code example:
public int[][,] figures = new int[8][,]//8种图形
{
new int[3,3] {
{0,0,0},
{0,1,0},
{1,1,1}
},
new int[3,3]
{
{0,0,0},
{0,2,2},
{2,2,0}
},
new int[3,3]
{
{0,0,0},
{3,3,0},
{0,3,3}
},
new int[2,2]
{
{4,4},
{4,4}
},
new int[3,3]
{
{5,0,0},
{5,0,0},
{5,5,0}
},
new int[3,3]
{
{0,6,0},
{0,6,0},
{6,6,0}
},
new int[4,4]
{
{7,0,0,0},
{7,0,0,0},
{7,0,0,0},
{7,0,0,0}
},
new int[3,3]
{
{0,8,0},
{8,8,8},
{0,0,0}
}
};