簡體   English   中英

如何在我的游戲中添加高分系統?

[英]How do I add a highscore system to my game?

我正在嘗試在我的飛碟射擊游戲中實施高分系統。 這一切都在控制台中運行。 我需要游戲用玩家的名字保存高分,所以我假設我需要將它們放入一個數組中。 不過,我不確定如何將分數和名稱添加到我當前代碼中的數組中。

public class Target {

public static int score = 0;
public static int i = 0;
public static String gameStart;

    public void TargetInfo(Player l) {

        Scanner scan = new Scanner(System.in);

        //Loop for all 25 targets
        for(i=0; i<25; i++) {

        System.out.println("\nhit Enter to shoot");
        gameStart = scan.nextLine();

        if(gameStart.equals("")){

            int random1 = (int) (Math.random() * 105 + 15);

        //Distance 15-35ft
        if (random1 >= 15 && random1 <= 35) {

            System.out.println("Your target is " + random1 + "ft away");

            int random2 = (int) (Math.random() * 5 + 1);

            if(random2<=3) {

                score++;
                System.out.println("You got one!");
            }
            else{

                System.out.println("You missed");
            }

        }
            //Distance 36-75ft
            else if (random1 >=36 && random1 <=75) {

                System.out.println("Your target is " + random1 + "ft away");

                int random3 = (int) (Math.random() * 21 + 1);

                if (random3 <= 11) {

                    score++;
                    System.out.println("You got one!");
                }
                else{

                    System.out.println("You missed");
                }

            }
            //Target Distance 76-105ft
            else if (random1 >=76 && random1 <=105){

                System.out.println("Your target is " + random1 + "ft away");

                int random4 = (int) (Math.random() * 11 + 1);

                    if (random4 <= 2) {

                        score++;
                        System.out.println("You got one!");
                    }
                        else{

                            System.out.println("You missed");
                }

            }

    }
}

        System.out.println(l.name + ", your score is: " + score);


    }

}

您可以通過多種方式執行此操作,例如添加整數數組和名稱數組或哈希表。 就個人而言,我更喜歡這種方式:

Hashtable<String, Integer> highScores = new Hashtable<String, Integer>();

通過這種方式,您可以將分數與特定玩家相關聯。 這比為每個數組維護一個索引更容易。

每場比賽結束時,檢查當前分數是否至少高於您的Hashtable中最低的最高分數,然后您可以更新它。 這是文檔。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM