【问题标题】:How to add a score system in java for an android game如何在Java中为Android游戏添加评分系统
【发布时间】:2015-04-28 13:59:42
【问题描述】:

我有一个游戏,它有一个开始/停止按钮。游戏的目的是看看你是否能数到 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);
    }

};

【问题讨论】:

    标签: java android timer


    【解决方案1】:

    您可以实施一个评分系统,在该系统中,用户在恰好 10 秒时点击最多可获得 10,000 分,而随着时间差的增大,分数会越小。

    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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      • 2018-11-19
      • 2023-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多