The preparations have been completed, and the key part "principles" has also been said. It's time to do something.
The main callProgramIt's time for the game class to appear,
I have to say that, first of all, for programmers, whether you are a master or a cainiao.
Clarify the goals and objectives of what you want to do.
What to do first, what to do first, what is "done", and what is "failed"
This is what a normal programmer should think. As a programmer, we need to see through the essence of things.
The flowchart is free of charge.Code(In fact, this is the real code ^_^, don't hit me ):
Public void go () // The main call method indicates 1 step. If the game is not over, it continues until the game ends {If (downcheck ()) // if there is a falling space {down (); // falling action} else // if the space exists or it is in contact with other shapes {Merge (); // print the "stuck" Square to the canvas (container gamearea), which means to pile the stuck square to the game zone if (! Isover) // if the game is not over (based on whether the block has been stacked to the top of the container) re-instantiate a shape to let it down {shape S = new shape (); int pindx = 0; For (INT I = 0; I <S. transform. getlength (0); I ++) {for (Int J = 0; j <S. transform. getlength (1); j ++) {If (S. transform [I, j] = true) {sh [pindx ++] = new point (J, I) ;}}} setscore (); // check the gamearea to see whether there are rows that can be eliminated and calculate the score before re-instantiation}
The following are some specific things, such as the left shift operation, the falling action, and whether there are any lines that can be eliminated. The specific implementation depends on your implementation method orAlgorithmIt is better than me and I want to share it.
The following is my implementation method:
Start the game:
Public void start () // start a game, including initializing gamearea and so on {area = new gamearea (); shape S = new shape (); int pindx = 0; for (INT I = 0; I <S. transform. getlength (0); I ++) {for (Int J = 0; j <S. transform. getlength (1); j ++) {If (S. transform [I, j] = true) {sh [pindx ++] = new point (J, I );}}}}
Mobile:
Gamearea area; // container (storing stacked blocks) point [] SH = new point [4]; // falling blocks (each block can only consist of up to four points) enum direction {left, right, down}; void move (Direction DIR) // move {Switch (DIR) {Case direction. left: {bool can = true; If (Sh! = NULL) foreach (point P in SH) {If (P. x <= 0) {can = false; break;} If (CAN) {for (INT I = 0; I <Sh. length; I ++) Sh [I]. X --;} break;} case direction. right: {bool can = true; If (Sh! = NULL) foreach (point P in SH) {If (P. x> = gamearea. width-1) {can = false; break;} If (CAN) {for (INT I = 0; I <Sh. length; I ++) Sh [I]. X ++;} break;} case direction. down: {for (INT I = 0; I <Sh. length; I ++) Sh [I]. Y ++; break ;}}}
Determine whether the square is stuck:
Bool downcheck () // perform a bool downcheck. For example, if the bool downcheck is finished or is in contact with another shape, compare it with the corresponding square in the container to see if "bool in contact {foreach (point P in SH)" is generated) {If (P. y> = gamearea. height-1) return false; If (area. gameareaarray [p. Y + 1, p. x] = true) return false;} return true ;}
Determine whether the game is over:
Public bool isover {// notify the external program that you are down (determine whether the program has been stacked to the top of the gamearea) get {for (Int J = 0; j <area. gameareaarray. getlength (1); j ++) {If (area. gameareaarray [0, J] = true) return true;} return false ;}}
It may be complicated for the comrades to think about how to heap stuck blocks to containers. In fact, it is super simple. It is OK to change the array elements (or coordinates) corresponding to the square in the container to true.
It means to "write" the pixel of the stuck square to the container.
Just so
Void Merge () // merge the contact with the boundary to add the shape to the canvas {foreach (point P in SH) area. gameareaarray [p. y, P. x] = true ;}
How is it super simple? ^_^
Finally, I have written so much. In the next chapter, I will explain how to find the "image contour" and the "clockwise rotation" algorithm.
See chapter 4