如何在java中为android游戏添加评分系统

ymdaylpp  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(224)

我有一个游戏,它有一个开始/停止按钮。这个游戏的目的是看你是否能在头脑中数到10。当你按下开始按钮时(不可见)计时器开始,当你按下停止按钮时,它应该停止计时器并在页面上显示你的分数。你越接近10秒,得分就越好。我想知道如何实现一个评分系统,因为它是基于时间的,我不知道如何将它转换成int?下面是使其运行的代码。

private Runnable updateTimerThread = new Runnable() {

    public void run() {

        timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

        updatedTime = timeSwapBuff + timeInMilliseconds;

        int secs = (int) (updatedTime / 1000);
        int mins = secs / 60;
        secs = secs % 60;
        int milliseconds = (int) (updatedTime % 1000);
        timerValue.setText("" + mins + ":"
                + String.format("%02d", secs) + ":"
                + String.format("%03d", milliseconds));
        customHandler.postDelayed(this, 0);
    }

};
oxf4rvwz

oxf4rvwz1#

你可以实现一个评分系统,用户在10秒内点击最多可以得到10000分,而随着时间差的增大,得分会越来越小。

long clickTime = System.currentTimeMillis() - startTime; //time between start and stop clicks
long timeDifference = Math.abs(10000L - clickTime); //absolute time difference between 10 second and the users clicks

int score = 10000; //set maximum score
score = score - (int) clickTime; //subtract the time difference from the score to get actual score

你应该记住使用 System.currentTimeMillis() 找到你的开始时间。如果用户单击时间超过20秒,此方法将给出负分数,因此如果愿意,可以添加另一条语句将其重置为0。

相关问题