• 大小: 11.49MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-06
  • 语言: 其他
  • 标签: unity  小游戏  

资源简介

利用unity实现的贪吃蛇。

资源截图

代码片段和文件信息

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {
    public KeyCode key_up;
    public KeyCode key_down;
    public KeyCode key_left;
    public KeyCode key_right;//这样写是为了,可以在外面的界面里,随时改变控制按键
    
    public bool flag;

    public float speed_canshu;
    private Vector2 speed;
    private Vector2 weizhi= new Vector2(0 0);
    private Vector3 weizhi_3D;
    public List weizhi_jilu=new List();
    public int ball_number = 0;
    public bool flag_game=true;//判断游戏结束

    private Listject> ball = new Listject>();
    private string fangxiang=“123“;//记录方向,防止“回头”动作
    
    public  Rigidbody2D rigidbody2D;

    // Use this for initialization
    void Start ()
    {
        rigidbody2D = GetComponent();
        speed_canshu = 0.15f;
        flag = false;
        Debug.Log(“ball:“ + flag);

    }

// Update is called once per frame
void Update () {
       
        if(flag_game)
        {
            if (Input.GetKey(key_up) && fangxiang != “down“)
            {
                speed = new Vector2(0 speed_canshu);
                fangxiang = “up“;
            }
            else if (Input.GetKey(key_down) && fangxiang != “up“)
            {
                speed = new Vector2(0 -speed_canshu);
                fangxiang = “down“;
            }
            else if (Input.GetKey(key_right) && fangxiang != “left“)
            {
                speed = new Vector2(speed_canshu 0);
                fangxiang = “right“;
            }
            else if (Input.GetKey(key_left) && fangxiang != “right“)
            {
                speed = new Vector2(-speed_canshu 0);
                fangxiang = “left“;
            }
            else
            {
                //位置移动的预先处理
            }

            if (ball_number == 0) fangxiang = “all_might“;//当只有一个球时所有方向都可以.


            weizhi += speed;
            weizhi_3D = new Vector3(0 0 0);
            weizhi_3D.x = weizhi.x;
            weizhi_3D.y = weizhi.y;
            rigidbody2D.transform.SetPositionAndRotation(weizhi_3D new Quaternion(0 0 0 0));



            //Debug.Log(weizhi_jilu.Count);
            for (int i = 0; i < ball_number; i++)
            {
                ball[i].transform.SetPositionAndRotation(weizhi_jilu[10 * (i + 1)] new Quaternion(0 0 0 0));
            }
            weizhi_jilu.Insert(0 weizhi_3D);//以三维向量的形式来记录
        }
    }
    
    private void OnCollisionEnter2D(Collision2D col)
    {
        if(flag_game)//判断游戏是不是完结了
        {
            if (col.gameobject.tag == “Ball_body“|| col.gameobject.tag == “Wall“)//判断是否接触到的蛇身,是的话,把判断游戏结束的变量flag_game设置为flase
            {
                flag_game = false;
            }
            if (col.gameobject.tag == “Ball_new“)//判断是否接触到的果子(ball_new)
            {
                if(ball_number!=0)//如果是第一个蛇身,就不把他修改掉。
           

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\.git\
     文件          40  2017-10-21 15:56  贪吃蛇\2D Train\.git\COMMIT_EDITMSG
     文件         652  2017-10-21 15:56  贪吃蛇\2D Train\.git\config
     文件          73  2017-10-21 15:56  贪吃蛇\2D Train\.git\description
     文件          23  2017-10-21 15:56  贪吃蛇\2D Train\.git\HEAD
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\.git\hooks\
     文件         478  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\applypatch-msg.sample
     文件         896  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\commit-msg.sample
     文件         189  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\post-update.sample
     文件         424  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\pre-applypatch.sample
     文件        1642  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\pre-commit.sample
     文件        1239  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\prepare-commit-msg.sample
     文件        1348  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\pre-push.sample
     文件        4951  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\pre-rebase.sample
     文件         544  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\pre-receive.sample
     文件        3610  2017-10-21 15:56  贪吃蛇\2D Train\.git\hooks\update.sample
     文件         225  2017-10-21 15:56  贪吃蛇\2D Train\.git\index
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\.git\info\
     文件         240  2017-10-21 15:56  贪吃蛇\2D Train\.git\info\exclude
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\.git\logs\
     文件         183  2017-10-21 15:56  贪吃蛇\2D Train\.git\logs\HEAD
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\.git\logs\refs\
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\.git\logs\refs\heads\
     文件         183  2017-10-21 15:56  贪吃蛇\2D Train\.git\logs\refs\heads\master
     文件         150  2017-10-21 15:57  贪吃蛇\2D Train\.git\ms-persist.xml
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\.git\objects\
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\.git\objects\1f\
     文件         751  2017-10-21 15:56  贪吃蛇\2D Train\.git\objects\1f\f0c423042b46cb1d617b81efb715defbe8054d
     目录           0  2017-10-27 20:15  贪吃蛇\2D Train\.git\objects\3c\
     文件        2112  2017-10-21 15:56  贪吃蛇\2D Train\.git\objects\3c\4efe206bd0e7230ad0ae8396a3c883c8207906
............此处省略740个文件信息

评论

共有 条评论