2016年11月2日 星期三

九宮格-拼圖遊戲-小品遊戲 (結束)

本文是九宮格拼圖遊戲-小品遊戲

圖片分割程式碼(請點選文字超連結)

腳本對印圖:



























圖片分割數量:




















結果圖:


 ----------------
開始遊戲九宮格圖片亂數變化 Script(C#):

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof(DetectionObjcet))]
[RequireComponent(typeof(MoveImage))]
[RequireComponent(typeof(Gamesettlement))]
public class Jiugongge : MonoBehaviour
{
    public Sprite[] sprite = new Sprite[8];                                  // 預設圖片
    private int type = 9;                                    // 建立物件數量

    public List objcetList = new List();   // list 監管物件建立
    private GameObject spriteMaster;                        // sprite 管理物件
    private BoxCollider boxCollider;                        // Sprite Collider;

    private bool randomSw = false;                          // 亂數開關
    private int randomNumber;                               // 亂數
    private int limitObjcet = 0;                            // 限制物件

    private List numbers = new List();

    // Use this for initialization
    void Awake()
    {
        spriteMaster = new GameObject("SpriteMaster");  // sprite  管理
        spriteMaster.transform.position = Vector2.zero; // sprite 管理位置

        spriteSwitch();
    }

    /// 
    /// 視窗GUI
    /// 
    void OnGUI()
    {
        randomSw = (GUI.Button(new Rect(Screen.width / Screen.width, Screen.height / Screen.height, 100, 30), "拼圖重製")) ? true : false;

        if (randomSw.Equals(true))
        {
            //  notRepeatingRandom();
            Application.LoadLevel("aa");

        }
    }

    /// 
    /// 加入圖片亂數選擇
    /// 
    void spriteSwitch()
    {
        randomNumberListWithoutDuplicate(type);

        while (limitObjcet < type)
        {
            try
            {

                randomNumber = getNumberInList();

                createSprite().GetComponent().sprite = sprite[randomNumber];
            }
            catch
            {
                Debug.LogError("警告");
            }
            limitObjcet++;
        }
        show();
    }

    /// 
    /// 選取範圍
    /// 
    int getNumberInList()
    {
        int return_num = numbers[0];
        numbers.RemoveRange(0, 1);
        return return_num;
    }

    /// 
    /// 亂數排序
    /// 
    List randomNumberListWithoutDuplicate(int max)
    {

        for (int i = 0; i < max; i++)
            numbers.Add(i);


        for (int i = 0; i < numbers.Count; i++)
        {
            int temp = numbers[i];
            int randomIndex = Random.Range(i, numbers.Count);
            numbers[i] = numbers[randomIndex];
            numbers[randomIndex] = temp;
        }

        return numbers;
    }

    /// 
    /// objcet 位置
    /// 
    void show()
    {

        int objcetNumber = 0;

        foreach (GameObject objcet in objcetList)
        {
            objcetNumber += 1;
            objcet.transform.SetParent(spriteMaster.transform);
            objcet.name = "Image" + objcetNumber;

            switch (objcetNumber)
            {
                case 1:
                    objcet.transform.position = new Vector2(-3.4f, 0);
                    break;
                case 2:
                    objcet.transform.position = new Vector2(-3.4f, 3.4f);
                    break;
                case 3:
                    objcet.transform.position = new Vector2(-3.4f, 6.8f);
                    break;
                case 4:
                    objcet.transform.position = new Vector2(0, 0);
                    break;
                case 5:
                    objcet.transform.position = new Vector2(0, 3.4f);
                    break;
                case 6:
                    objcet.transform.position = new Vector2(0, 6.8f);
                    break;
                case 7:
                    objcet.transform.position = new Vector2(3.4f, 0f);
                    break;
                case 8:
                    objcet.transform.position = new Vector2(3.4f, 3.4f);
                    break;
                case 9:
                    objcet.transform.position = new Vector2(3.4f, 6.8f);
                    break;
            }
        }
    }

    /// 
    /// 建立物件
    /// 
    /// 
    GameObject createSprite()
    {

        GameObject objcet = new GameObject("Image");

        objcetAddComponentFunction(objcet);
        ComponentSetting();

        objcetList.Add(objcet);
        return objcet;
    }

    /// 
    /// 物件加入組件
    /// 
    /// 
    void objcetAddComponentFunction(GameObject objcet)
    {
        objcet.AddComponent();
        boxCollider = objcet.AddComponent();
    }

    /// 
    /// 組件設定
    /// 
    void ComponentSetting()
    {
        boxCollider.size = new Vector3(3.4f, 3.4f, 0);
    }
}

---------------
Script(c#) 偵測方位物件
using UnityEngine;
using System.Collections;

public class DetectionObjcet : MonoBehaviour {

    private Jiugongge jiugongge;                    // 九宮格腳本
    private Ray ray1, ray2, ray3, ray4;             // 射線
    private RaycastHit hits1, hits2, hits3, hits4;  // 射線碰撞

    public GameObject objcetSprtieNull;             // 沒貼圖片物件
    public GameObject leftMoveObjcet,               // 射線偵測左邊物件
                      rightMoveObjcet,              // 射線偵測右邊物件
                      upMoveObjcet,                 // 射線偵測上邊物件
                      downMoveObjcet;               // 射線偵測下邊物件

    void Awake()
    {   
        jiugongge = gameObject.GetComponent();   // 取得物件腳本
        readOjbcetNoSprite();             
    }

    void Update()
    {
        RayColliderObjcet();

        // 射線方位
        ray1 = leftRayPoint();
        ray2 = rightRayPoint();
        ray3 = upRayPoint();
        ray4 = downRayPoint();
    }

    /// 
    /// 射線偵測
    /// 
    void RayColliderObjcet() {

        if (Physics.Raycast(ray1, out hits1))
            leftMoveObjcet = hits1.collider.gameObject;

        if (Physics.Raycast(ray2, out hits2))
            rightMoveObjcet = hits2.collider.gameObject;


        if (Physics.Raycast(ray3, out hits3))
            upMoveObjcet = hits3.collider.gameObject;

        if (Physics.Raycast(ray4, out hits4))
            downMoveObjcet = hits4.collider.gameObject;
    }

    /// 
    ///  左邊射線
    /// 
    /// 
    Ray leftRayPoint() {

        Vector2 vector2 = objcetSprtieNull.transform.position;

        Ray ray = new Ray(vector2, Vector2.left);
        return ray;
    }

    /// 
    /// 右邊射線
    /// 
    /// 
    Ray rightRayPoint()
    {
        Vector2 vector2 = objcetSprtieNull.transform.position;

        Ray ray = new Ray(vector2, Vector2.right);

        return ray;
    }

    /// 
    /// 向上射線
    /// 
    /// 
    Ray upRayPoint()
    {
        Vector2 vector2 = objcetSprtieNull.transform.position;

        Ray ray = new Ray(vector2, Vector2.up);

        return ray;
    }

    /// 
    /// 向下射線
    /// 
    /// 
    Ray downRayPoint()
    {
        Vector2 vector2 = objcetSprtieNull.transform.position;

        Ray ray = new Ray(vector2, Vector2.down);

        return ray;

    }

    /// 
    /// 尋找沒貼圖物件
    /// 
    void readOjbcetNoSprite()
    {
        foreach (GameObject objcet in jiugongge.objcetList)
        {
            if (objcet.GetComponent().sprite == null)
                objcetSprtieNull = objcet;
        }
    }
}

--------------------
偵測物件並且與沒有貼圖物件位置對調
using UnityEngine;
using System.Collections;

public class MoveImage : MonoBehaviour {

    private DetectionObjcet detectionObjcet;        // 選擇物件類別
 
    private Ray userRay;                            // 玩家射線
    private RaycastHit userRayHits;                 // 射線碰撞

    private Vector2 moveLimitPoint,                 // 無Sprite 物件位置(交換位置)
                    clickObjcetPoint;               // 射線探聽物件位置

    void Awake() {

        detectionObjcet = GetComponent();
    }

    void Update(){

        userRayFunction();
    }

    /// 
    /// 玩家發出射線 (可更改控制事件)
    /// 
    void userRayFunction() {

        userRay = Camera.main.ScreenPointToRay(Input.mousePosition);
        moveLimitPoint = detectionObjcet.objcetSprtieNull.transform.position;

        if (Input.GetMouseButton(0))
        {
            if (Physics.Raycast(userRay, out userRayHits))
            {
                GameObject userOnclickObjcet = userRayHits.collider.gameObject;

                OnClickView(userOnclickObjcet);
            }
        }
    }

    /// 
    /// 探聽事件
    /// 
    /// 
    void OnClickView(GameObject objcet)
    {
        if (objcet == detectionObjcet.leftMoveObjcet)
        {
            clickObjcetPoint = objcet.transform.position;
            MoveObjcetFunction_Left(detectionObjcet.leftMoveObjcet, moveLimitPoint);
        }

        if (objcet == detectionObjcet.rightMoveObjcet)
        {
            clickObjcetPoint = objcet.transform.position;
            MoveObjcetFunction_Right(detectionObjcet.rightMoveObjcet, moveLimitPoint);
        }

        if (objcet == detectionObjcet.upMoveObjcet)
        {
            clickObjcetPoint = objcet.transform.position;
            MoveObjcetFunction_Up(detectionObjcet.upMoveObjcet, moveLimitPoint);
        }


        if (objcet == detectionObjcet.downMoveObjcet)
        {
            clickObjcetPoint = objcet.transform.position;
            MoveObjcetFunction_Down(detectionObjcet.downMoveObjcet, moveLimitPoint);
        }
    }

    /// 
    /// 偵測左邊物件_向右移動
    /// 
    /// 
    /// 
    void MoveObjcetFunction_Left(GameObject objcet, Vector2 point) {
  
        if(objcet.transform.position.x <= point.x)
        {
            objcet.transform.position = new Vector2(point.x, point.y);
        }
        detectionObjcet.objcetSprtieNull.transform.position = clickObjcetPoint;
    }

    /// 
    /// 偵測右邊物件_向左移動
    /// 
    /// 
    /// 
    void MoveObjcetFunction_Right(GameObject objcet, Vector2 point) {

        if (objcet.transform.position.x >= point.x)
        {
            objcet.transform.position = new Vector2(point.x, point.y);
        }
        detectionObjcet.objcetSprtieNull.transform.position = clickObjcetPoint;
    }

    /// 
    /// 偵測上邊物件_向下移動
    /// 
    /// 
    /// 
    void MoveObjcetFunction_Up(GameObject objcet, Vector2 point) {

        if (objcet.transform.position.y >= point.y)
        {
            objcet.transform.position = new Vector2(point.x, point.y);
        }
        detectionObjcet.objcetSprtieNull.transform.position = clickObjcetPoint;
    }


    /// 
    /// 偵測下邊物件_向上移動
    /// 
    /// 
    /// 
    void MoveObjcetFunction_Down(GameObject objcet, Vector2 point) {

        if (objcet.transform.position.y <= point.y)
        {
            objcet.transform.position = new Vector2(point.x, point.y);
        }
        detectionObjcet.objcetSprtieNull.transform.position = clickObjcetPoint;
    }
}

---------------------
遊戲結算(輸贏/其他方式結束遊戲) ※ 本程式只判斷遊戲是否拼圖正確
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Gamesettlement : MonoBehaviour
{

    private DetectionObjcet detctionObjcet;     // 選擇物件類別
    private Jiugongge jiugongge;                // 取得最先設定類別

    public Sprite winSprite;                    // 補區塊

    private bool win;                           // 輸贏數值

    void Awake()
    {

        detctionObjcet = GetComponent();
        jiugongge = GetComponent();
    }

    void Update()
    {

        readObjcetSprtie();

        if(win)
            detctionObjcet.objcetSprtieNull.GetComponent().sprite = winSprite;

        Debug.Log(win);
    }

    void OnGUI() {

        string str = (win.Equals(true)) ? "完成" : "未完成";

        GUI.Label(new Rect(Screen.width / Screen.width, Screen.height / Screen.height * 30f, 300, 30), str);

        if (Input.GetKey(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    /// 
    /// 讀取物件Sprite後及判斷位置
    /// 
    void readObjcetSprtie()
    {

        bool point1 = false, point2 = false, point3 = false, point4 = false, point5 = false, point6 = false,
            point7 = false, point8 = false;

        foreach (GameObject objcet in jiugongge.objcetList)
        {
            try
            {
                // sprite1 位置 x:-3.4f, y:0f;
                if (objcet.GetComponent().sprite.Equals(jiugongge.sprite[0]))
                {
                    if (objcet.transform.position.x.Equals(-3.4f) & objcet.transform.position.y.Equals(0))
                        point1 = true;
                }

                // sprite2 位置 x:-3.4f, y:3.4f;
                if (objcet.GetComponent().sprite.Equals(jiugongge.sprite[1]))
                {
                    if (objcet.transform.position.x.Equals(-3.4f) & objcet.transform.position.y.Equals(3.4f))
                        point2 = true;
                }

                // sprite3 位置 x:-3.4f, y:6.8f;
                if (objcet.GetComponent().sprite.Equals(jiugongge.sprite[2]))
                {
                    if (objcet.transform.position.x.Equals(-3.4f) & objcet.transform.position.y.Equals(6.8f))
                        point3 = true;
                }

                // sprite4 位置 x:0f, y:0f;
                if (objcet.GetComponent().sprite.Equals(jiugongge.sprite[3]))
                {
                    if (objcet.transform.position.x.Equals(0) & objcet.transform.position.y.Equals(0))
                        point4 = true;
                }

                // sprite5 位置 x:0f, y:3.4f;
                if (objcet.GetComponent().sprite.Equals(jiugongge.sprite[4]))
                {
                    if (objcet.transform.position.x.Equals(0) & objcet.transform.position.y.Equals(3.4f))
                        point5 = true;
                }

                // sprite6 位置 x:0f, y:6.8f;
                if (objcet.GetComponent().sprite.Equals(jiugongge.sprite[5]))
                {
                    if (objcet.transform.position.x.Equals(0) & objcet.transform.position.y.Equals(6.8f))
                        point6 = true;
                }

                // sprite7 位置 x:3.4f, y:0f;
                if (objcet.GetComponent().sprite.Equals(jiugongge.sprite[6]))
                {
                    if (objcet.transform.position.x.Equals(3.4f) & objcet.transform.position.y.Equals(0))
                        point7 = true;
                }

                // sprite8 位置 x:3.4f, y:3.4f;
                if (objcet.GetComponent().sprite.Equals(jiugongge.sprite[7]))
                {
                    if (objcet.transform.position.x.Equals(3.4f) & objcet.transform.position.y.Equals(3.4f))
                        point8 = true;
                }
            }
            catch
            {
                Debug.LogError("警告2: 強迫語法");
            }
        }

        // point 1-8 正確就可以過關
        if (point1 & point2.Equals(true) & point3.Equals(true) & point4.Equals(true) & point5.Equals(true)
            & point6.Equals(true) & point7.Equals(true) & point8.Equals(true))
            win = true;
        else win = false;
    }
}