Unity3D贪吃蛇
?簡單的貪吃蛇游戲,目前存在的缺陷:沒有死亡判定,沒有障礙物,沒有UI,可以按和移動方向相反的按鍵
?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Control1 : MonoBehaviour
{
? ? Vector3 oldPosition;
? ? // Start is called before the first frame update
? ? void Start()
? ? {
? ? ? ? InvokeRepeating("snakeMove", 0.1f, 0.5f);
? ? }
? ? // Update is called once per frame
? ? void Update()
? ? {
? ? }
? ? private void snakeMove()
? ? {
? ? ? ? oldPosition = transform.position;
? ? ? ? float H = Input.GetAxis("Horizontal");
? ? ? ? float V = Input.GetAxis("Vertical");
? ? ? ? if (H < 0)
? ? ? ? {
? ? ? ? ? ? transform.position = transform.position + new Vector3(-2, 0, 0);
? ? ? ? ? ? GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().setPosition(oldPosition);
? ? ? ? ? ? GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().bodyMove();
? ? ? ? }
? ? ? ? if (H > 0)
? ? ? ? {
? ? ? ? ? ? transform.position = transform.position + new Vector3(2, 0, 0);
? ? ? ? ? ? GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().setPosition(oldPosition);
? ? ? ? ? ? GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().bodyMove();
? ? ? ? }
? ? ? ? if (V < 0)
? ? ? ? {
? ? ? ? ? ? transform.position = transform.position + new Vector3(0, 0, -2);
? ? ? ? ? ? GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().setPosition(oldPosition);
? ? ? ? ? ? GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().bodyMove();
? ? ? ? }
? ? ? ? if (V > 0)
? ? ? ? {
? ? ? ? ? ? transform.position = transform.position + new Vector3(0, 0, 2);
? ? ? ? ? ? GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().setPosition(oldPosition);
? ? ? ? ? ? GameObject.Find("SnakeBody1").transform.GetComponent<controlBody>().bodyMove();
? ? ? ? }
? ? }
}
移動的實現(xiàn)代碼
?
總結(jié)
以上是生活随笔為你收集整理的Unity3D贪吃蛇的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RS485通信原理图及程序实例详解
- 下一篇: 【 Grey Hack 】综合工具 sh