The examples in this article share with you the 3rd part of Unity’s implementation of Tetris for your reference. The specific content is as follows
Solve penetration problems
Logical part
1. Update the grid information during the movement of the object, and empty the original occupied position information, and now occupied grid is assigned.
2. Make a judgment on the position after moving. First, the position after moving cannot be null, and it cannot be a moving object.
Code section
void updateGrid() { //Remove location information before moving for (int y = 0; y < ; y++) { for (int x = 0; x < ; x++) { //The script is hung on the object instead of the grid that makes up the object //If there is a grid that is the same as the current position, then empty it if ([x, y]!= null) { if ([x, y].parent == transform) { [x, y] = null; } } } } //Add to this update location information foreach (Transform child in transform) { Vector2 v = Grid.roundVec2(); [(int), (int)] = child; } } bool isValidGridPos()//Judge whether it is valid { foreach (Transform child in transform) { Vector2 v=Grid.roundVec2(); //Judge whether it is between boundaries if (!(v)) return false; //If the grid is occupied and is not a moving object, it cannot continue to move. if ([(int), (int)] != null&& [(int), (int)].parent != transform) return false; } return true; }
Solve the operation of clearing the line and moving downward after the line is full
Logical part
1. When it cannot be moved, you must determine which line on the current game page is full and delete the line.
2. After deleting this line, you must drop all the blocks on this line. If this line is full, first delete it and move the overall block above down. Check this line until this line cannot be deleted, and then check the previous line.
Code section
//Judge whether a line is full public static bool isRowFull(int y) { for (int x = 0; x < width; x++) { //As long as one of the lines is empty, then this line will be unsatisfied if (grid[x, y] == null) return false; } return true; } //Delete all data in a certain row public static void deleteRow(int y) { for (int x = 0; x < width; x++) { //Delete the elements of this line Destroy(grid[x, y].gameObject); //Set the grid empty grid[x, y] = null; } } //Delete all full lines public static void deleteFullRows() { for (int y = 0; y < height; ) { if (isRowFull(y)) { deleteRow(y); += 5; decreaseRowAbove(y + 1); } else y++; } } //Down a whole row public static void decreaseRow(int y) { //Copy the data of this line to the next line //Clear the data of this line //Visually change the position of the original block for (int x = 0; x < width; x++) { if (grid[x, y] != null) { grid[x, y - 1] = grid[x, y]; grid[x,y].position += new Vector3(0,-1,0); grid[x, y] = null; } } } //Check from the specified number of rows and move the row and the data above the row down public static void decreaseRowAbove(int y) { for (int i = y; i < height; i++) { decreaseRow(i); } }
Set grades
Logical part
Whenever a row is eliminated, the score will be added to 5 points.
Code section
public Text Score;//The text component used to bind the scores to modify the content of the textpublic static int score = 0; void Start() { //Sensitize to upper and lower case timer = ("Canvas/Timer").GetComponent<Text>(); Score = ("Canvas/Score").GetComponent<Text>(); //Get the game start time (unit: s) startTime = ; } // Update is called once per frame void Update() { //How long has the game been running (unit: s) time= - startTime; //Second int seconds=(int)time % 60; //Fraction int minutes = (int)time / 60; //Where: The first 0 1 refers to the parameter: the next 00 represents the consist of several bits. The between {}: is to be displayed string strTime = ("{0:00}:{1:00}",minutes,seconds); = strTime; = (); }
For more exciting Tetris articles, please click on the topic:Tetris game collectionConduct learning.
More interesting classic game implementation topics, share with you:
Summary of C++ classic games
Summary of classic python games
python Tetris game collection
JavaScript classic game
Summary of classic javascript games
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.