Source code analysis of ARTetris-AR version of Tetris

1. Project effect

 Figure 1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The framework of the project

Third, the specific code interpretation

Start with the method of viewDidLoad:

1. Start

Get the horizontal plane, and then start the game in the proxy method func renderer(...) function.

Start the analysis from 67 lines of code:

The entire TetrisConfig.swift, just this piece of code, configures the black grid wells in Figure 1. Just set the width and height. Colors, etc. can be set later, which can be set according to project requirements.

2. The following  is an analysis of the functions in Tetromino, TetrisState, TetrisWell, TetrisScene, and TetrisEngine

2.1、Tetromino 

FixedTetromino is a class that stores 8 points, the odd-numbered bits are the x-coordinates, and the even-numbered bits are the y-coordinates.

Line 20 of the code in the image above represents a square shape: for example

                               Figure 6    then [Lines 20 to 23] has 4 columns, representing the rotation state of Figure 6 in 4.

2.2、TetrisState

TetrisState refers to the shape and position of Tetris. The shape and position also depend on the FixedTetromino class.

 

2.3、TetrisWell

TetrisWell refers to the black mesh well in Figure 1. The code setup here is very interesting.

 

private var matrix: [[ Bool]] = [] //Nested array. I have written a note for specifics.

The main function is: if it is false , it means that the current position is not occupied by the block. Then update the value of the matrix array by comparing through a for loop .

 2.4、TetrisScene

Mainly look at the init function and the show function  createWellFrame function.

In the AR world, it is the SCNNode node.

  1. The drawing of the black grid well in Figure 1 is created in the init function.
  2. Then look at the show() function to display the first randomly generated Tetris.

2.5、TetrisEngine

The TetrisEngine game engine controls rotation, left and right movement, etc., but in essence, it relies on the Tetromino class for state control.

4. Logic 

  1. All blocks and grids are SCNNode nodes. Then the Tetromino class stores the shape of the block. TetrisState further deepens Tetromino, including the algorithm that the position coordinates of the block have moved. Then the specific algorithm is nothing more than changing the value of a specific point in Tetromino .
  2. The movement of the block also changes the value of the specific point in the Tetromino . Then rotate and explain in Figure 6
  3. Regarding the splicing of Tetris in the process of moving down, it controls the matrix array in TetrisWell to update, thereby controlling the state of the blocks in the entire program.
  4. The specific movement and judgment is to compare the matrix array through the for loop to update, and then perform the corresponding processing, see the source code for details.

5. Expansion

AR feature recognition is a very simple feature recognition. Able to recognize planes. For example: the plane of the floor, the plane of the table. But assuming that in a complex environment, it can be recognized that the ground is the ground and the table is a table, then you can use this property to do many useful functions, such as: indoor navigation. In the future, I also plan to integrate SLAMORB2 for feature recognition and integrate it into the AR project of iOS. The new engine will be very useful.

6. Citation

Source address: https://github.com/exyte/ARTetris

 

Source address after my comment: https://download.csdn.net/download/yuhui_now/10379609 . In order to avoid copyright issues, the transmitted CSDN, if necessary, you can send me an email to you privately.

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324969748&siteId=291194637