Unity 贪吃蛇2D简易制作(二)
【摘要】
文末源码:
然后开始写代码:
此代码用于动态生成食物,,挂载场景物体上即可(灯光,背景,摄像机)
using UnityEngine; public class FoodDemo : MonoBehaviour { public GameObject SSFood; //把食物的预制体赋值 public int xLimit = 30; //游...
文末源码:
然后开始写代码:
此代码用于动态生成食物,,挂载场景物体上即可(灯光,背景,摄像机)
-
using UnityEngine;
-
-
public class FoodDemo : MonoBehaviour {
-
-
public GameObject SSFood; //把食物的预制体赋值
-
public int xLimit = 30; //游戏边界(最大高度,宽度)
-
public int yLimit = 22;
-
-
// Use this for initialization
-
void Start () {
-
//动态生成食物的时间
-
InvokeRepeating("Food", 1, 5);
-
}
-
-
void Food()
-
{
-
//随机生成食物位置
-
int x = Random.Range(-xLimit, xLimit);
-
int y = Random.Range(-yLimit, yLimit);
-
Instantiate(SSFood, new Vector2(x, y), Quaternion.identity);
-
}
-
}
挂载到蛇身上:,,运行游戏就可以玩耍了,,,
-
using System.Collections.Generic;
-
using System.Linq;
-
using UnityEngine;
-
using UnityEngine.SceneManagement;
-
-
-
//蛇的移动脚本
-
public class Move : MonoBehaviour {
-
List<Transform> Bodylist = new List<Transform>();
-
-
public GameObject Body; //吃到东西添加在后面的物体
-
public bool ISwith = false; //是否吃到
-
-
public float velocitytime = 0.5f; //初始速度
-
-
Vector2 direction = Vector2.up; //初始方向,
-
-
// Use this for initialization
-
void Start () {
-
//第一次调用方法是程序开始0.5秒过后,之后每隔velocitytime秒之后调用一次
-
InvokeRepeating("MoveSnake",0.5f, velocitytime);
-
}
-
-
// Update is called once per frame
-
void Update () {
-
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
-
{
-
direction = Vector2.up;
-
}
-
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
-
{
-
direction = Vector2.left;
-
}
-
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
-
{
-
direction = Vector2.down;
-
}
-
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
-
{
-
direction = Vector2.right;
-
}
-
-
}
-
#region 移动&&核心算法
-
void MoveSnake()
-
{
-
//每执行一次获取一次头部的位置
-
Vector3 vpos = transform.position;
-
//执行移动
-
transform.Translate(direction);
-
-
if (ISwith) //吃到食物
-
{
-
//实例化吃到的食物
-
GameObject Bodyper =(GameObject)Instantiate(Body, vpos, Quaternion.identity);
-
//添加到list的头部
-
Bodylist.Insert(0, Bodyper.transform);
-
ISwith = false;
-
}
-
-
//==============核心算法================
-
if (Bodylist.Count>0)
-
{
-
//最后一个移动到第一个的位置
-
Bodylist.Last().position = vpos;
-
//list里面的元素 进行交换位置,,最后一个物体添加到list的最前面
-
Bodylist.Insert(0, Bodylist.Last());
-
//移除最后一个,,(因为他已经被加入到第一个的位置了)
-
Bodylist.RemoveAt(Bodylist.Count - 1);
-
}
-
//======================================
-
}
-
#endregion
-
-
/// <summary>
-
/// 触发检测
-
/// </summary>
-
/// <param name="other">碰到带物体的名字</param>
-
private void OnTriggerEnter(Collider other)
-
{
-
if (other.tag == "Food") //使用标签
-
{
-
//销毁食物预制体
-
Destroy(other.gameObject);
-
ISwith = true;
-
}
-
else //碰到除了food的物体,重新开始游戏
-
{
-
SceneManager.LoadScene(0);
-
}
-
}
-
}
扫描主页左侧二维码或 V信搜 " 开发同学留步",回复“ 贪吃蛇”获得项目源码。
文章来源: czhenya.blog.csdn.net,作者:陈言必行,版权归原作者所有,如需转载,请联系作者。
原文链接:czhenya.blog.csdn.net/article/details/77916738
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)