我有一个使用图像的游戏,本质上是2D自上而下的公路赛车游戏。当玩家与交通冲突时,屏幕上会出现一个游戏,告诉用户他们的得分。稍后我还将显示高分(以秒为单位的时间),但现在我要从文件中读取分数(我将分数手动放入以进行测试),将分数放入数组,然后将分数输出到文件中。但是,每次游戏结束并检查文件时,文件都是相同的。我怀疑这些方法甚至都没有被调用,但是我实在很沮丧。一些指导将不胜感激。不要与外星人和宇宙飞船相混淆,我使用一个非常基础的游戏作为基础,以此作为我可以学习的平台。(宇宙飞船是玩家,外星人就是交通)
package com.mycompany.samplegame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener{
private Timer stopwatch;
private Timer timer;
public SpaceShip spaceship;
private List<Alien> aliens;
private boolean ingame;
private final int ICRAFT_X = 250;
private final int ICRAFT_Y = 250;
private final int B_WIDTH = 400;
private final int B_HEIGHT = 300;
private final int DELAY = 15;
boolean started=true;
BufferedImage track = null;
BufferedImage display1 = null;
BufferedImage display2 = null;
BufferedImage display3 = null;
BufferedImage display4 = null;
BufferedImage display5 = null;
BufferedImage display6 = null;
BufferedImage display7 = null;
BufferedImage display8 = null;
BufferedImage display9 = null;
int playerIcon=0;
int highscore;
JFrame f = new JFrame("My Game");
//for timer
/*long sec;
long min;
long t;
String secs;
String mins;
JLabel watch = new JLabel("00:00");
String watch2;*/
int secondspassed=0;
//int score=0;
private final int[][] pos = {
/*{2380, 29}, {2500, 59}, {1380, 89},
{780, 109}, {580, 139}, {680, 239},
{790, 259}, {760, 50}, {790, 150},
{980, 209}, {560, 45}, {510, 70},
{930, 159}, {590, 80}, {530, 60},
{940, 59}, {990, 30}, {920, 200},
{900, 259}, {660, 50}, {540, 90},
{810, 220}, {860, 20}, {740, 180},
{820, 128}, {490, 170}, {700, 30}*/
{2380, 50}, {2500, 100}, {1380, 150},
{780, 200}, {580, 250}, {680, 300},
{790, 300}, {760, 350}, {790, 400},
{980, 430}, {560, 100}, {510, 490},
{930, 520}, {590, 550}, {530, 560},
{940, 590}, {990, 620}, {920, 650},
{900, 675}, {660, 700}, {540, 725},
{810, 750}, {860, 775}, {740, 800},
{820, 825}, {490, 850}, {700, 875}
};
public Board() {
initBoard();
}
private void initBoard() {
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.BLACK);
ingame = true;
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
spaceship = new SpaceShip(ICRAFT_X, ICRAFT_Y);
initAliens();
timer = new Timer(DELAY, this);
timer.start();
}
public void initAliens() {
aliens = new ArrayList<>();
for (int[] p : pos) {
aliens.add(new Alien(p[0], p[1]));
}
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (started==true)
{
pregame(g);
}
else if (ingame) {
drawObjects(g);
}
else {
drawGameOver(g);
}
Toolkit.getDefaultToolkit().sync();
}
private void drawObjects(Graphics g) {
secondspassed++;
g.drawImage(track,200,200,null);
if (spaceship.isVisible()) {
g.drawImage(spaceship.getImage(), spaceship.getX(), spaceship.getY(),
this);
}
for (Alien alien : aliens) {
if (alien.isVisible()) {
g.drawImage(alien.getImage(), alien.getX(), alien.getY(), this);
}
}
g.setColor(Color.WHITE);
g.drawString("Points: " + aliens.size(), 5, 15);
g.drawString("Seconds Passed:"+ String.valueOf(secondspassed/60),5,20);
}
private void pregame(Graphics g)
{
String msg = "Your Current HighScore is "+ highscore;
String msg2 = "Pick a car by inputing the assigned number to each car";
String msg3 = "To pick cars 4 to 6 have a score of over 40 seconds!";
String msg4 = "To pick cars 7 to 9 have a score of over 60 seconds";
Font small = new Font("Comic Sans", Font.BOLD, 12);
FontMetrics fm = getFontMetrics(small);
g.drawString(msg, 100, 50);
g.drawString(msg2, 100, 100);
g.drawString(msg3, 100, 150);
g.drawString(msg4, 100, 200);
if (spaceship.ChoiceMade()==-1)
{
drawObjects(g);
}
}
private void drawGameOver(Graphics g)
{
trigger();
String msg = "Game Over! ";
String msg2="You played for " +secondspassed/60+ "seconds";
Font small = new Font("Comic Sans", Font.BOLD, 14);
FontMetrics fm = getFontMetrics(small);
g.setColor(Color.white);
g.setFont(small);
g.drawString(msg, (B_WIDTH - fm.stringWidth(msg)) / 2,
B_HEIGHT / 4);
g.drawString(msg2, (B_WIDTH - fm.stringWidth(msg2)) / 2,
B_HEIGHT / 2);
}
@Override
public void actionPerformed(ActionEvent e) {
inGame();
updatePlayer();
updateAliens();
checkCollisions();
repaint();
}
private void inGame() {
if (!ingame) {
timer.stop();
}
}
private void updatePlayer() {
if (spaceship.isVisible())
{
spaceship.move();
}
if (spaceship.getX()<100)
{
spaceship.x=100;
}
if (spaceship.getX()>300)
{
spaceship.x=300;
}
}
private void updateAliens() {
if (aliens.isEmpty())
{
ingame = false;
return;
}
for (int i = 0; i < aliens.size(); i++)
{
Alien a = aliens.get(i);
if ((secondspassed/60)==20)
{
a.Faster(1);
spaceship.SpeedIncrease(1);
}
if ((secondspassed/60)==40)
{
a.Faster(2);
spaceship.SpeedIncrease(2);
}
if ((secondspassed/60)==60)
{
a.Faster(3);
spaceship.SpeedIncrease(3);
}
if (a.getY()>500)
{
a.setY(-10);
a.newX();
}
if (a.isVisible()) {
a.move();
} else {
aliens.remove(i);
}
}
}
public void checkCollisions()
{
Rectangle r3 = spaceship.getBounds();
for (Alien alien : aliens)
{
Rectangle r2 = alien.getBounds();
if (r3.intersects(r2))
{
spaceship.setVisible(false);
alien.setVisible(false);
ingame = false;
}
}
}
private class TAdapter extends KeyAdapter
{
@Override
public void keyReleased(KeyEvent e)
{
spaceship.keyReleased(e);
}
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == 49) //ascii of 1
{
spaceship.ChangeCar(1);
playerIcon=1;
started=false;
ingame=true;
}
if (e.getKeyCode() == 50) //ascii of 2
{
spaceship.ChangeCar(1);
playerIcon=1;
started=false;
ingame=true;
}
if (e.getKeyCode() == 51) //ascii of 3
{
spaceship.ChangeCar(3);
playerIcon=1;
started=false;
ingame=true;
}
if (e.getKeyCode() == 52) //ascii of 4
{
spaceship.ChangeCar(4);
playerIcon=1;
started=false;
ingame=true;
}
if (e.getKeyCode() == 53) //ascii of 5
{
spaceship.ChangeCar(5);
playerIcon=1;
started=false;
ingame=true;
}
if (e.getKeyCode() == 55) //ascii of 6
{
spaceship.ChangeCar(6);
playerIcon=1;
started=false;
ingame=true;
}
if (e.getKeyCode() == 55) //ascii of 7
{
spaceship.ChangeCar(7);
playerIcon=1;
started=false;
ingame=true;
}
if (e.getKeyCode() == 56) //ascii of 8
{
spaceship.ChangeCar(8);
playerIcon=1;
started=false;
ingame=true;
}
if (e.getKeyCode() == 57) //ascii of 9
{
spaceship.ChangeCar(9);
playerIcon=1;
started=false;
ingame=true;
}
spaceship.keyPressed(e);
}
}
public void trigger()
{
try {
FileIOScores(secondspassed);
}
catch (Exception e) {
}
}
public void FileIOScores(int secondspassed) throws IOException
{
//Stack sampleStack = new Stack(10);
BufferedReader readFile= new BufferedReader(new FileReader("time.txt"));
String strInput="";
int intRecCount=0;
//counts record
while((strInput=readFile.readLine())!=null)
{
intRecCount++;
}
readFile.close();
int ScoreList[] = new int[intRecCount+1];
int scores;
for (int i=0;i<((ScoreList.length)-2);i++)
{
scores=Integer.parseInt(readFile.readLine());
ScoreList[i]=scores;
}
ScoreList[(ScoreList.length)-1]=(secondspassed/60);
QuickSort(ScoreList, ScoreList.length);
PrintWriter pw = new PrintWriter(new FileWriter("time.txt"));
//input information
for(int i=0; i<ScoreList.length-1; i++)
{
pw.println(ScoreList[i]);
}
}
public static void QuickSort(int numArray[], int arraySize)
{
//Calls on q_sort function
q_sort(numArray, 0, arraySize- 1);
}
public static void q_sort(int numArray[], int left, int right)
{
//Declaration of variables
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numArray[left];
while (left < right)
{
while ((numArray[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numArray[left] = numArray[right];
left++;
}
while ((numArray[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numArray[right] = numArray[left];
right--;
}
}
numArray[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numArray, left, pivot-1);
if (right > pivot)
q_sort(numArray, pivot+1, right);
String sortedText = "";
}
}
我的文件输入输出方法是否被调用?
可以通过添加println语句来快速测试您的方法以查看是否正在调用它。
将System.out.println("Input output method called!");
添加到您的方法中,然后从命令提示符或IDE运行您的应用程序以测试您的方法是否被调用。消息“输入输出方法已调用!”调用该方法时,它将出现在控制台中。要获取其他信息,例如堆栈跟踪,您还可以添加new Exception().printStackTrace();
。
一种更好的调试代码的方法是从IDE运行(以调试模式)程序,并将断点添加到您的方法中。您的程序将在添加断点的行上暂停,并显示有用的信息,例如变量内容和堆栈。