java - 在蛇游戏中为文本文件创建排行榜
问题描述
我是 Java 编程新手,我想做一个迷你蛇游戏。到目前为止,我已经研究了蛇功能的基础知识以及除了排行榜之外的所有内容,我希望在我的游戏中使用这些排行榜来跟踪高分。我看了一些制作蛇游戏的编程视频,我尝试在writeFile中添加“,true”来记录当前的高分,但我只能记录2个分数,以前的高分和当前的高分。之后,如果我在游戏中达到第三个高分,我会收到错误消息。如何附加或如何修改此代码?
//this is for the score in the user interface
public String getHighScore() {
FileReader readFile = null;
BufferedReader reader = null;
try {
readFile = new FileReader("highscore.txt");
reader = new BufferedReader(readFile);
return reader.readLine();
} catch (Exception e) {
return "Nobody:0";
}
finally {
try {
if(reader != null)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//this is the method i created to record the highscore in game to text file
public void checkScore() {
if(hs.equals(""))
return;
if(appleEaten > Integer.parseInt((hs.split(":")[1]))) {
String name = JOptionPane.showInputDialog("You have set a new highscore. What is your name?");
hs = name + ":" + appleEaten;
File scoreFile = new File("highscore.txt");
if (!scoreFile.exists())
{
try {
scoreFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter writeFile = null;
BufferedWriter writer = null;
try {
writeFile = new FileWriter(scoreFile);
writer = new BufferedWriter(writeFile);
writer.write(this.hs);
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if(writer != null) {
writer.close();
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
}
当我要获得第三个高分时,这是我得到的错误
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "2Stephen"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:660)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at GamePanel.checkScore(GamePanel.java:253)
at GamePanel.checkCollisions(GamePanel.java:154)
at GamePanel.actionPerformed(GamePanel.java:177)
at java.desktop/javax.swing.Timer.fireActionPerformed(Timer.java:310)
at java.desktop/javax.swing.Timer$DoPostEvent.run(Timer.java:242)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:316)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
解决方案
推荐阅读
- node.js - 我无法使用 express js 应用程序将 gcloud mysql 与应用程序引擎连接起来
- github - 自动拒绝更新某些文件的 PR
- java - Visual Studio Code Java:找不到 java.lang.Object 的类文件
- python - 如何在 Python Plotly/Dash Rangeslider 中显示不同位置的工具提示?
- solidity - Safemoon - 需要帮助设置一些参数
- c# - 如何计算数组json中的值
- powershell - 如何列出用户拥有的所有团队
- python-3.x - 如何使用python将数据写入excel并保持excel数字格式?
- android - MutableLiveData NullPointerException 警告
- android - Android Studio 自动关闭