重写的小游戏4.0

把之前做的小游戏的重新写了下,新改动有:

  • 增加了level2里的原路返回时间加速和指针加速,原理就是在钥匙的两遍放了两个空物体,设置变量a记录两个空物体情况初始值为0,player触碰到左边的空物体a++,碰到右边的a--;只有a!=0时,时间才会加速,指针转速变快。
  • 新增了游戏结束的背景显示,给最后的文字添加了个框框~
  • 背景秒钟的声音本来挂在player上的,运行后发现太影响操作流畅了,可能因为player一直在移动,音乐也跟着移动就掉帧了,所以把这个秒钟的声音改挂在Camera上了,测试过很流畅;本来秒钟的声音是一直持续的,改成游戏结束后就停止。
  • 改了改整体代码的结构。因为只有一个页面的游戏,就把变量都设在一个脚本里了,果然被班里大佬嘲笑说我写的代码是超超超强耦合T T。又想重写╮(╯﹏╰)╭
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.SceneManagement;



public class player : MonoBehaviour
{

    public GameObject key1;
    public GameObject key2;
    public GameObject key3;
    public GameObject level1BG;
    public GameObject level2BG;
    public GameObject level3BG;
    public GameObject door1;
    public GameObject door2;
    public GameObject door3;
    public GameObject opendoor1;
    public GameObject opendoor2;
    public GameObject opendoor3;
    public GameObject clock;
    public GameObject pointer;
    public GameObject dot;
    public GameObject playing;
    public GameObject rewinding;
    public GameObject Black;
    public GameObject again;
    public GameObject introduce;
    public GameObject check1;
    public GameObject check2;

    public bool level1;
    public bool level2;
    public bool level3;
    public bool pointer1;
    public bool pointer2;
    public bool pointer3;
    public bool gameEnd = false;
    public bool rewind = false;
    public bool finalend = false;
    public bool finalend1 = false;
    public bool change=false;
    public bool IsKey = true;
    public bool pointer2change = false;


    public float spendtime1=0;
    public float spendtime2=0;
    public float spendtime3=0;
    public float desTime;
    public float Time1 = Time.deltaTime;


    private List<Vector3> path = new List<Vector3>();
    public int pathTotal;
    public int path1;
    public int path2;
    public int path3;
    public int i;
    public int a=0;
    public int level2speed1=70;
    public int level2speed2=140;


  //  public AudioSource BGMusic;
    public AudioClip keyMusic;
    public AudioClip endingMusic;
    public bool finalmusic = true;

    public static int level = 0;
    private void Start()
    {

        level1instantiate();//初始化level;

    }

    private void Update()
    {
        pathTotal = path.Count;

        pointerSpeed();

        if (rewind == false && gameEnd == true&&finalend == false)
        {
            rewindingBack();//倒退
        }

       else if(rewind==true&&gameEnd==true&&finalend==false)
        {
            playinggBack();//回放画线
        }

        if (finalend == true&&finalend1==false)
        {
            ending();//结束
        }
       
    }



    private void FixedUpdate()
    {
        move();

        if (level1 == true)
            level1Data();//监测leve1是否超时

        if (level2==true)
            level2Data();//监测leve2是否超时

        if (level3 == true)
            level3Data();//监测leve3是否超时



    }



    private void OnTriggerEnter2D(Collider2D other)//碰撞检测
    {
        if (other.tag == "yaoshi")
        {
            Destroy(other.gameObject);//销毁钥匙
            AudioPlay(keyMusic);//音效
            Instantiate(opendoor1, new Vector3((float)-5.33, (float)-1.53, 0), Quaternion.Euler(0, 0, 0));//生成开门icon
        }

        if (other.tag == "opendoor1")//触碰到level1的开门icon
        {
            level1 = false;
            pointer1 = false;
            pointer2 = true;
            level2 = true;

            Destroy(GameObject.Find("biaopan1(Clone)"));//销毁level1下面的表盘
            Destroy(GameObject.Find("pointer(Clone)"));
            this.transform.GetComponent<Transform>().position = new Vector3((float)-0.05, (float)0.5, 0);//人物移动到level2位置
            level2instantiate();//调用level2的生成函数。
            spendtime1 = 0;
            path1 = path.Count;//记录level1的数组长度
        }

        if (other.tag == "key2")
        {
            Destroy(other.gameObject);//销毁钥匙
            AudioPlay(keyMusic);//音效
            Instantiate(opendoor2, new Vector3((float)-0.09, (float)0.58, 0), Quaternion.Euler(0, 0, 0));//生成开门icon
        }
        if (other.tag == "opendoor2")
        {
            level2 = false;
            level3 = true;
            pointer2 = false;
            pointer3 = true;

            Destroy(GameObject.Find("biaopan1(Clone)"));//销毁level2下面的表盘
            Destroy(GameObject.Find("pointer(Clone)"));
            this.transform.GetComponent<Transform>().position = new Vector3((float)3.87, (float)0.62, 0);//人物移动到level3位置
            level3instantiate();//调用level3的生成函数。
            spendtime2 = 0;
            path2 = path.Count;
        }

        if (other.tag == "key3")
        {
            Destroy(other.gameObject);//销毁钥匙
            AudioPlay(keyMusic);//音效
            Instantiate(opendoor3, new Vector3((float)6.37, (float)0.76, 0), Quaternion.Euler(0, 0, 0));//生成开门icon
        }

        if (other.tag == "opendoor3")
        {
            level3 = false;
            gameEnd = true;
            pointer3 = false;

            i = pathTotal;
            GameObject bp = GameObject.FindGameObjectWithTag("biaopan1");
            Destroy(bp);
            Destroy(GameObject.Find("pointer(Clone)"));
            Instantiate(rewinding, new Vector3((float)0.06, -4, 0), Quaternion.Euler(0, 0, 0));
            Time.timeScale = 0;
        }

        if (other.tag == "check1")
        {
            Destroy(other.gameObject);
            a++;//左侧监测物体
        }
        if (other.tag == "check2")
        {
            Destroy(other.gameObject);
            a--;//右侧监测物体
        }

    }






    private void move()//移动函数
    {
        double movev = 0;
        double moveh = 0;
        if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
        {

            if (Input.GetKey(KeyCode.UpArrow))
            {
                movev = movev + Time.deltaTime * 5;
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                movev -= Time.deltaTime * 5;

            }
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                moveh -= Time.deltaTime * 5;

            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                moveh += Time.deltaTime * 5;

            }
            this.transform.Translate(new Vector3((float)moveh, (float)movev, 0));
            path.Add(this.transform.position);//路径存储
        }
    }


    private void level1Data()//监测level1是否超时
    {
        spendtime1 += Time.deltaTime;//记录level1消耗时间

        if (spendtime1 > 2)
        {
            level1Destory();
            desTime = spendtime1;
            if (desTime >= 2.3)
            {
                level1instantiate();
                desTime = 0;
                spendtime1 = 0;
            }
        }
    }

    private void level2Data()//监测level2是否超时
    {
        if(a==0)
        {
            spendtime2 += Time.deltaTime;
        }
        else
        {
            spendtime2 += Time.deltaTime* (float)2;//倒走时间的情况下记录加快
        }

        if (spendtime2>=5)
        {
            level2Destory();
            desTime = spendtime2;
            if (desTime >= 5.3)
            {
                level2instantiate();
                IsKey = true;
                change = false;
                desTime = 0;
                spendtime2 = 0;
                a = 0;
            }
        }
    }

    private void level3Data()//监测level3是否超时
    {
        spendtime3 += Time.deltaTime;
        pointer.transform.Rotate(Vector3.back * 180 * Time.deltaTime, Space.Self);
        if (spendtime3 > 3)
        {
            level3Destory();
            desTime = spendtime3;
            if (desTime >= 3.3)
            {
                level3instantiate();
                desTime = 0;
                spendtime3 = 0;
            }
        }
    }


    private void pointerSpeed()//指针转速控制
    {
        if (pointer1 == true)
        {
            GameObject zz = GameObject.FindGameObjectWithTag("zhizhen1");
            zz.transform.Rotate(Vector3.back * 180 * Time.deltaTime, Space.Self);
        }
        if (pointer2 == true)
        {
            GameObject zz = GameObject.FindGameObjectWithTag("zhizhen1");
            if (a==0)
            {
                zz.transform.Rotate(Vector3.back * level2speed1 * Time.deltaTime, Space.Self);//只有在两个监测物块都销毁,a才为0,说明玩家没有倒着走。
            }
            else
            {
                zz.transform.Rotate(Vector3.back * level2speed2 * Time.deltaTime, Space.Self);//倒走则指针加速
            }
            
        }
        if (pointer3 == true)
        {
            GameObject zz = GameObject.FindGameObjectWithTag("zhizhen1");
            zz.transform.Rotate(Vector3.back * 120 * Time.deltaTime, Space.Self);
        }
    }



    private void level1instantiate()//level1生成
    {
        level1 = true;
        pointer1 = true;

        Instantiate(level1BG, new Vector3((float)-5.07, (float)0.3, 0), Quaternion.Euler(0, 0, 0));//生成背景图
        Instantiate(key1, new Vector3((float)-5.35, (float)0.02, 0), Quaternion.Euler(0, 0, 0));//生成钥匙
        Instantiate(door1, new Vector3((float)-5.32, (float)-1.54, 0), Quaternion.Euler(0, 0, 0));//生成门
        Instantiate(clock, new Vector3((float)-5.255, (float)-3.185, 0), Quaternion.Euler(0, 0, 0));//生成表盘        
        Instantiate(pointer, new Vector3((float)-5.25, (float)-3.18, 0), Quaternion.Euler(0, 0, 0));//生成指针

    }

    private void level1Destory()//level1销毁
    {
        this.transform.GetComponent<Transform>().position = new Vector3((float)-5.27, (float)1.27, 0);
        Destroy(GameObject.Find("level1(Clone)"));
        Destroy(GameObject.Find("yaoshi(Clone)"));
        Destroy(GameObject.Find("door1(Clone)"));
        Destroy(GameObject.Find("biaopan1(Clone)"));
        Destroy(GameObject.Find("pointer(Clone)"));
        Destroy(GameObject.Find("door(Clone)"));
        path.Clear();//清空存储的路径
    }



    private void level2instantiate()//level2生成
    {
        level2 = true;


        Instantiate(level2BG, new Vector3((float)0.01, (float)0.52, 0), Quaternion.Euler(0, 0, 0));//生成背景图
        Instantiate(key2, new Vector3((float)-0.04, (float)-1.61, 0), Quaternion.Euler(0, 0, 0));//生成钥匙
        Instantiate(door2, new Vector3((float)-0.09, (float)0.58, 0), Quaternion.Euler(0, 0, 0));//生成门
        Instantiate(clock, new Vector3((float)-0.05, (float)-3.08, 0), Quaternion.Euler(0, 0, 0));//生成新表盘       
        Instantiate(pointer, new Vector3((float)-0.05, (float)-3.08, 0), Quaternion.Euler(0, 0, 0));//生成指针
        Instantiate(check1, new Vector3((float)-0.86, (float)-1.25, 0), Quaternion.Euler(0, 0, 0));//监测块
        Instantiate(check2, new Vector3((float)0.64, (float)-1.27, 0), Quaternion.Euler(0, 0, 0));
    }
    private void level2Destory()//level2销毁
    {
        this.transform.GetComponent<Transform>().position = new Vector3((float)-0.05, (float)0.5, 0);
        Destroy(GameObject.Find("level2(Clone)"));
        Destroy(GameObject.Find("yaoshi2(Clone)"));
        Destroy(GameObject.Find("door1(Clone)"));
        Destroy(GameObject.Find("biaopan1(Clone)"));
        Destroy(GameObject.Find("pointer(Clone)"));
        Destroy(GameObject.Find("kaimen2(Clone)"));
        Destroy(GameObject.Find("check1(Clone)"));
        Destroy(GameObject.Find("check2(Clone)"));
        path.RemoveRange(path1, pathTotal - path1);//清除路径
    }




    private void level3instantiate()//level3生成
    {
        level3 = true;

        Instantiate(level3BG, new Vector3((float)5.04, (float)0.03, 0), Quaternion.Euler(0, 0, 0));//生成背景图
        Instantiate(key3, new Vector3((float)5.06, (float)-1.62, 0), Quaternion.Euler(0, 0, 0));//生成钥匙
        Instantiate(door3, new Vector3((float)6.37, (float)0.76, 0), Quaternion.Euler(0, 0, 0));//生成门
        Instantiate(clock, new Vector3((float)5.19, (float)-3.08, 0), Quaternion.Euler(0, 0, 0));//生成新表盘       
        Instantiate(pointer, new Vector3((float)5.19, (float)-3.08, 0), Quaternion.Euler(0, 0, 0));//生成指针

    }

    private void level3Destory()//level3销毁
    {
        this.transform.GetComponent<Transform>().position = new Vector3((float)3.87, (float)0.62, 0);
        Destroy(GameObject.Find("level3(Clone)"));
        Destroy(GameObject.Find("yaoshi3(Clone)"));
        Destroy(GameObject.Find("door1(Clone)"));
        Destroy(GameObject.Find("biaopan1(Clone)"));
        Destroy(GameObject.Find("pointer(Clone)"));
        Destroy(GameObject.Find("kaimen3(Clone)"));
        path.RemoveRange(path2, pathTotal - path2);//清除路径
    }




    public void AudioPlay(AudioClip clip)
    {
        AudioSource.PlayClipAtPoint(clip, transform.position);
    }



    private void rewindingBack()//倒放操作
    {
        if (i <= 0)
        {
            rewind = true;//倒放结束
            return;
        }

        else
        {
            i--;
            transform.position = path[i];

        }
    }




    private void playinggBack()//回放操作
    {
        Destroy(GameObject.FindGameObjectWithTag("rewinding"));

        if (finalmusic == true)//设置音乐播放只调用一次
        {
            AudioPlay(endingMusic);
            Instantiate(playing, new Vector3((float)2.1, (float)-3.82, 0), Quaternion.Euler(0, 0, 0));//为什么没有生成? 噢 生成了 被destroy了  
            finalmusic = false;
            GameObject c = GameObject.FindGameObjectWithTag("MainCamera");
            AudioSource BGMusic =c.GetComponent<AudioSource>();
            BGMusic.Stop();
        }

        if (i >= pathTotal)
        {
            finalend = true;//游戏结束
            return;

        }

        else
        {
            i++;
            transform.position = path[i];
            Quaternion qua = Quaternion.Euler(0, 0, 0);
            Instantiate(dot, path[i], qua);
        }
    }





    private void ending()//结束处理
    {
        Instantiate(again, new Vector3((float)7.34, (float)-4.33, 0), Quaternion.Euler(0, 0, 0));
        Instantiate(introduce, new Vector3((float)-7.27, (float)-4.33, 0), Quaternion.Euler(0, 0, 0));
        Destroy(GameObject.FindGameObjectWithTag("playing"));
        Instantiate(Black, new Vector3((float)0.03, (float)0.3, 0), Quaternion.Euler(0, 0, 0));
        finalend1 = true;//设置只被调用一次
    }

}
压缩包(Windows系统)链接:https://pan.baidu.com/s/1dMF0D7E3hYKBe0a5QmE6Lw
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值