0
我將在java中製作迷宮遊戲。我已經做了差不多,但我面臨問題添加分數。我添加了花時間結束遊戲,但它並沒有終止。這是我的代碼。請幫我打印總時間.....在用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");
}
我不認爲油漆是你想要或正在嘗試作出這些決定的地方。如果使用通用狀態標誌並評估/設置其他位置,則更好。可以使用類似於javax.swing.Timer的東西來觸發超時(或者甚至勾選超時) – MadProgrammer