欢迎来到传世资源网!
加载中...
正在加载,请耐心等待...
本站为收藏、学习站,如有侵权,请联系管理员删除!

神经猫源码(unity)

介绍 评论 失效链接反馈

      游戏形象来源于“围住神经猫”的小游戏。这是一款使用Egret(白鹭)引擎开发的游戏,由南京泥巴怪公司开发,一名美术和一名程序员两人仅耗时一天半就完成了。7月22日下午2点游戏上线短短三天,游戏访问量已经过亿次。 神经猫拥有夸张的造型与欠抽的表情,在屏幕中扭来扭去,游戏设定为玩家对其进行毫不留情地围剿。
from clipboard using UnityEngine;
using System.Collections;

public class GameColler : MonoBehaviour
{
public GameObject pot1;
public GameObject pot2;
public GameObject cat;
public int colnum = 9;
public int rownum = 9;
public ArrayList potArr;

//判断游戏是否开始了
private bool started = false;
//游戏结束标志
public bool gameOver = false;

//声明变量
public GameObject startScreen;
public GameObject victory;
public GameObject failed;
public GameObject replay;
public GUIText cishu;

private ArrayList pot2Arr;

//猫的位置恢复可移动状态
private int catii;
private int catjj;

//有效步数 点击
public int clicknum = 0;

// 游戏初始化开始
void Start ()
{
//赋值controller   设置controller 方法  
startScreen.GetComponent<StartGame> ().controller = this;
replay.GetComponent<StartGame> ().controller = this;
//补充创建一个pot1 我不晓得为啥丢失
//Item item110 = CreatePot (pot1, 0, 0);
//初始化pot2Arr
pot2Arr = new ArrayList ();
//初始化POT1数组
potArr = new ArrayList ();

for (int rowIndex=0; rowIndex<rownum; rowIndex ) {
ArrayList tmp = new ArrayList ();//临时二维数组
for (int colIndex = 0; colIndex < colnum; colIndex ) {
Item item = CreatePot (pot1, rowIndex, colIndex);
tmp.Add (item);
}
potArr.Add (tmp);//把tmp添加到数组中  二维数组
}

}
//开始游戏
//写一个点击的方法
public void StartGame ()
{
//重置开始状态
started = true;//设置开始游戏 可以点击单元格
//声明的变量添加到gamecontroller中   指引到
startScreen.SetActive (false);//设置启动界面隐藏

//重置游戏开始状态
gameOver = false;
victory.SetActive (false);
failed.SetActive (false);
replay.SetActive (false);

//重新设置pot1 moveable to true
for (int rowIndex=0; rowIndex<rownum; rowIndex ) {
for (int colIndex = 0; colIndex < colnum; colIndex ) {
Item item = GetPot (rowIndex, colIndex);
item.moveable = true;
}
}

//remove all pot2
for (int i=0; i<pot2Arr.Count; i ) {
Item pot2 = pot2Arr [i]as Item;
//删除对象pot2
Destroy (pot2.gameObject);
}
//从数组中删除 clear
pot2Arr = new ArrayList ();  //放置再次删除的时候报错  初始化赋值

// 首先需要初始化猫的状态
cat.SetActive (true);
bool catsucc = false;
while (catsucc==false) {
int cati = Random.Range (3, rownum - 3);  //首先随机数目少的
int catj = Random.Range (3, colnum - 3);
Item itemcat = GetPot (cati, catj);  //判断猫当前的位置是否可以移动
if (itemcat.moveable) {
MoveCat (cati, catj);
catii = cati;//保留cat的初始化位置
catjj = catj;
catsucc = true;//成功创建猫的位置
itemcat.moveable = false;//不能在猫的脚下创建pot2
}
}

//随机生成pot2 的数量和位置
int cspot2 = Random.Range (12, 14);  //生成pot2的个数
int cspot2num = 0;
while (cspot2num<cspot2) {
//生成行  列
int hang = Random.Range (0, 9);
int lie = Random.Range (0, 9);
Item itempot = GetPot (hang, lie);  //判断当前位置是否可以创建pot2
if (itempot.moveable) {
Item pot2Item0 = CreatePot (pot2, hang, lie) as Item;
pot2Arr.Add (pot2Item0); //pot2加入数组
itempot.moveable = false;  //这里应该是pot1的moveable设置为false
cspot2num = cspot2num 1;
}

}
//把猫的初始化位置设置成可点击事件
Item itemcatsite = GetPot (catii, catjj);  //
itemcatsite.moveable = true;

//初始化点击次数
clicknum = 0;
cishu.text = "点击次数为:" clicknum;

}

//取二维数组  单独写个二维数组的方法
Item GetPot (int rowIndex, int colIndex)
{
//数组不存在或者越界   异常情况
if (rowIndex < 0 || rowIndex > rownum - 1 || colIndex < 0 || colIndex > colnum - 1)
return null;

ArrayList tmp = potArr [rowIndex] as ArrayList;  //先取行  再取列
Item item = tmp [colIndex] as Item;
return item;
}

//移动猫的事件
void MoveCat (int rowIndex, int colIndex)
{
Item item = cat.GetComponent<Item> ();
item.Goto (rowIndex, colIndex);

}


//操作执行事件   创建pot2 判断猫移动
public void Select (Item item)
{
//游戏未开始  则无法点击
if (!started || gameOver)
return;//没有返回值
//Debug.Log ("Select" item);
//MoveCat(item.rowIndex, item.colIndex);

//成功实现cat脚下不可以点击事件 
Item itemcatnow = cat.GetComponent<Item> ();
if (item.rowIndex == itemcatnow.rowIndex && item.colIndex == itemcatnow.colIndex)
return;

if (item.moveable) {
//点击次数 1
clicknum = clicknum 1;
//赋值给GUI TEXT
cishu.text = "点击次数为:" clicknum;

//add pot2 
Item pot2Item = CreatePot (pot2, item.rowIndex, item.colIndex);
pot2Arr.Add (pot2Item); //pot2加入数组
//改后无法走
item.moveable = false;
//s声明一个steps  查看神经猫可找的步骤
ArrayList steps = FindSteps ();
//Debug.Log (steps.Count);
//判断神经猫是否有路可以走   简单的猫的AI判断
if (steps.Count > 0) {
//为啥开始从0 随机 range的取值   0 <=X<6 之间随机????   简单猫的AI
int index = Random.Range (0, steps.Count);
Vector2 v = (Vector2)steps [index];
//移动猫
MoveCat ((int)v.y, (int)v.x);

if (Escaped ()) {

gameOver = true;//游戏结束
//Debug.Log ("神经猫逃跑了");
failed.SetActive (true);//失败界面显示
replay.SetActive (true);

 
}

} else {

gameOver = true;//游戏结束
//Debug.Log ("玩家胜利了。。。胜利了");
victory.SetActive (true);//胜利界面显示
replay.SetActive (true);

}
}
}
//判断神经猫 是否逃脱
bool Escaped ()
{
Item item = cat.GetComponent<Item> ();
int rowIndex = item.rowIndex;
int colIndex = item.colIndex;

if (rowIndex == 0 || rowIndex == rownum - 1 || colIndex == 0 || colIndex == colnum - 1) {
return true;
} else {
return false;
}

}

//判断猫是否可以移动
bool Movable (Vector2 v)
{
Item item = GetPot ((int)v.y, (int)v.x);  //行  列   y 轴对应行的变化   x轴对应列的变化
//返回空则错误

//item 必须存在   找不到就返回空
if (item == null)
return false;

return item.moveable;  //想知道猫是否可以移动

}

//神经猫走的步数
ArrayList FindSteps ()
{
//声明item  取rowIndex colIndex  取猫的行 列 
Item item = cat.GetComponent<Item> ();
int rowIndex = item.rowIndex;
int colIndex = item.colIndex;

//存放猫可以走的步数
ArrayList steps = new ArrayList ();
Vector2 v = new Vector2 ();

//left
v.y = rowIndex;
v.x = colIndex - 1;
if (Movable (v))
steps.Add (v);

//right
v.y = rowIndex;
v.x = colIndex 1;
if (Movable (v))
steps.Add (v);

//top
v.y = rowIndex 1;
v.x = colIndex;
if (Movable (v))
steps.Add (v);

//bottom
v.y = rowIndex - 1;
v.x = colIndex;
if (Movable (v))
steps.Add (v);

//奇数行  topleft   OR  topright
v.y = rowIndex 1;   
if (rowIndex % 2 == 1)//行号  判断奇数行  偶数行
v.x = colIndex - 1;
else
v.x = colIndex 1;
if (Movable (v))
steps.Add (v);

//偶数行  bottomleft  OR  bottomright 
v.y = rowIndex - 1;    
if (rowIndex % 2 == 1)//行号  判断奇数行  偶数行
v.x = colIndex - 1;
else
v.x = colIndex 1;
if (Movable (v))
steps.Add (v);

return steps;

}

//创建pot事件 pot1 pot2
Item CreatePot (GameObject pot, int rowIndex, int colIndex)
{
GameObject o = Instantiate (pot)as GameObject;
o.transform.parent = this.transform;
//o.transform.position = new Vector3 (0.5f * colIndex xoff, 0.5f * rowIndex yoff, 0f);
Item item = o.GetComponent<Item> ();  //拿到Item  把功能点重构了
item.Goto (rowIndex, colIndex); 
//          item.rowIndex = rowIndex;
//          item.colIndex = colIndex;
//          item.UpdatePosition();

item.game = this;  //赋予当前控制属性

return item; //返回值
}

}

下载声明:

本站资源均有第三方用户自行上传分享推荐,非本站自制,仅供玩家做交流学习之用!切勿用于商业用途!游戏作品版权归原作者享有,如有版权问题,请附带版权证明至邮件,本平台将应您的要求删除。
相关推荐:

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复