近期热门
粉丝4
关注 0
获赞 2
Unity3D 贪食蛇小游戏Demo[3D版]-准备工作(一)

[U3D] Unity3D 贪食蛇小游戏Demo[3D版]-准备工作(一)

[复制链接]
2002 3 0 0 7年前 举报
     我们知道,学习是一个循序渐进的过程,所以我们通过做一些小游戏,继而能熟悉灵活使用Unity,而且我们也许还能在游戏开发中找到一点自信。   
     好吧!今天小编为大家分享一下,如何做一个简单的贪吃蛇的小项目。有兴趣一起学习交流的同学可以加我的群:575561285

    1.新建一个Unity3d 项目,并且把需要的图片和资源导入到项目中。
图1.png

    2.新建一个Menu.unity 场景,作为游戏的一个开始菜单。
图2.png

   3.实现菜单开始游戏/退出游戏的功能,新建MenuController.cs 菜单控制类。
图3.png
      
     MenuController.cs 代码如下:
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;

  3. public class MenuController : MonoBehaviour {
  4.         void Start () {
  5.         //垂直同步数(VSyncs数值需要在每帧之间传递,使用0为不等待垂直同步。值必须是0,1或2。) :让显卡的运算和显示器刷新率一致以稳定输出的画面质量
  6.         QualitySettings.vSyncCount = 0;
  7.         Application.targetFrameRate = 60;
  8.     }


  9.     public void Start3DGame() {
  10.         //保存数据到内存中
  11.         PlayerPrefs.SetInt("3dMode", 1);
  12.         PlayerPrefs.SetInt("AppleCount", 10);
  13.         //加载到游戏场景
  14.         SceneManager.LoadScene("Main");
  15.     }

  16.     public void ExitGame() {
  17.         //退出游戏
  18.         Application.Quit();
  19.     }
  20. }
复制代码
4.菜单按钮关联类相关的方法。
图4.png

    5.到这一步为止,我们的UI菜单简单完成,然后需要开始我们游戏的主体。新建一个Main.unity 作为贪食蛇的游戏场景。
图5.png

    6.我们知道贪食蛇的背景地图都是一个一个小方块组成的,所以我们需要准备一个GridCube.prefab,并且对象附加GridCube.cs 组件。
图6.png

      GridCube.cs 代码如下:
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;

  4. public class GridCube : MonoBehaviour {

  5.     private CubeState currentState = CubeState.EMPTY;
  6.     public CubeSide cubeSide = 0;

  7.     [Flags]
  8.     public enum CubeSide {
  9.         FRONT = 1,
  10.         BACK = 2,
  11.         TOP = 4,
  12.         BOTTOM = 8,
  13.         LEFT = 16,
  14.         RIGHT = 32
  15.     }

  16.     public enum Direction {
  17.         UP, DOWN, LEFT, RIGHT, NONE
  18.     }

  19.     public enum CubeState {
  20.         SNAKE, APPLE, EMPTY, HOLE
  21.     }

  22.     public void AddCubeSide(CubeSide s) {
  23.         cubeSide |= s;
  24.     }

  25.     public bool SameSideAs(GridCube other) {
  26.         return (other.cubeSide & cubeSide) != 0;
  27.     }

  28.     public void SetCubeState(CubeState state) {
  29.         Renderer ren = GetComponent<MeshRenderer>();
  30.         currentState = state;

  31.         switch (state) {
  32.             case CubeState.SNAKE:
  33.                 ren.material.color = Color.blue;
  34.                 break;
  35.             case CubeState.APPLE:
  36.                 ren.material.color = Color.red;
  37.                 break;
  38.             case CubeState.HOLE:
  39.                 ren.material.color = Color.black;
  40.                 break;
  41.             case CubeState.EMPTY:
  42.             default:
  43.                 ren.material.color = Color.grey;
  44.                 break;
  45.         }
  46.     }

  47.     public bool IsApple() {
  48.         return currentState == CubeState.APPLE;
  49.     }

  50.     public bool IsHole() {
  51.         return currentState == CubeState.HOLE;
  52.     }

  53.     public bool IsSnake() {
  54.         return currentState == CubeState.SNAKE;
  55.     }

  56.     public bool isEmpty() {
  57.         return currentState == CubeState.EMPTY;
  58.     }

  59.     public GridCube GetNextCube(Direction dir, out bool changedSide) {
  60.         changedSide = false;
  61.         Vector3 direction;

  62.         switch (dir) {
  63.             case Direction.UP:
  64.                 direction = new Vector3(0, 1, 0);
  65.                 break;
  66.             case Direction.DOWN:
  67.                 direction = new Vector3(0, -1, 0);
  68.                 break;
  69.             case Direction.LEFT:
  70.                 direction = new Vector3(-1, 0, 0);
  71.                 break;
  72.             case Direction.RIGHT:
  73.                 direction = new Vector3(1, 0, 0);
  74.                 break;
  75.             default:
  76.                 return null;
  77.         }

  78.         GridCube neighbour = GetNeighbourAt(direction);
  79.         if (neighbour == null) {
  80.             // Get neighbour on the other side of the cube (back)
  81.             changedSide = true;
  82.             return GetNeighbourAt(new Vector3(0, 0, 1));
  83.         }

  84.         return neighbour;
  85.     }

  86.     private GridCube GetNeighbourAt(Vector3 direction) {
  87.         RaycastHit hit;
  88.         Ray ray = new Ray(transform.position, direction);
  89.         if (Physics.Raycast(ray, out hit)) {
  90.             GameObject go = hit.collider.gameObject;
  91.             return go.GetComponent<GridCube>();
  92.         }

  93.         return null;
  94.     }
  95.        
  96.         // Update is called once per frame
  97.         void Update () {
  98.        
  99.         }
  100. }
复制代码
好吧!因为这个游戏的内容,稍微有点多,所以决定分成两篇文章展现。欲想知后续,请关注下一篇详解。

   


0
点赞
0
打赏
0
添加到收藏夹

0

点击复制链接

使用微信扫码分享
一次扣10个券
全部评论3
您需要登录后才可以回帖 登录

Eitdion 发表于 2016-12-19 16:33
资源甚好,且阅且珍惜!

好资源啊

7年前
回复

使用道具 举报

资源甚好,且阅且珍惜!
7年前
回复

使用道具 举报

看了楼主的帖子,我只想说一句很好很强大!
7年前
回复

使用道具 举报

您当前使用的浏览器IE内核版本过低会导致网站显示错误

请使用高速内核浏览器或其他浏览器