如何解决我将如何读取txt文件并将其输出到java中的控制台?
我想将 level1.txt
文件输出到控制台,但将其存储在一个字符数组中,以便我可以在输出时更改某些图标的颜色,因此 #
将是棕色的例如,我的问题是我什至无法从扫描仪输出字符串,我可以实施任何提示或修复吗?
另外,我希望能够实现一个 x
和 y
轴让玩家在 txt
地图上移动,但我不知道如何实现
这是 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 + "\n" + 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();
}
}
}
这是 level1.txt
文件:
#########################################
#.........#.........#...................#
#...................#...................#
#.........#.........#...................#
###############...###...................#
#.........#.........#...................#
#.......................................#
#.........#.........#...................#
#########################################
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。