0

我正在用 Java 制作迷宫游戏。我做了一个迷宫板,一个起点和一个终点。当我到达终点时,它退出并显示获胜信息。但我不能添加时间限制。假设玩家必须在 30 秒内到达终点,否则他输掉比赛。请帮我。这是我到目前为止所做的代码......

迷宫.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){

         }*/
     }
 }

地图.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();
}
}

播放器.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文件

地图.txt

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

1 回答 1

0

你可以做一个工作,添加一个计时器类,它将在 x 秒执行并继续计算变量中的总秒数,当超过限制时你可以停止你的程序。

于 2013-04-08T08:58:21.043 回答