Unity3d and leap motion-based jigsaw puzzle games

Source: Internet
Author: User

Recently, I used the unity3d engine to create a jigsaw puzzle game, which will be completed several times and used as a summary. This article basically finds all the information that can be found online as a reference. This saves time.

Currently, only the puzzle part has been completed. the leap motion gesture Control Branch will be completed later, but to be honest, it is not optimistic about LM.

The project resources come from the http://game.ceeger.com/forum/read.php? Tid = 2852 is the same. I have also rewritten the former program. Some of the Implementation ideas are different. The two game projects will be packaged and uploaded after this article is completed.

The first is the module required by the entire game.

A puzzle game generally requires the following control modules: fragment display, random fragmentation disruption, legality check of the fragment order after random sorting, mobile control, and check whether the puzzle is completed.

1. display fragments.

This part can have several options, the first is to separate each part into an image, such

The advantage of http://tieba.baidu.com/p/2053275362 is that the processing is more convenient, you can use GUI processing, the disadvantage is that each image needs to be processed in the early stage, and because the number of parts in the early stage is fixed, the difficulty of the game cannot be adjusted at will, unless each graph has a lot of separated small graphs with different difficulty.

The second is to use Atlas in NGUI to redefine sprite information in the gallery. For more information, see the small program. The outer struct in the UISprite class is used, and the outer records the position information of sprite in the gallery. However, in NGUI3.6, outer is no longer a member variable of UISprite, and whether it can be modified using the methods in this article has not been tried. If someone tries, please leave a message and thank you.

The third is to set the texture offset and scaling in the material ball. For details, refer to the post posted by Dongdong. I also use this method.

The advantage of the second and third methods is that you can adjust the number of pictures to be divided at will, so you can easily adjust the difficulty of the game.

Vector2 offset; \ records the offset of each part. x = origin. x + piecesLength * j; offset. y = origin. y-piecesLength * I; temp. transform. localPosition = new Vector3 (offset. x * 10f, offset. y * 10f); texOffset [k]. x = j * transform. localScale. x/row; \ calculates the texture offset texOffset [k]. y = (row-1-i) * transform. localScale. x/row; temp. renderer. material. mainTextureOffset = texOffset [k]; \ sets the texture offset temp. renderer. material. mainTextureScale = new Vector2 (transform. localScale. x/row, transform. localScale. x/row); \ sets texture Scaling



Because the graph source is a square, only the division of N * N blocks is considered. Therefore, the calculation of offset and scaling is relatively simple.

The display of fragments is so much. below is the fragment disruption algorithm.

Ii. Fragment disruption Algorithm

There are two different ideas here. The first one is adopted by Xiao Peng. The correct fragments are randomly moved several times to disrupt the fragmentation order. The advantage of this method is that, the random sequence generated using this method can be restored, but the disadvantage is that the implementation is complicated. See the article on implementation.

The second approach is to use the shuffling algorithm to save the texture offset of the nth shard using an array, starting from the first shard and exchanging content with a random shard until the array ends. It is relatively simple to implement the algorithm, but it may not necessarily be restored correctly.

void Shuffle(){Random.seed = System.Environment.TickCount;for (int i = 0; i < squence.Length - 1; i++) {  int temp = squence[i];  int randomIndex = Random.Range(0, squence.Length-1);  squence[i] = squence[randomIndex];  squence[randomIndex] = temp;  }  }



Iii. Reverse Order and Test

The random sequence obtained through the shuffling algorithm may not be restored to the initial sequence because the reverse order and of the array are changed during shuffling. Join Baidu Encyclopedia (unrecoverable puzzles )...... Http://baike.baidu.com/link? Url = required _

Lemene proves this, please refer to http://www.cppblog.com/lemene/archive/2007/10/04/33405.html

Shaomn is easy to understand, http://blog.sina.com.cn/s/blog_4ed8b87701011c6x.html

For the array squence [], define its reverse order and sum + = (I-j) * (squence [I]-squence [j])> 0? 0: 1; for the reverse order of the initial sequence is 0, the sum of the left and right moving fragments remains unchanged, and the sum + 2,-2, or unchanged when moving up and down, but no matter how moving, the reverse order and parity remain unchanged. Therefore, after shuffling the algorithm, check whether the reverse order of the array and whether it is an even number.

bool Check(){int sum = 0;for (int i = 0; i < squence.Length; i++)for (int j = 0; j < i; j++)sum += (i - j) * (squence [i] - squence [j]) > 0 ? 0 : 1;return sum % 2 == 1;}


 

(To be continued ......)

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.