测手速小游戏

来源:互联网 发布:手机版财务记账软件 编辑:程序博客网 时间:2024/06/13 09:40
看到傅园慧女神的洪荒之力,心中也充满了洪荒之力,顺势做一个测手速的小游戏,虽然做的有点渣。谁让我既不会美术又不会动画,图还得自己做、动画还得自己截,差点折腾死我。
下面展示(轻吐槽,我知道做得难看):
再说一下:资源、图、动画啥的就不要太认真了,我费了九牛二虎之力也只这个水平了。(PS:大部分时间就和这些杠上了,没美工真可怕)

首先,第一个scene一张图说明下一下:
 
这个scene的脚本我都不想贴,就这两东西
[C#] 纯文本查看 复制代码
?
 
1
2
3
4
5
6
7
publicvoid IStart()
    {
        SceneManager.LoadScene("Scene1");
    }
    publicvoid IExit()
    {
        Application.Quit();
    }


第二个scene也先来一张图,清晰明了:
这部分使用了两块代码,分别是相机平滑跟随player的followTarget.cs和Move.cs。
其中的第二个脚本虽然用最通俗的if来写的,但是包含了移动的加速减速、动画的播放停止、秒计时器、数据存储等诸多方面,列出来看看吧。
[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
usingUnityEngine;
usingSystem.Collections;
 
publicclass followTarget : MonoBehaviour {
 
    publicfloat smoothing = 3;
    privateTransform player;
 
    // Use this for initialization
    voidStart()
    {
        //player = GameObject.FindGameObjectWithTag(Tags.player).transform;
        player = GameObject.Find("swimmer").transform;
    }
 
    // Update is called once per frame
    voidFixedUpdate()
    {
        Vector3 targetPos = player.position + newVector3(0, 0, -6);
        transform.position = Vector3.Lerp(transform.position, targetPos, smoothing * Time.deltaTime);
    }
}


[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI;
usingUnityEngine.SceneManagement;
 
publicclass Move : MonoBehaviour {
 
    publicfloat speed = 1;
    floatspeed1 = 0;
    floati = 0;
    intm = 0;//时间
    intn = 0;//点击次数
    Vector2 ToPos = newVector2(1,1);
    GameObject player;
 
    Text indexClick;
    Text indexTime;
 
    boolifSave = true;
    //动画组件
    privateAnimator mAnim;
 
        // Use this for initialization
        voidStart () {
        player = GameObject.Find("swimmer");
        indexClick = GameObject.Find("Canvas").transform.Find("clickText").GetComponent<Text>();
        indexTime = GameObject.Find("Canvas").transform.Find("timeText").GetComponent<Text>();
 
        mAnim = GetComponent<Animator>();
        mAnim.SetBool("idle",true);
        mAnim.SetBool("swim",false);
        }
         
        // Update is called once per frame
        voidUpdate () {
 
        if(Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.X))
        {
            speed1 += 0.9f;
            n++;
            indexClick.text = "点 击 次 数:" + n.ToString();
 
            mAnim.SetBool("swim",true);
            mAnim.SetBool("restart",true);
        }
        elseif(speed1 > 0)
        {
            speed1 -= 0.09f;
            mAnim.SetBool("swim",false);
            mAnim.SetBool("restart",false);
        }
        else
        {
            speed1 = 0;
            mAnim.SetBool("stop",true);
        }
        GetComponent<Rigidbody2D>().MovePosition(transform.position + newVector3(1, 0, 1) * speed1 * Time.deltaTime);
 
        //秒计时器
        if(i >= 1.0f)
        {
            i = 0;
            m++;
            indexTime.text = "用时(秒):"+ m.ToString();
            //print(m);
        }
        i += Time.deltaTime;
 
        //数据存储
        if(player.transform.position.x > 8.4f && ifSave == true)
        {
            PlayerPrefs.SetInt("indexClick", n);
            PlayerPrefs.SetInt("indexTime", m);
            ifSave = false;
            SceneManager.LoadScene("Scene2");
        }
         
        }
}


第三部分就更简单了,取一下第二个scene存的数据,然后显示出来就行了。本来想加个排行榜,然后想了下,这个再说吧,先不浪费时间了。
如图:
代码也很简洁:
[C#] 纯文本查看 复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
usingUnityEngine;
usingSystem.Collections;
usingUnityEngine.UI;
usingUnityEngine.SceneManagement;
 
publicclass showFinal : MonoBehaviour {
 
    intindexTime;
    intindexClick;
 
    Text ClickText;
    Text TimeText;
 
        // Use this for initialization
        voidStart () {
        indexClick = PlayerPrefs.GetInt("indexClick");
        indexTime = PlayerPrefs.GetInt("indexTime");
        ClickText = GameObject.Find("Canvas").transform.Find("clickText").GetComponent<Text>();
        TimeText = GameObject.Find("Canvas").transform.Find("timeText").GetComponent<Text>();
        ClickText.text = "点 击 次 数:" + indexClick.ToString();
        TimeText.text = "用时(秒):"+ indexTime.ToString();
        }
         
        // Update is called once per frame
        voidUpdate () {
         
        }
 
    publicvoid Restart()
    {
        SceneManager.LoadScene("Scene1");
    }
    publicvoid Exit()
    {
         Application.Quit();
    }
}

可以发布成安卓的,也可以发布成pc的,pc的按X键或者鼠标加速,小伙伴们,爆发你的洪荒之力吧(傅女神借我点洪荒之力呗)。
原创粉丝点击