欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

unity实现简单贪吃蛇游戏

程序员文章站 2024-02-04 16:54:10
本文实例为大家分享了unity实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下首先创建一个头部,编写脚本利用wasd控制头部的移动。vector3 up=new vector3(0,1,0);vec...

本文实例为大家分享了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);
  }
}

将该脚本挂载在头部对象上然后添加身体和食物预设体,再添加边界即可。

unity实现简单贪吃蛇游戏

unity实现简单贪吃蛇游戏

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。