The development of small games a total of 18 classic small games, updated every day. With you to learn some classic small game development ideas and algorithms, if you want to directly see the full effect of the game, the relevant complete project can directly go to my resources to download. Learn One day with everyone.

Today’s little jigsaw puzzle

A quick word about jigsaw puzzles:

Jigsaw puzzle is a classic puzzle game. Its history can be traced back to around 1760, when English engravers and cartographers stuck maps on cardboard and then cut them up along the national lines to create the original jigsaw puzzle. At first, puzzles were educational, with pictures teaching people history or geography. Later, various puzzles emerged, but most of them were beneficial to the development of people’s brain power, memory training and practical ability.

Rules of the game:

Breaking an image into pieces (pieces) and players putting the pieces together in the original style is considered a success.

Let’s talk about the development idea:

1. Game flow chart:

2. The original picture corresponds to the coordinates of fragments

Using 33 squares to design this jigsaw puzzle, the 9 pieces that make up the original picture can be represented as different integers (e.g., 1,2,3,4,5,6,7,8,9) and saved into a two-dimensional array of 33.

If the width of the fragment is W and the height is H, the center position of the fragment is the coordinate position of the fragment, and the image of the first element of the two-dimensional array is located at the lower left corner of the original image.

When the coordinate of the lower left corner of the original picture is the origin coordinate of the world coordinate, that is, the position of (0,0), assuming that the index number of the current picture in the two-dimensional array is [I][j], the specific position of the current fragment is as follows :(x=(I +1/2)*w, y=(j+1/2)*h)

3. Corresponding interactive operations

Mouse drag and drop moving fragments function:

(1). Determine whether the clicking position of the mouse is within the fragment (for example, the coordinate of the lower left corner of the fragment is (sx, SY)), that is, within the internal coordinate range of the fragment sx<x<sx+ W,sy<y< SY + H. If the mouse’s world coordinate position is within this range, you can determine that the fragment is selected.

Note: The screen coordinates of the mouse must be converted to world coordinates.

(2). Implement dragged fragments to move with the mouse:

Convert the screen coordinates of the mouse to the world coordinates, then calculate the offset values of the mouse position and the fragment position as follows:

Shard coordinates = the world coordinates of the mouse + the offset of the shard position and mouse position.

(3) when the mouse is released:

Get the current position of the shard, place the shard in the correct position if it is close to the corresponding correct position coordinates (set a critical distance value, if the correct position and the shard position is less than this critical value), otherwise put the shard back into the shard pool.

4. Judge correctly

Distance calculation of fragment position and corresponding correct position:

Hypothesis (x1, y1) and B (x2, y2) corresponding to the location of the fragments and the corresponding correct position, we can use the formula | AB | squared = (x1, x2) squared + (y1, y2) squared primitives is square root, but the root of computing resources consuming, so we can directly use this formula. It can also be directly judged by the position of two coordinate points, such as the coordinate (Sx, SY) of the obtained fragment and the corresponding correct position (Tx,Ty).

The pseudocode is as follows:

If (| Tx – Sx | < threshold && | Ty – Sy | < critical value)

{

Fragment coordinates = correct coordinates;

}

else

{

Fragments return to their original positions;

}

5. Winning judgment

We set a variable to count. Each time a fragment is placed in the correct position, this variable will be +1. When this variable is equal to the total number of fragments, it means that all the fragments are placed in the correct position. The winning game is over; You can set timeout, timeout will fail the game!

(If you see this blog post, write it yourself before you see the game implementation below)

Here is the game implementation:

The specific implementation code is as follows:

CreatePic class loads resources and initializes the interface

using System.Collections; using System.Collections.Generic; using UnityEngine; public class CreatePic : MonoBehaviour { public string sprit_Path = "Sprites/Pictures"; public Sprite[] sp_S; Public int textureNum = -1; Public static GameObject[,] PIC = new GameObject[3,3]; public static bool isSetTruePosition = false; // </summary> void Start () {sp_S = resources.loadall <Sprite>(sprit_Path); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { textureNum++; pic[i,j] = new GameObject("picture" + i + j); PIC [I,j].addComponent <SpriteRenderer>().sprite = sp_S[textureNum]; PIC [I,j].getComponent <Transform>().position = new Vector2 (Random Range (3.0 f to 5.5 f), Random. The Range (0.0 f to 2.5 f)); }}}}Copy the code

MouseDrag class MouseDrag operation code implementation:

using System.Collections; using System.Collections.Generic; using UnityEngine; public class MouseDrag : MonoBehaviour { private Vector2 _vec3Offset; // Offset public vector2_ini_pos; Private Transform targetTransform; Public static int width = 3; public static int height = 3; Public float threshold = 0.2f; // Critical value private bool isMouseDown = false; Private Vector3 lastMousePosition = vector3. zero; float chipWidth = 1; // Float chipHeight = 1; Void Update() {if (input.getMouseButtonDown (0)) {isMouseDown = true; for(int i = 0; i<width; i++) { for(int j = 0; j<height; j++) { if(Mathf.Abs(Camera.main.ScreenToWorldPoint(Input.mousePosition).x - Done_CreatePic.pic[i, j].transform.position.x) <chipWidth/2 && Mathf.Abs(Camera.main.ScreenToWorldPoint(Input.mousePosition).y - Done_CreatePic.pic[i, j].transform.position.y) < chipHeight/2) { targetTransform = Done_CreatePic.pic[i, j].transform; _ini_pos = new Vector2(targetTransform.position.x, targetTransform.position.y); // Record the fragment's initial position break; } } } } if (Input.GetMouseButtonUp(0)) { isMouseDown = false; lastMousePosition = Vector3.zero; OnMyMouseUp(); } if (isMouseDown) { if (lastMousePosition ! = Vector3. Zero) {/ / put objects in the upper targetTransform GetComponent < SpriteRenderer > () sortingOrder = 100; Vector3 offset = Camera.main.ScreenToWorldPoint(Input.mousePosition) - lastMousePosition; // Mouse offset // Current position of the shard = position of the previous frame of the shard + mouse offset targetTransform.position += offset; } lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); } } void OnMyMouseUp() { for(int j = 0; j<width; j++) { for(int i= 0; i<height; i++) { if(targetTransform.name == Done_CreatePic.pic[i,j].name) { if (Mathf.Abs(targetTransform.position.x - j) < threshold&& Mathf.Abs(targetTransform.position.y - i) < threshold) { Debug.Log("OnMyMouseUp"); targetTransform.position = new Vector2(j, i); Done_GameOver._trueNum++; Debug.Log(Done_GameOver._trueNum); Done_GameOver.Judge(); break; } else { targetTransform.position = _ini_pos; } } targetTransform.GetComponent<SpriteRenderer>().sortingOrder = 5; }}}}Copy the code

GameOver end decision

using System.Collections; using System.Collections.Generic; using UnityEngine; public class GameOver : MonoBehaviour { public static int _trueNum; Private static int _allPicNum = 9; private static int _allPicNum = 9; Public static void Judge() {if (_trueNum == _allPicNum) {if (_trueNum == _allPicNum) { Debug.log (" Game over "); }}}Copy the code