unity实现简单贪吃蛇游戏
作者:EmberWn 发布时间:2023-05-25 23:59:53
标签:unity,贪吃蛇
本文实例为大家分享了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
0
投稿
猜你喜欢
- C#剪切板Clipboard类我们现在先来看一下官方文档的介绍位于:System.Windows.Forms 命名空间下Provides m
- 简介:本文已一个简要的代码示例介绍ThreadLocal类的基本使用方式,在此基础上结合图片阐述它的内部工作原理。早在JDK1.2的版本中就
- Android Fragment滑动组件ViewPager的实例详解1适配器FragmentPagerAdapter的实现对于Fragmen
- 我就废话不多说了,大家还是直接看代码吧~import com.alibaba.fastjson.JSON;import java.util.
- SessionSession对象用于获取与数据库的物理连接。 Session对象是重量轻,设计了一个互动是需要与数据库每次被实例化。持久化对
- 本文实例讲述了Android编程实现为ListView创建上下文菜单(ContextMenu)的方法。分享给大家供大家参考,具体如下:Con
- 在spring Boot中,有些代码是WEB功能,例如API等,但是有些逻辑是非WEB,启动时就要调用并持续运行的,该如何加载自己的非WEB
- 1.新建控制台应用程序2.新建类 EncryptHelper.cspublic static class EncryptHelper{ &n
- 本文实例讲述了C#可用于登录验证码的四位随机数生成方法。分享给大家供大家参考。具体实现方法如下:这里提供了两种方法,调用其一即可:using
- 公钥与私钥公钥与私钥是成对的,一般的,我们认为的是公钥加密、私钥解密、私钥签名、公钥验证,有人说成私钥加密,公钥解密时不对的。公钥与私钥的生
- 本文实例讲述了C#利用原图和水印图的重叠简单实现水印的方法。分享给大家供大家参考,具体如下:图片操作类/// <summary>
- 各位亲们可以尝试以下代码:注:这里我就只有一个html标签对来说明问题了,首部之类的东西,自己添加。<html> &n
- 前言在RocketMQ中为,我们创建消息生产者时,只需要设置NameServer地址,消息就能正确地发送到对应的Broker中,那么Rock
- 归并排序是利用递归和分而治之的技术将数据序列划分成为越来越小的半子表,再对半子表排序,最后再用递归步骤将排好序的半子表合并成为越来越大的有序
- 涉及access_token的获取请参考《C#微信公众平台开发之access_token的获取存储与更新》一、为了实现高级群发功能,需要解决
- sms4j 2.0 全新来袭即sms-aggregation成功加入dromara之后,很多人向我们反应了项目名称太长不好记,也太绕口, 在
- 1、创建在class块外面:class Test{}/** 我是main入口函数 **/fun main(args: Array<St
- 前言Guava是google公司开发的一款Java类库扩展工具包,内含了丰富的API,涵盖了集合、缓存、并发、I/O等多个方面。使用这些AP
- 先上效果图:如图,可实现设置:总流程数、已完进度程数、已完成颜色,各个标题github地址戳这里使用方法1.导入compile 'c
- 同步是一种只允许一个线程在特定时间访问某些资源的技术。没有其他线程可以中断,直到所分配的线程或当前访问线程访问数据完成其任务。在多线程程序中