在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → 神经猫源码(unity)

神经猫源码(unity)

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:19.09M
  • 下载次数:38
  • 浏览次数:691
  • 发布时间:2019-06-13
  • 实例类别:C#语言基础
  • 发 布 人:wujianguo
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 源码

实例介绍

【实例简介】

      游戏形象来源于“围住神经猫”的小游戏。这是一款使用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; //返回值
}

}

标签: 源码

实例下载地址

神经猫源码(unity)

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警