Unity 围住神经猫

来源:互联网 发布:玄武智慧数据科技 编辑:程序博客网 时间:2024/05/02 01:08

Unity 围住神经猫

围住神经猫是一个有趣的小游戏。



我们下面简述一下思路:

1、首先我们生成所有的小圆点。其中随机生成少量不能行走的红色小圆点。把所有可行走的小圆点添加到一个List中。
2、当我们点击可行走的小圆点时,小圆点会变成红色,不可行走。并且从这个List中删除。
3、检测神经猫四周的园点,看是否有圆点存在于我们的List。如果有,则根据神经猫的AI来实现移动。如果没有,则判断游戏胜利。
4、关于神经猫的AI,我们可以获得所有的圆点,根据具体的算法,来实现移动。(我这的移动特别简单。
5、如果神经猫移动到边界,则游戏失败。

    

using UnityEngine;using System.Collections;using System.Collections.Generic;public class GameController : MonoBehaviour {    public static GameController _Instantate;    public GameObject Pot1;    public GameObject Pot2;    public GameObject CatObj;    public int xLine = 9;    public int yLine = 9;    public float xOff = -2f;    public float yOff = -3f;    public List<pot1> potList = new List<pot1>();    public void Init()    {        Debug.Log("---->>>初始化");        int i=0,j=0;        for(i=0;i<9;i++){            for (j = 0; j < 9; j++)            {                if ((i <= 3 || i >= 6) && (j <= 3 || j >= 6))                    //do not creat pot2 in the center                {                    int z = Random.Range(0, 10);                    if (z < 0.5)                    {                        CreatPot(i, j, Pot2);                    }                    else                    {                        CreatPot(i, j, Pot1);                    }                }                else                {                    if (i == 5 && j == 5)                    {                        GameObject cat = Instantiate(CatObj) as GameObject;                        cat.transform.position += new Vector3(i * 0.5f + xOff, j * 0.5f + yOff);                    }                    CreatPot(i, j, Pot1);                }            }        }    }    void Awake()    {        _Instantate = this;    }// Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {}    public void CreatPot(int x, int y, GameObject Pot)    {        Vector2 PotPosition = new Vector3(0,0);        if (y % 2 == 0)        {            PotPosition = new Vector3(x * 0.5f + xOff, y * 0.5f + yOff);        }        else        {            PotPosition = new Vector3(x * 0.5f + xOff + 0.25f, y * 0.5f + yOff);        }                GameObject pot =  Instantiate(Pot) as GameObject;        pot.transform.position = PotPosition;        if (Pot == Pot1)        {            pot.GetComponent<pot1>().setId(x, y);            potList.Add(pot.GetComponent<pot1>());            Debug.Log(potList.Count);        }                pot.transform.parent = this.gameObject.transform;    }}

using UnityEngine;using System.Collections;public class StartGame : MonoBehaviour {    void OnMouseDown()    {        GameController._Instantate.Init();//Init        Destroy(this.gameObject);//destory the bg of start    }    }

using UnityEngine;using System.Collections;public class pot1 : MonoBehaviour {    public int x;    public int y;// Use this for initialization    void OnMouseDown()    {        GameObject pot = Instantiate(GameController._Instantate.Pot2) as GameObject;        //change the pot1 to pot2        pot.transform.position = this.transform.position;        printPot();        delectPot();        cat._Instantate.move();        //set the same position        Destroy(this.gameObject);    }    public void setId(int i, int j)    {        x = i;        y = j;        }    public void delectPot()    {                          GameController._Instantate.potList.Remove(this);                Debug.Log(GameController._Instantate.potList.Count);    }    public void printPot()    {        Debug.Log("--->>>"+x+"  --->>>"+y);    }}

using UnityEngine;using System.Collections;public class cat : MonoBehaviour {    public static cat _Instantate;    public GameObject failed;    public int x = 4;    public int y = 4;    void Awake()    {        _Instantate = this;    }    void Update()    {        if(x==0||y==0||x==8||y==8){            Instantiate(failed);        }    }    public void move()    {        //if (i % 2 == 0)        //{        //    transform.position += new Vector3(x * 0.5f + -2f, y * 0.5f + -3f);        //}        //else        //{        //    transform.position += new Vector3(x * 0.5f + -2f + 0.25f, y * 0.5f + -3f);        //}        if (gotoOne())        {            Debug.Log("--->>>gotoOne");            transform.position += new Vector3(-0.5f, 0);        }        else if (gotoTow())        {            Debug.Log("--->>>gotoTow");            //go to tow            transform.position += new Vector3(0.5f, 0);        }        else if (gotoThree())        {            Debug.Log("--->>>gotoThree");            //go to three            transform.position += new Vector3(-0.25f, -0.5f);        }        else if (gotoFour())        {            Debug.Log("--->>>gotoFour");            //go to four            transform.position += new Vector3(-0.25f, 0.5f);        }        else if (gotoFive())        {            Debug.Log("--->>>gotoFive");            //go to five            transform.position += new Vector3(0.25f, 0.5f);        }        else if (gotoSix())        {            Debug.Log("--->>>gotoSix");            //go to six            transform.position += new Vector3(0.25f, -0.5f);        }        else        {            //game win            Debug.Log("--->>>game win");        }        Debug.Log("--->>>" + x + "  --->>>" + y);    }    public bool gotoOne()    {        foreach (pot1 temp in GameController._Instantate.potList)        {            if (temp.x == (x-1) && temp.y == y)            {                setId(temp.x, temp.y);                return true;            }        }        return false;    }    public bool gotoTow()    {        foreach (pot1 temp in GameController._Instantate.potList)        {            if (temp.x == (x + 1) && temp.y == (y))            {                setId(temp.x, temp.y);                return true;            }        }        return false;    }    public bool gotoThree()    {        foreach (pot1 temp in GameController._Instantate.potList)        {            if (temp.x == (x - 1) && temp.y == (y-1))            {                setId(temp.x, temp.y);                return true;            }        }        return false;    }    public bool gotoFour()    {        foreach (pot1 temp in GameController._Instantate.potList)        {            if (temp.x == (x - 1) && temp.y == (y+1))            {                setId(temp.x, temp.y);                return true;            }        }        return false;    }    public bool gotoFive()    {        foreach (pot1 temp in GameController._Instantate.potList)        {            if (temp.x == (x ) && temp.y == (y + 1))            {                setId(temp.x, temp.y);                return true;            }        }        return false;    }    public bool gotoSix()    {        foreach (pot1 temp in GameController._Instantate.potList)        {            if (temp.x == (x) && temp.y == (y - 1))            {                setId(temp.x, temp.y);                return true;            }        }        return false;    }    public void setId(int i,int j){        x=i;        y=j;    }}

===================================================================================
结束。

0 0
原创粉丝点击