Unity3d Getting Started Tetris summary (i)

Source: Internet
Author: User

Main reference: http://blog.csdn.net/kobbbb/article/details/8900974

Draw Tetris for Unity's introductory learning.

The results are as follows:

The basic ability is realized.

Summarized as follows:

1.Unity and DX Similar, default left-handed coordinate system. Rotate and draw in a clockwise direction. 2. Note Manger, Mycube, Block1,block2 .... The relationship between

Where Mycube is the basic unit of drawing, block1 and other shapes are also drawn by Mycube. 2-2, the Block1 property is just a logical drawing of the design, and there is no real render, in the CS script, finally there is mycube to instance operation, the string of the ' 1 ' judgment, the combination of drawing Block1 ...

Manger, as an empty gameobject, controls the whole, combining seven forms and loading mycube to make a real drawing.

Fig. 2-1 Fig. 2-2

3. Distinguish between stationary block plotting and dynamically declining block plotting
IEnumerator Fall () { while(true) {myposition--;//block whereabouts,if(true==Manager.manager.CheckBlock (Blockmatrix, Mxposition, myposition))//If a wall, drop stop {Manager.manager.SetBlock (Blockmatrix, mxposition, Myposition +1)///Draw a static block and initialize a new block Destroy (Gameobject) within the Setblock;//delete this dynamic block, Break//This block is dead, and a new block is falling. for(floati=myposition+1; i>myposition;i-=time.deltatime*mfallspeed)//steady drop of a unit, taking time as control, without the control of frame rate {Transform.position=NewVector3 (transform.position.x, I-childsize, transform.position.z); yield return NULL;//wait for the next frame to fall ...} }    } 
4. The scene coordinate system and the initialization coordinates of each dynamic block

This example has a local coordinate system and a world coordinate system.

Figure 4-1

As 4-1,block as a gameobject, you need to determine a transform, and for all subsets of the center. To initialize:

BlockSize =block. length;//This length is 3 childsize= (BlockSize-1) *0.5f; Blockmatrix=New BOOL[blocksize,blocksize];//creates a bool array of corresponding elements to determine whether to draw Myblock for(inty =0; Y < blocksize;y++ )        {             for(intx =0; X < blocksize;x++ )            {                if(block[y][x]=='1')//If the character in element is ' 1 ', then clone a mycube, substantive drawing {Blockmatrix[y,x]=true;
The position of each cube relative to the parent node, which is the cube drawing in the local coordinate system var cube = (Transform) instantiate (Manager.manager.oneCube, new Vector3 (x-childsize, Childsize- Y, 0), quaternion.identity); Cube.parent = transform;//bind to parent node }} } myposition= Manager.manager.GetFieldHeight ()-1;
Initialize the location of the block, and the upper-left corner of the position myposition mxposition; transform.position=NewVector3 (Manager.manager.GetFieldWidth ()/2+ (blockSize%2==0?0.5f:0), Myposition-childsize,0); Mxposition= (int) (transform.position.x-childsize);
For example, represents the coordinates of a dynamic block when it falls in the entire scene

Figure 4-2

     Public voidSetblock (BOOL[,] Blockmatrix,intPosX,intPosY)//draw the corresponding block in the rest area    {        intSize = Blockmatrix.getlength (0); When the judgment falls, it is stationary, then the rest is drawn:
for(inty =0; Y < size;y++ )// { for(intx =0; X < size;x++ ) { if(true==Blockmatrix[y,x]) {Instantiate (Onecube,NewVector3 (Posx+x, Posy-y,0), quaternion.identity);//Draw the static block Fieldsjudge[posx+ x, posy-y] =true; } } } //after this block is stationary: 1. Does the detection line satisfy the elimination? 2. Create a new dynamic block ...Startcoroutine (Checkrows (posy-size,size)); }
Instantiate (Onecube, New Vector3 (posx+x, posy-y, 0), quaternion.identity);
which
Vector3 (posx+x, posy-y, 0) 4-2 can be known that each cube's drawing needs to correspond to its own coordinates, this time the coordinates of the world coordinate system.


5. The whole process of Tetris

1. Random initialization of a block,createblock (int random) in Manger.cs

2. For this block, the Myblock is instantiated according to the ' 1 ' of string, and the transform of this block is set,

3. Decline ... is judgment still? ... Yes, draw a static block and create a new dynamic block, deleting the previous dynamic block

4. Repeat the above process ...

One of the most difficult things to understand is the relationship between the local coordinate system and the child nodes in the code, the location information for each block

The next chapter records the unity syntax used this time.

Unity3d Getting Started Tetris summary (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.