unity实现简单贪吃蛇游戏
作者:EmberWn 时间:2023-05-25 23:59:53
本文实例为大家分享了unity实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下
首先创建一个头部,编写脚本利用WASD控制头部的移动。
Vector3 up=new Vector3(0,1,0);
Vector3 down=new Vector3(0,-1,0);
Vector3 left=new Vector3(-1,0,0);
Vector3 right=new Vector3(1,0,0);
Vector3 now;//头部实际前进方向
float timer=0f;
float timerGap=0.1f;
void Start ()
{
now = up;
}
void Update ()
{
if (now!=up&&now!=down&&Input.GetKey (KeyCode.W))
{
now = up;
}
if (now!=up&&now!=down&&Input.GetKey (KeyCode.S))
{
now = down;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.A))
{
now=left;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.D))
{
now = right;
}
timer += Time.deltaTime;
if (timer > timerGap)
{
//每隔0.1s向当前方向移动一个单位(0.5为头部大小)。
timer = 0;
transform.position = 0.5f * now + transform.position;
}
}
然后就是创建初始身体,实现身体跟随头部。采用的方法是将身体放进一个数组,然后下标0的身体移动到头部之前的位置,然后下标 i 的身体移动到 i-1 的position。
创建初始身体,并放入数组。
public GameObject body;//身体预设体
List<GameObject> snakeBody = new List<GameObject>();
void Awake()
{
for (int i = 0; i < 3; ++i)
{
GameObject newbodynext=Instantiate (body,
transform.position-(i+1)*new Vector3(0,0.5f,0),
Quaternion.identity)as GameObject;
snakeBody.Add (newbodynext);
}
}
实现跟随
void Update ()
{
if (now!=up&&now!=down&&Input.GetKey (KeyCode.W))
{
now = up;
}
if (now!=up&&now!=down&&Input.GetKey (KeyCode.S))
{
now = down;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.A))
{
now=left;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.D))
{
now = right;
}
timer += Time.deltaTime;
if (timer > timerGap)
{
Vector3 tmpPosition = transform.position;//记录头部变化前的位置
List<Vector3> tmpList = new List<Vector3> ();//记录身体变化前的位置
for (int i = 0; i < snakeBody.Count; ++i)
{
tmpList.Add (snakeBody [i].transform.position);
}
timer = 0;
transform.position = 0.5f * now + transform.position;
snakeBody [0].transform.position = tmpPosition;//将0移到头部之前的位置
//依次前移身体的位置
for (int i = 1; i < snakeBody.Count; ++i)
{
snakeBody [i].transform.position = tmpList [i - 1];
}
}
}
初始蛇创建好后,就开始添加食物,和增长蛇的身体。还有检测游戏失败,即撞到身体或者边界,采用事件触发检测完成。
创建食物
public GameObject foodPrefab;//食物预设体
void Start () {
now = up;
createFood ();
}
void createFood()
{
float x = Random.Range(-6.5f, 6.5f);
float y = Random.Range(-4.5f, 4.5f);
Instantiate(foodPrefab,new Vector3(x,y,0f),Quaternion.identity);
}
触发检测
void OnTriggerEnter(Collider other)
{ //这个other就是被碰撞体
if (other.gameObject.tag.Equals("Food"))
{
Destroy(other.gameObject);
GameObject newbodynext = Instantiate (body,
snakeBody[snakeBody.Count-1].transform.position,
Quaternion.identity)as GameObject;
snakeBody.Add (newbodynext);//增加蛇的身体
createFood();
}
else if(other.gameObject.tag.Equals("Body"))
{
SceneManager.LoadScene("Snake", LoadSceneMode.Single);//重新开始
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag.Equals("Boundary"))
SceneManager.LoadScene("Snake", LoadSceneMode.Single);
}
完整代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class HeadMove : MonoBehaviour {
public GameObject body;
public GameObject foodPrefab;
Vector3 up=new Vector3(0,1,0);
Vector3 down=new Vector3(0,-1,0);
Vector3 left=new Vector3(-1,0,0);
Vector3 right=new Vector3(1,0,0);
Vector3 now;
float timer=0f;
float timerGap=0.1f;
List<GameObject> snakeBody = new List<GameObject>();
// Use this for initialization
void Awake()
{
for (int i = 0; i < 3; ++i)
{
GameObject newbodynext=Instantiate (body, transform.position-(i+1)*new Vector3(0,0.5f,0),Quaternion.identity)as GameObject;
snakeBody.Add (newbodynext);
}
}
void Start () {
now = up;
createFood ();
}
void createFood()
{
float x = Random.Range(-6.5f, 6.5f);
float y = Random.Range(-4.5f, 4.5f);
Instantiate(foodPrefab,new Vector3(x,y,0f),Quaternion.identity);
}
// Update is called once per frame
void Update ()
{
if (now!=up&&now!=down&&Input.GetKey (KeyCode.W))
{
now = up;
}
if (now!=up&&now!=down&&Input.GetKey (KeyCode.S))
{
now = down;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.A))
{
now=left;
}
if (now!=left&&now!=right&&Input.GetKey (KeyCode.D))
{
now = right;
}
timer += Time.deltaTime;
if (timer > timerGap)
{
Vector3 tmpPosition = transform.position;
List<Vector3> tmpList = new List<Vector3> ();
for (int i = 0; i < snakeBody.Count; ++i)
{
tmpList.Add (snakeBody [i].transform.position);
}
timer = 0;
transform.position = 0.5f * now + transform.position;
snakeBody [0].transform.position = tmpPosition;
for (int i = 1; i < snakeBody.Count; ++i)
{
snakeBody [i].transform.position = tmpList [i - 1];
}
}
}
void OnTriggerEnter(Collider other)
{ //这个other就是被碰撞体
if (other.gameObject.tag.Equals("Food"))
{
Destroy(other.gameObject);
GameObject newbodynext = Instantiate (body,snakeBody[snakeBody.Count-1].transform.position,Quaternion.identity)as GameObject;
snakeBody.Add (newbodynext);
createFood();
}
//由于身体和头部一开始就接触,所以将身体的碰撞半径减小到0.4
else if(other.gameObject.tag.Equals("Body"))
{
SceneManager.LoadScene("Snake", LoadSceneMode.Single);
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.tag.Equals("Boundary"))
SceneManager.LoadScene("Snake", LoadSceneMode.Single);
}
}
将该脚本挂载在头部对象上然后添加身体和食物预设体,再添加边界即可。
来源:https://blog.csdn.net/blubluhao/article/details/77946116
标签:unity,贪吃蛇
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Java如何实现一个基于LRU时间复杂度为O(1)的缓存
2022-02-02 08:35:36
struts+spring+hibernate三个框架的整合
2022-08-08 08:13:15
如何清空文件夹里面的所有文件和文件夹
2022-09-17 04:11:06
Java 数据结构与算法系列精讲之二叉堆
2022-05-14 06:31:15
![](https://img.aspxhome.com/file/2023/7/64367_0s.gif)
spring cloud consul注册的服务报错critical的解决
2021-05-28 14:13:14
![](https://img.aspxhome.com/file/2023/5/64925_0s.jpg)
C#实现软件监控外部程序运行状态的方法
2023-12-04 20:46:15
flutter图片组件核心类源码解析
2023-09-14 16:29:00
![](https://img.aspxhome.com/file/2023/3/108293_0s.jpg)
一文带你认识Java中的Object类和深浅拷贝
2023-02-05 16:52:08
![](https://img.aspxhome.com/file/2023/4/119074_0s.png)
Android中NestedScrolling滑动机制详解
2022-09-16 22:32:13
![](https://img.aspxhome.com/file/2023/9/106059_0s.png)
C# Distinct和重写IEqualityComparer时要知道的二三事
2023-07-11 15:41:20
Android SharedPreferences存取操作以及封装详解
2022-11-02 07:10:19
分布式Netty源码分析EventLoopGroup及介绍
2022-02-12 03:20:51
![](https://img.aspxhome.com/file/2023/9/70289_0s.png)
详解Java高级特性之反射
2021-06-09 14:01:06
Java C++题解leetcode904水果成篮
2023-12-08 21:06:09
![](https://img.aspxhome.com/file/2023/3/89953_0s.png)
Java并发包线程池ThreadPoolExecutor的实现
2022-11-10 09:52:41
![](https://img.aspxhome.com/file/2023/3/64383_0s.jpg)
Compare And Swap底层原理及代码示例详解
2022-10-10 16:06:18
![](https://img.aspxhome.com/file/2023/8/125658_0s.png)
Java运行时数据区域(内存划分)的深入讲解
2023-03-16 02:36:50
![](https://img.aspxhome.com/file/2023/8/94298_0s.png)
Android Studio快捷键生成TAG、Log.x日志输出介绍
2022-03-14 02:10:37
Java接口返回json如何忽略特定属性
2022-03-16 07:54:24
IntelliJ IDEA 下载安装超详细教程(推荐)
2023-11-19 23:50:16
![](https://img.aspxhome.com/file/2023/6/59086_0s.png)