Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

ascii art - how would i take the file level1.txt and output it to the console in java?

I want to output the level1 file to the console, but store it in a char array, so that i can change the color of certain icons when it outputs, so # would be brown as an example, My issue is that i can't even output the string, from the scanner, any tips or fixes i can implement?

Also, i would want to be able to implement a x and y axis for the player to move on the txt map, but i don't know how to implement that either.

here is the code for game.java

    import java.io.FileNotFoundException;
    import java.io.File;
    import java.util.Scanner;
    public class game {

    // change weapon to have its own file for certain weapons, make at least 5 different ones with different atributes
    public static String weapon = "fists";
    public static Scanner s = new Scanner(System.in);
    public static char levelLoader[] = new char[100000];
    public static String level = "";
    public static File file = new File("level1.txt");
    
    private static void healthBar() {
        System.out.println(color.GREEN_BOLD_BRIGHT + "Health: " + color.RESET +
        color.RED + player.maxHealth + "/" + player.health + 
        color.RESET + color.GREEN_BOLD_BRIGHT + " XP: " + color.RESET 
        + color.YELLOW + player.maxXp + "/" + player.xp + 
        color.RESET + color.GREEN_BOLD_BRIGHT + " Weapon: " + color.RESET + 
        color.CYAN + weapon + color.RESET);
    }
    
    // starts up game and tells player how to move
    public static void intro(){
        try{startUp.clear();}catch(Exception e){}
        System.out.println("You can move by either pressing W, A, S, D or " +
        "type 'exit' to close the game, your character is the @ ");
        startUp.slow();
        try{startUp.clear();}catch(Exception e){}
        player.initializePlayerAtributes();
    }
    
    public static void paitWalls(){
    }
    
    //prints game board to terminal
    public static void paintGameBoard() throws FileNotFoundException{
        healthBar();
        
        Scanner levelReader = new Scanner(file);
        level = levelReader.nextLine();
        while(levelReader.hasNextLine()){
            level = level + "
" + levelReader.nextLine();
        }
        levelLoader = level.toCharArray();
        for(int i = 0; i > levelLoader.length; i++){
            System.out.println(levelLoader[i]);
        }
        levelReader.close();
    }
    
    // uses w a s d and exit to determine if to move or exit
    public static void playerMovementAndExit(){
        String movementOrPause = s.nextLine();
        switch (movementOrPause.toLowerCase()){
            case "w":
                player.y++;
                try{startUp.clear();}catch(Exception e){}
            break;
            case "s":
                player.y--;
                try{startUp.clear();}catch(Exception e){}
            break;
            case "d":
                player.x++;
                try{startUp.clear();}catch(Exception e){}
            break;
            case "a":
                player.x--;
                try{startUp.clear();}catch(Exception e){}
            break;
            case "exit":
                try{startUp.clear();}catch(Exception e){}
                System.exit(1);
            break;
            default:
                System.out.println("You need to move with W A S D or" +
                " type 'exit' to close");
                startUp.slow();
                try{startUp.clear();}catch(Exception e){}
            break;
            }
    }

    public static void run(){
        intro();
        while(true){
            try {paintGameBoard();} catch (Exception e) {}
            playerMovementAndExit();
        }
    }
}

and here is the level1.txt file

#########################################
#.........#.........#...................#
#...................#...................#
#.........#.........#...................#
###############...###...................#
#.........#.........#...................#
#.......................................#
#.........#.........#...................#
#########################################
question from:https://stackoverflow.com/questions/65856455/how-would-i-take-the-file-level1-txt-and-output-it-to-the-console-in-java

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...