首页 > 其他 > 详细

排行榜的写法

时间:2018-09-11 17:49:24      阅读:289      评论:0      收藏:0      [点我收藏+]

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

public class BirdController : MonoBehaviour
{
    int score = 0;
    public Text text;
    int max = 0;
    int[] nums = new int[5];
    Button b;

    // Use this for initialization
    void Start()
    {

        text = text.GetComponent<Text>();
        for (int i = 0; i < nums.Length; i++)
        {
            nums[i] = PlayerPrefs.GetInt("max" + i);
        }

    }

    // Update is called once per frame
    void Update()
    {

        text.text = "得分: " + score + "\n最高分:" + max;

        transform.Rotate(Vector3.back * Time.deltaTime * 70);
        if (Input.GetMouseButtonDown(0))
        {
            GetComponent<Rigidbody>().velocity = Vector3.up * Time.deltaTime * 130;
            // transform.rotation=Quaternion.Euler(0,0,30);
            //GetComponent<Rigidbody>().angularVelocity
            transform.eulerAngles = new Vector3(0, 0, 30);
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "mid")//得分
        {
            score += 1;
            Destroy(other.gameObject);
        }
        else
        {
            //排行榜的存入 
            Array.Sort(nums);//从小到大排序
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] < score)//数组里的数字和分数比较,分数比其中任何一个大就赋值
                {
                    nums[0] = score;//此时最小的是数组里的第一个,把新的值赋值给他就跳出
                    break;
                }
            }
            Array.Sort(nums);//再一次排序
            Array.Reverse(nums);//反转
            for (int i = 0; i < nums.Length; i++)//按顺序输出
            {
                PlayerPrefs.SetInt("max" + i, nums[i]);
            }


            Application.LoadLevel(2);
            Time.timeScale = 0;
            // Destroy(gameObject);
        }
    }
}

 

 

 

排行榜的写法

原文:https://www.cnblogs.com/satanj/p/9629088.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!