简体   繁体   English

Java游戏程序不重复循环和退出功能?

[英]Java game program not repeating loop and quit function?

I am trying to write a text based projectile game where until the user hits the target, the game loops in increments of rounds. 我正在尝试编写一个基于文本的射弹游戏,在该游戏中,直到用户击中目标,游戏才会以回合为增量循环。 If the user hits the target the game resets. 如果用户击中目标,游戏将重置。 Every round the user will be prompted to exit the game. 每回合都会提示用户退出游戏。

However, right now I am unsure how to make the game continuous as when the user hits the target the game just stops, I'm thinking due to the System.exit(0). 但是,由于System.exit(0),现在我不确定如何使游戏连续进行,因为当用户击中目标时,游戏才停止。 Any ideas to make the program continuous after hitting the target? 有什么想法可以使程序在达到目标后继续运行? (Sorry for the messiness I'm a complete beginner!) (对不起,我是一个完整的初学者!)

import java.util.Scanner;
import java.util.Random;
public class Resttest {
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    Random rn = new Random();
    //Game variables
    int round = 0;
    int score = 0;
    int distance_target = 0; 
    double gravity_constant = 9.8;
    double angle;
    double speed;
    boolean on = false;
    //introduction
    System.out.println("********************************\nWelcome to the Zombie Defenders!  \nYou are one of the last remaining defenders of human kind, armed with nothing but your trusty catapult! \nAre you ready to show those zombies what you're made of? \n(If you prefer a zombie apocalypse, Enter q to quit and any other letter to continue): \n Oh noes! They're coming get ready! ");
    distance_target = rn.nextInt(100); //generates a random number to the the maximum distance of 100

    do{ 
        String w = scanner.nextLine(); 
        if(w.equals("q")){
        System.exit(0);
        }else{
        round += 1;
        System.out.println("Round " + round);
        System.out.println("The target is " + distance_target + " meters away");
        System.out.print("Set the angle(degrees)!: ");
        angle = scanner.nextDouble();
        System.out.print("Set the speed(m/s)!: ");
        speed = scanner.nextDouble();
        //sets variables into equation and calculates the distance
        double distance = ((speed*speed)*(Math.sin(2*angle)))/gravity_constant;

                    if(distance - distance_target >= 20 || distance - distance_target <= -20){//miss and went long 
                        System.out.println(distance);
                        System.out.println("Current Score: " + score);
                        System.out.println("Ouch, that's a miss... went way too far... Were you trying to hit the sky?\n You were " + (distance - distance_target) + " meters off");
                        score -= 1; 
                        System.out.println("(Input q to quit)");
                    }else if(distance-distance_target <= -20){
                        System.out.println(distance);
                        System.out.println("Current Score: " + score);
                        System.out.println("Ouch, that's a miss... went way too short... Who in their right mind thought it was a good idea to let you manage the catapult?\n You were " + (distance - distance_target) + " meters off");
                        score -= 1; 
                        System.out.println("(Input q to quit)");
                    }else if(distance - distance_target >= 5){// close and went long
                        System.out.println(distance);
                        System.out.println("Current Score: " + score);
                        System.out.println("Close but no cigar :/ went right over their heads.\n You were " + (distance - distance_target) + " meters off");
                        System.out.println("(Input q to quit)");
                    }else if(distance-distance_target <= -5){
                        System.out.println(distance);
                        System.out.println("Current Score: " + score);
                        System.out.println("Close but no cigar :/ went down right in front of them. At least you gave them a good scare.\n You were " + (distance - distance_target) + " meters off");
                        System.out.println("(Input q to quit)");
                    }else if (distance - distance_target <= 5 || distance - distance_target >= -5){// hit
                        System.out.println(distance);
                        System.out.println("Current Score: " + score);
                        System.out.println("You got those suckers! Nice shot!");
                        System.out.println("(Input q to quit)");
                        break;
                }
            }
    }while(on != true);
}

} }

Your whole big loop is the logic needed for a single game to take place. 您的整个大循环是进行一场比赛所需的逻辑。 Move that to a new method, and program around it something like: 将其移动到新方法,并围绕它编程,例如:

public static void main(String[] args) {
    boolean play = true;
    while(play){
        play = playTheGame(generateDistance());
    }
    System.out.println("thanks for playing");
}

...

private boolean playTheGame(int distance){
    while(true){
        // your code with System.exit(0); changed to return false;
        // and break; changed to return true; 
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM