unity贪吃蛇c 语言代码,Unity3D开发之贪吃蛇

主要视图

afe82c5aa3c2

Materials

afe82c5aa3c2

Prefabs

afe82c5aa3c2

Scenes

afe82c5aa3c2

Scripts

afe82c5aa3c2

Texture

afe82c5aa3c2

关键代码

FoodSnake

public class FoodSnake : MonoBehaviour {

//定义预置体,方便实例化

public GameObject foodSanke;

//公开食物生成位置

public int xLimit = 30;

public int yLimit = 22;

void Start () {

//调用方法的函数

InvokeRepeating("CreateFood",1,3);

}

void Update () {

}

void CreateFood() {

//实例化食物 游戏对象的自身方法

// GameObject.Instantiate(foodSanke,new Vector2(1,1),Quaternion.identity);

//让位置随机 x=61 y=45;

//GameObject.Instantiate(foodSanke,new Vector2(Random.Range(-30,30),Random.Range(-22,22)),Quaternion.identity);

//方便修改食物生成范围,将位置公开

GameObject.Instantiate(foodSanke,new Vector2(Random.Range(-xLimit,xLimit),Random.Range(-yLimit,yLimit)),Quaternion.identity);

}

}

MoveSnake

using UnityEngine;

using System.Collections;

using System.Collections.Generic;//引用 泛型集合 命名空间

using System.Linq;//引用,命名空间

using UnityEngine.SceneManagement;//转换场景需要的引用

public class MoveSnake : MonoBehaviour {

//定义一个transform类型的变量

private Transform m_Transform;

//定义一个开始的方向 默认向上移动

Vector2 direction = Vector2.up;

//定义一个公开的速度 为之后游戏速度改变做准备

public float velocitytime = 0.5f;

//定义一个公开的body变量,用来存放cube body预置体

public GameObject body;

//定义一个开关,用来检测食物

private bool Open = false;

//将蛇身存入一个集合里去 定义集合 的变量

List snakeBody = new List();

void Start () {

//赋值

m_Transform=gameObject.GetComponent();

//重复调用方法的函数

//InvokeRepeating("Move",0.5f,0.5f);//参数1:移动的方法 参数2:第一次调用的时间 参数3:以后每个0.5f秒调用一次;

InvokeRepeating("Move", 0.5f, velocitytime);

}

void Update () {

//控制方向

if (Input.GetKey(KeyCode.A))

{

direction = Vector2.left;

}

if (Input.GetKey(KeyCode.D))

{

direction = Vector2.right;

}

if (Input.GetKey(KeyCode.W))

{

direction = Vector2.up;

}

if (Input.GetKey(KeyCode.S))

{

direction = Vector2.down;

}

}

///

/// 移动的方法

///

void Move() {

//在移动过程中,判断开关是否打开,打开就创建蛇身;(实例化)位置为本次蛇头的位置

Vector3 VPosition = m_Transform.position; //先执行这一句,保存起来位置,再执行下一句,让它移动

//默认向上移动

m_Transform.Translate(direction);

if (Open)

{

//实例化 蛇身

// GameObject.Instantiate(body,VPosition,Quaternion.identity);

//把实例化的蛇身存起来;

GameObject bodyYuzhi = GameObject.Instantiate(body,VPosition,Quaternion.identity) as GameObject;

//集合插入方法 吃掉食物后在定义的蛇身集合0 号位前面插入实例化后的蛇身

snakeBody.Insert(0,bodyYuzhi.transform);

//检查是否插入成功

// Debug.Log(snakeBody.Count);

//实力一次之后把开关 关掉

// Open = false;

Open = !Open;

}else if (snakeBody.Count > 0)//判断集合里有元素(蛇身)的话 将蛇身跟随蛇头移动的方法

{

//蛇身的位置等于蛇头的位置

snakeBody.Last().position = VPosition;

// Debug.Log(snakeBody.Last().position);

//把最后一个位置从零号位插入;

snakeBody.Insert(0, snakeBody.Last());

//把最后一个元素删除掉;

snakeBody.RemoveAt(snakeBody.Count - 1);//参数:表示固定的下标移除元素;

}

}

//没有刚体 又想触发事件 有两个碰撞器前提下,将蛇改成触发器,添加刚体,不使用重力

//食物是碰撞体就行 ,并且将碰撞体的size改成0.5,否则侧面也会吃掉食物的;

//蛇在移动过程中,检测下触发了谁,就让说消失 把食物的便签改成Food;

///

/// 检测触发方法

///

/// 触发的对象

void OnTriggerEnter(Collider coll) {

if (coll.gameObject.tag == "Food")

{

//触发食物,将开关打开;

// Open = true;

Open = !Open;

//销毁食物

Destroy(coll.gameObject);

}

else {

//如果不是触发食物,触发其他物体,游戏结束;

//切换场景

// SceneManager.LoadScene(0);//场景管理,加载到另一场景 场景就是设置的第一个 在边框加上4个碰撞器 触发其他即游戏重新开始;

//切换第二场景

SceneManager.LoadScene(0);

}

}

}

StartUI

using UnityEngine;

using System.Collections;

using UnityEngine.SceneManagement;//场景管理命名空间

public class StartUI : MonoBehaviour {

// Use this for initialization

void Start () {

}

// Update is called once per frame

void Update () {

if (Input.GetMouseButtonDown(0))

{

//点击回到第一个场景;

SceneManager.LoadScene(1);

}

}

}

后记:没有计分,没有结束界面(因为在滚动球和飞盘射击都已经写过了,这里就不再一一重复了)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值