unity贪吃蛇c 语言代码,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 snakeBody = new List();
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 tmpList = new List ();//记录身体变化前的位置
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 snakeBody = new List();
// 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 tmpList = new List ();
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);
}
}
将该脚本挂载在头部对象上然后添加身体和食物预设体,再添加边界就行了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持乐购源码。
unity贪吃蛇c 语言代码,unity实现简单贪吃蛇游戏相关推荐
- C语言贪吃蛇游戏代码,贪吃蛇C语言代码实现大全
一.C语言贪吃蛇代码实现前言 设计贪吃蛇游戏的主要目的是让大家夯实C语言基础,训练编程思维,培养解决问题的思路,领略多姿多彩的C语言. 贪吃蛇是非常经典的一款游戏,本次我们模拟在控制台实现贪吃蛇游戏, ...
- linux贪吃蛇c语言代码,C语言贪吃蛇讲解及源码
{ set_cursor_position(0, i); printf("■"); set_cursor_position(N+2, i); printf("■" ...
- 贪吃蛇python语言代码_Python贪吃蛇简单的代码
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 在自学Python的过程中在网上查询资料时发现了一些好玩的东西,python的游戏库模块,它可以自己弄一个小游戏来玩玩,然后我在网上找了一些游戏的代码,, ...
- 简单贪吃蛇c语言代码,一个C语言写简单贪吃蛇源代码.doc
一个C语言写简单贪吃蛇源代码 #include #include #include #include #include #include int grade=5,point=0,life=3; voi ...
- java弹球游戏代码_Java实现简单的弹球游戏
本文实例为大家分享了Java实现简单的弹球游戏的具体代码,供大家参考,具体内容如下 该程序主要是用于对java图形化界面编程进行联系,程序实现全部采用的是AWT包下的类. 程序仅做参考,供学习使用. ...
- 用Python代码做一个简单数字小游戏
#作者是一个十三岁的小男孩. 编辑工具 电脑Python 需要模块 random #今日用代码做一个猜数小游戏 #话不多说,上代码!!! import random number = random.r ...
- 贪吃蛇C语言代码(简单易懂)
//游戏说明; //游戏由基本的C语言和easyx制作而成 //在玩游戏之前,你需要下载一个VS并创建一个空项目 //然后再在源文件里创造一个后缀为.cpp的源文件 //由于easyx(一种绘图语言) ...
- 用200行C语言代码写出一个贪吃蛇——1.0(基本版)
1.设计思路 总的来说,贪吃蛇这个小游戏涉及到的东西不多,但是对逻辑思维是比较吃基本功的. 贪吃蛇,显示给我们看的有三部分:蛇.食物.地图边界. 我们可以用一个二维数组来标记这些部分: 例如这里我创建 ...
- 贪吃蛇c语言代码数组,刚学C语言,想写一个贪吃蛇的代码
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 #include #include #include #include typedef struct snake { int a; int b; stru ...
最新文章
- 基于深度学习的OCR-from 美團技術團隊
- 每天一个linux命令(7):mv命令
- PowerPC中断系统简介(一)
- Vue 之qs 使用详解
- BigDecimal类setScale方法问题:算数异常,精确度丢失-ArithmeticException: Rounding necessary
- PHP秒杀截流原理,节流阀和去抖动的基本实现方法介绍
- 利用JavaScript的字符串操作实现简单查字
- 所有铣床行业调研报告 - 市场现状分析与发展前景预测
- java数据结构银行叫号,数据结构实验二——队列(银行叫号系统)
- c语言指针向前移动i个位置,C语言指针
- android 科技感动画,PPT最炫动画,3分钟学会超有科技感的扫描动画,轰动全场!...
- 企业邮箱为何不能当作邮件群发工具
- 目前网页制作的基本语言html,第二讲网页制作基本语言HTML”.ppt
- 3、(三)外汇学习基础篇之银行间外汇即期交易
- DM达梦数据库集群之分布式集群(MPP)主备
- MySQL数据库表结构的设计
- matlab 矩阵分解
- 【人物专访】FreeICQ的CTO龙云飞[1001]访谈
- 解决Themida加壳程序在VMware虚拟机无法运行问题_HS_TMD
- Soul新发布录音有电流 杂音 m4a文件夹 解决方法
热门文章
- 数据库-MySQL-高级查询-IN通配符LIKE
- mysql 备份100G花费时间_利用xtrabackup 全量备份100G的数据恢复到单实例测试
- 结合 Apache Kafka 生态系统,谈谈2018年机器学习五大趋势
- 如何只用一个小时定制一个行业AI 模型?
- 云小课|网络好不好,ping一下就知道
- 技术实践丨React Native 项目 Web 端同构
- 一文搞懂*argv和**kwargs
- JavaScript基础修炼(14)——WebRTC在浏览器中如何获得指定格式的PCM数据【华为云分享】
- 【华为大咖分享】7.大型云平台的DevOps实践(后附PPT下载地址)
- Android doc译文|Building Apps with Content Sharing|Sharing Simple Data