我打算用java做一个迷宫游戏。我几乎完成了,但我遇到了添加分数的问题。我已经添加了结束游戏所花费的时间,但它并没有终止。这是我的代码。请帮我打印总时间......
package Maze;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel implements ActionListener
{
private Timer timer;
private Map m;
private Player p;
private boolean win=false;
long startTime=0;
long message=0;
public Board()
{
startTime=System.currentTimeMillis();
m= new Map();
p= new Player();
addKeyListener(new Al());
setFocusable(true);
timer=new Timer(25, this);
timer.start();
}
public void actionPerformed(ActionEvent e)
{
if(m.getMap(p.getTileX(), p.getTileY()).equals("f"))
{
win=true;
message=(startTime+10000)-System.currentTimeMillis();
}
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
if(!win&&(System.currentTimeMillis()<=(startTime+10000)))
{
for(int y=0;y<14;y++)
{
for(int x=0;x<14;x++)
{
if(m.getMap(x,y).equals("f"))
g.drawImage(m.getFinish(), x*32, y*32, null);
if(m.getMap(x, y).equals("w"))
g.drawImage(m.getWall(), x*32, y*32, null);
if(m.getMap(x, y).equals("g"))
g.drawImage(m.getGrass(), x*32, y*32, null);
}
}
g.drawImage(p.getPlayer(), p.getTileX()*32, p.getTileY()*32,null);
}
else if(win)
{
long end = System.currentTimeMillis();
long time=end-startTime;
//String str=Long.toString(time);
JOptionPane.showMessageDialog(null, "Your Score:"+time);
//System.exit(0);
//g.drawImage(m.getWinn(), 32, 32, null);
//JOptionPane.showMessageDialog(this, "Winner");
}
else
{
g.drawImage(m.getLose(), 32, 32, null);
}
}
public class Al extends KeyAdapter{
public void keyPressed(KeyEvent e){
/* long startTime1 = System.currentTimeMillis();
totalTime=(startTime1-startTime)/1000;
JOptionPane.showMessageDialog(null,totalTime); */
int keycode= e.getKeyCode();
if(keycode==KeyEvent.VK_UP )
{
if(!m.getMap(p.getTileX(),p.getTileY()-1).equals("w"))
{
p.move( 0, -1);
}
}
if(keycode==KeyEvent.VK_DOWN )
{
if(!m.getMap(p.getTileX(),p.getTileY()+1).equals("w"))
{
p.move( 0, 1);
}
}
if(keycode==KeyEvent.VK_LEFT )
{
if(!m.getMap(p.getTileX()-1,p.getTileY()).equals("w"))
{
p.move( -1, 0);
}
}
if(keycode==KeyEvent.VK_RIGHT )
{
if(!m.getMap(p.getTileX()+1,p.getTileY()).equals("w"))
{
p.move( 1, 0 );
}
}
}
}
}
这是我的困惑点
else if(win)
{
long end = System.currentTimeMillis();
long time=end-startTime;
// String str=Long.toString(time);
JOptionPane.showMessageDialog(null, "Your Score:"+time);
// System.exit(0);
// g.drawImage(m.getWinn(), 32, 32, null);
//JOptionPane.showMessageDialog(this, "Winner");
}