Java迷宫 - 只播放路线可见

时间:2016-03-29 15:22:23

标签: java

我创建了一个从文本文件上传的迷宫,该文件目前在运行时完全可见且功能正常。 但是我想仅将播放的路线显示为可见,因此在开始时仅显示起始位置和周围的墙/地板。 有人知道怎么做吗?

以下是董事会课程, (我也有一个Map,Player和Maze课程,如果你需要它们,请告诉我):

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.*;

import javax.swing.*;

public class Brett extends JPanel implements ActionListener {

private Timer timer;

private Kart m;

private Spiller p;

private boolean win = false;

private String Melding = " ";

private Font font = new Font("Serif", Font.BOLD, 48);

public Brett(){
    m = new Kart();
    p = new Spiller();
    addKeyListener(new Al());
    setFocusable(true);
    timer = new Timer(25, this);
    timer.start();
}

public void actionPerformed(ActionEvent e){
    if(m.getKart(p.getTileX(), p.getTileY()).equals("f")){
        Melding = "Vinner";
        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.getKart(x, y).equals("f")){
                g.drawImage(m.getMål(), x * 32, y * 32, null);
            }
            if(m.getKart(x,y).equals("g")){
                g.drawImage(m.getGulv(), x * 32, y * 32, null);
            }
            if(m.getKart(x,y).equals("w")){
                g.drawImage(m.getVegg(), x * 32, y * 32, null);
            }}
        }
        g.drawImage(p.getSpiller(), p.getTileX() * 32, p.getTileY() * 32, null);
    }

    if(win){
    g.setColor(Color.MAGENTA);
    g.setFont(font);
    g.drawString(Melding, 150, 300);
    }
}

public class Al extends KeyAdapter{
    public void keyPressed(KeyEvent e){
        int keycode = e.getKeyCode();

        if(keycode == KeyEvent.VK_W){
            if(!m.getKart(p.getTileX(), p.getTileY() - 1).equals("w")){
            p.move(0, -1);}
        }
        if(keycode == KeyEvent.VK_S){
            if(!m.getKart(p.getTileX(), p.getTileY() + 1).equals("w")){
            p.move(0, 1);}
        }
        if(keycode == KeyEvent.VK_A){
            if(!m.getKart(p.getTileX() -1, p.getTileY() ).equals("w")){
            p.move(-1, 0);}
        }
        if(keycode == KeyEvent.VK_D){
            if(!m.getKart(p.getTileX() +1 , p.getTileY()).equals("w")){
            p.move(1, 0);}
        }
    }

    public void keyReleased(KeyEvent e){

    }

    public void keyTyped(KeyEvent e){

    }

}
}

1 个答案:

答案 0 :(得分:2)

这是一个开始:VisibilityMap实现一个密集的2D布尔数组,首先在起始位置初始化可见的邻居p

private Kart m;
private VisibilityMap visibilities;
...
m = new Kart();
p = new Spiller();
visibilities = new VisibilityMap(m.getTileSizeX(), m.getTileSizeY());
visibilities.updateVisibilityAround(p.getTileX(), p.getTileY());

不要忘记更新keyPressed中新到达位置周围的可见度,并检查visibilities.get(x, y)中的paint以查看是否已发现图块。这是实施:

public class VisibilityMap {
    public final int sizeX, sizeY;
    private boolean[] map;

    public VisibilityMap(int sizeX, int sizeY) {
        assert sizeX >= 0 && sizeY >= 0: "invalid arguments";
        this.sizeX = sizeX;
        this.sizeY = sizeY;
        map = new boolean[sizeX * sizeY];
    }

    public boolean get(int x, int y) {
        if(x < 0 || sizeX <= x || y < 0 || y <= sizeY)
            return false;
        return map[y * sizeX + x];
    }

    public boolean set(int x, int y, boolean value) {
        if(x < 0 || sizeX <= x || y < 0 || sizeY <= y)
            return false;
        map[y * sizeX + x] = value;
        return true;
    }

    public void updateVisibilityAround(int x, int y) {
        boolean valid = set(x, y, true);
        assert valid: "position out of bounds!";
        set(x + 1, y, true);
        set(x - 1, y, true);
        set(x, y + 1, true);
        set(x, y - 1, true);
    }
}