围住神经猫小游戏制作源码与文件

1.Game Controller:

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

public class GameController : MonoBehaviour
{
    public GameObject pot1;
    public GameObject pot2;

    public int columnNum = 7;
    public int rowNum = 7;

    public GameObject cat;

    ArrayList potArr;

    public GameObject startScreen;
    public GameObject vvvvvv;

    public GameObject fa;
    public GameObject re;

    bool started = false;
    bool gameOver = false;

    ArrayList pot2Arr;
    // Use this for initialization
    void Start()
    {
        pot2Arr = new ArrayList();
        potArr = new ArrayList();
        for (int rowIndex = 0; rowIndex < rowNum; rowIndex++)
        {
            ArrayList tmp = new ArrayList();
            for (int columnIndex = 0; columnIndex < columnNum; columnIndex++)
            {
                //GameObject o = Instantiate(pot1) as GameObject;
                //o.transform.parent = this.transform;
                //Item item = o.GetComponent();
                ////item.rowIndex = rowIndex;
                ////item.columnIndex = columnIndex;
                //item.Goto(rowIndex, columnIndex);
                //item.game = this;
                Item item = CreatePot(pot1, rowIndex, columnIndex);
                tmp.Add(item);
            }
            potArr.Add(tmp);
        }
        //Catmove(4, 4);
    }

    // Update is called once per frame
    public void StartGame()
    {
        started = true;
        startScreen.SetActive(false);

        gameOver = false;
        vvvvvv.SetActive(false);
        fa.SetActive(false);
        re.SetActive(false);


        cat.SetActive(true);
        Catmove(Random.Range(3, rowNum - 3), Random.Range(3, columnNum - 3));

        //reset pot1 moveable to ture
        for (int rowIndex = 0; rowIndex < rowNum; rowIndex++)
        {
            for (int columnIndex = 0; columnIndex < columnNum; columnIndex++)
            {
                Item item = GetPot(rowIndex, columnIndex);
                item.movable = true;
            }
        }
        for (int i = 0; i < pot2Arr.Count; i++)
        {
            Item pot2 = pot2Arr[i] as Item;
            Destroy(pot2.gameObject);
        }
        pot2Arr = new ArrayList();
    }




    Item GetPot(int rowIndex, int columnIndex)
    {
        if (rowIndex < 0 || rowIndex > rowNum - 1 || columnIndex < 0 || columnIndex > columnNum - 1)
        {
            return null;
        }
        ArrayList tmp = potArr[rowIndex] as ArrayList;
        Item item = tmp[columnIndex] as Item;
        return item;
    }



    void Catmove(int rowIndex, int columnIndex)
    {
        Item item = cat.GetComponent();
        item.Goto(rowIndex, columnIndex);
    }

    public void Select11(Item item)
    {
        if (!started || gameOver)
        {
            return;
        }
        //Debug.Log("item");
        // Catmove(item.rowIndex, item.columnIndex);
        if (item.movable)
        {
            Item pottt = CreatePot(pot2, item.rowIndex, item.columnIndex);
            pot2Arr.Add(pottt);
            item.movable = false;
            ArrayList steps = FindSteps();
            Debug.Log(steps.Count);
            if (steps.Count > 0)
            {
                int index = Random.Range(0, steps.Count);  //steps.count是个随机二维数组  (5,3)
                Vector2 v = (Vector2)steps[index];
                Catmove((int)v.y, (int)v.x);
                if (pao())
                {
                    gameOver = true;
                    Debug.Log("PAO");
                    fa.SetActive(true);
                    re.SetActive(true);
                }
            }
            else
            {
                gameOver = true;

                Debug.Log("OK");
                vvvvvv.SetActive(true);
                re.SetActive(true);

            }



        }
    }
    bool pao()
    {
        Item item = cat.GetComponent();
        int rowIndex = item.rowIndex;
        int columnIndex = item.columnIndex;
        if (rowIndex == 0 || rowIndex == rowNum - 1 || columnIndex == 0 || columnIndex == columnNum - 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    Item CreatePot(GameObject pot, int rowIndex, int columnIndex)
    {
        GameObject o = Instantiate(pot) as GameObject;
        o.transform.parent = this.transform;
        Item item = o.GetComponent();
        //item.rowIndex = rowIndex;
        //item.columnIndex = columnIndex;
        item.Goto(rowIndex, columnIndex);
        item.game = this;
        return item;
    }




    bool Movable(Vector2 v)
    {
        Item item = GetPot((int)v.y, (int)v.x);
        if (item == null) return false;
        return item.movable;
    }
    ArrayList FindSteps()
    {

        Item item = cat.GetComponent();
        int rowIndex = item.rowIndex;
        int columnIndex = item.columnIndex;

        ArrayList steps = new ArrayList();
        Vector2 v = new Vector2();

        v.y = rowIndex;
        v.x = columnIndex - 1;
        if (Movable(v)) steps.Add(v);

        v.y = rowIndex;
        v.x = columnIndex + 1;
        if (Movable(v)) steps.Add(v);

        v.y = rowIndex + 1;
        v.x = columnIndex;
        if (Movable(v)) steps.Add(v);

        v.y = rowIndex - 1;
        v.x = columnIndex;
        if (Movable(v)) steps.Add(v);

        v.y = rowIndex + 1;
        if (rowIndex % 2 == 1) v.x = columnIndex - 1;
        else v.x = columnIndex + 1;
        if (Movable(v)) steps.Add(v);

        v.y = rowIndex - 1;
        if (rowIndex % 2 == 1) v.x = columnIndex - 1;
        else v.x = columnIndex + 1;
        if (Movable(v)) steps.Add(v);


        return steps;
    }

}

2.pot,cat身上

 

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

public class Item : MonoBehaviour
{
    public int columnIndex;
    public int rowIndex;
    public float xOff = -3.27f;
    public float yOff = -5.69f;

    public GameController game;
    public bool movable = true;
    // Use this for initialization
    //public gam
    public void UpdatePosition()
    {
        //Vector3 v = new Vector3(1.0f * columnIndex + xOff, 0f + yOff + 1.0f * rowIndex, 0f);
        Vector3 v = new Vector3(0, 0, 0);
        v.x = 1.0f * columnIndex + xOff;
        if (rowIndex % 2 == 0)
        {
            v.x = 1.0f * columnIndex + xOff + 0.5f;
        }
        v.y = 1.0f * rowIndex + yOff;
        transform.position = v;
    }
    public void Goto(int rowIndex, int columnIndex)
    {
        this.rowIndex = rowIndex;
        this.columnIndex = columnIndex;
        UpdatePosition();
    }
    private void OnMouseDown()
    {
        game.Select11(this);
    }
}

3.Startgame

using UnityEngine;
using System.Collections;

public class Startgame : MonoBehaviour
{

    GameController g;

    private void OnMouseDown()
    {
        g = GameObject.Find("Game Controller").GetComponent();
        g.StartGame();
    }
}
https://pan.lanzou.com/i0rae8j

里面有源码,EXE文件

你可能感兴趣的:(自学)