我将在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");
}
答案 0 :(得分:0)
我猜你想让游戏完成,如果:
win == true
或System.currentTimeMillis() <= (startTime+10000)
如果是这种情况,则为if
条件.-
if (!win && (System.currentTimeMillis() <= (startTime + 10000)))
没关系,但是你的其他分支.-
else if(win)
只是:
else
代替。换句话说:只要玩家没有获胜并且还剩下时间,游戏就会继续。否则(无论玩家是否获胜),显示得分。