2013-04-08 53 views
0

我正在Java中製作迷宮遊戲。我做了一個迷宮板,一個起點和一個終點。當我到達終點時,它會退出並顯示一條獲勝消息。但我不能添加時間限制。假設玩家必須在30秒內到達終點,否則他會失去比賽。請幫幫我。 這裏是我的代碼,到目前爲止,我已經做了.......如何在使用java的迷宮遊戲中添加時間限制

Maze.java

package Maze; 

import javax.swing.JFrame; 

public class Maze { 
    public static void main(String args[]) 
    { 
     new Maze(); 

    } 
    public Maze() 
    { 
     JFrame f= new JFrame(); 
     f.setTitle("Maze Game"); 
     f.add(new Board()); 
     f.setSize(460,480); 
     f.setLocationRelativeTo(f); 
     f.setVisible(true); 
     f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
} 

Board.java

package Maze; 

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 

// @SuppressWarnings("serial") 
@SuppressWarnings("serial") 
public class Board extends JPanel implements ActionListener 
{ 
    private Timer timer; 
    private Map m; 
    private Player p; 
    private boolean win=false; 

    long startTime = System.currentTimeMillis(); 
    long elapsedTime; 

    //private String Message=""; 
    //private Font font=new Font("Serif",Font.BOLD,50); 

    public Board() 
    { 
     long elapsedTime = System.currentTimeMillis() - startTime; 
     elapsedTime=elapsedTime/1000; 
     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")) 
     { 
      //Message="WINNER"; 
      win=true; 
     } 
     if(elapsedTime>=5) 
      win=true; 

     repaint(); 
    } 
    public void paint(Graphics g) 
    { 
     super.paint(g); 
     if(!win) 
     { 
     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); 
     } 

     if(win) 
     { 
      g.drawImage(m.getWinn(), 32, 32, null); 
      // g.setColor(Color.ORANGE); 
      //g.setFont(font); 
      //g.drawString(Message, 150, 200); 
     } 
    } 
    public class Al extends KeyAdapter{ 

     public void keyPressed(KeyEvent e){ 
      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 );  
       } 

      } 
     } 
     /* public void keyRealeased(KeyEvent e){ 

     } 
     public void keyTyped(KeyEvent e){ 

     }*/ 
    } 
} 

Map.java

package Maze; 

import java.awt.*; 
import java.io.*; 
import java.util.*; 

import javax.swing.ImageIcon; 

public class Map { 
    private Scanner m; 
    private String Map[]=new String[14]; 

    private Image grass,wall,finish,winn; 

    public Map(){ 

     ImageIcon img = new ImageIcon("C://project//7.jpg"); 
     grass = img.getImage(); 
     img = new ImageIcon("C://project//2.jpg"); 
     wall = img.getImage(); 
     img=new ImageIcon("C://project//hell.gif"); 
     finish=img.getImage(); 
     img=new ImageIcon("C://project//12.jpg"); 
     winn=img.getImage(); 

     openfile(); 
     readfile(); 
     closefile(); 
    } 
    public Image getGrass() 
    { 
     return grass; 
    } 
    public Image getWall() 
    { 
     return wall; 
    } 
    public Image getFinish() 
    { 
     return finish; 
    } 
    public Image getWinn() 
    { 
     return winn; 
    } 

    public String getMap(int x, int y){ 
     String index=Map[y].substring(x, x+1); 
     return index; 

    } 

    public void openfile(){ 
     try { 
      m = new Scanner(new File("C://project//Map.txt")); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      //e.printStackTrace(); 
      System.out.println("Error loading file."); 
     } 
    } 
    public void readfile(){ 
     while(m.hasNext()){ 
      for(int i=0;i<14;i++){ 
       Map[i]=m.next(); 
      } 
     } 
    } 
    public void closefile(){ 
    m.close(); 
} 
} 

Player.java

package Maze; 

import java.awt.Image; 

import javax.swing.ImageIcon; 

public class Player { 

    private int tilex,tiley; 
    private Image player; 
    public Player(){ 
     ImageIcon img=new ImageIcon("C://project//5990.gif"); 
     player=img.getImage(); 

     tilex=1; 
     tiley=1; 

    } 

    public Image getPlayer(){ 
     return player; 
    } 


    public int getTileX(){ 
     return tilex; 
    } 
    public int getTileY(){ 
     return tiley; 
    } 
    public void move(int dx, int dy){ 


     tilex += dx; 
     tiley += dy; 

    } 
} 

,這裏是.txt文件

的map.txt

wwwwwwwwwwwwww 
wggggggwgggggw 
wggwwggwgwwggw 
wwgggwwwggwggw 
wgwgggggggwwgw 
wgggwggwwwgggw 
wgggwgggwggwww 
wggwggwwwggggw 
wgwwgggggwwggw 
wgggggwwwgwggw 
wggwggggwgwwgw 
wwwwgwwwwggwgw 
wggggwgfgggggw 
wwwwwwwwwwwwww 
+2

這是太多的代碼。嘗試只顯示問題的相關代碼部分。 – Austin 2013-04-08 08:40:03

回答

0

你可以做一個變通,添加一個計時器類將在x秒執行,並保持在計算總秒一個變量,當超出限制時,您可以停止您的程序。

+0

你能推薦一個特定的計時器嗎? – trashgod 2013-04-08 11:08:33

+0

如果您想修改GUI:http://docs.oracle.com/javase/6/docs/api/javax/swing/Timer.html – 2013-04-08 12:23:22