gpt4 book ai didi

Java 迷宫 - 仅播放路线可见

转载 作者:行者123 更新时间:2023-11-30 02:59:38 26 4
gpt4 key购买 nike

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

以下是 Board 类,(我还有一个 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){

}

}
}

最佳答案

从这里开始:VisibilityMap 实现一个密集的二维 boolean 数组,首先初始化起始位置的可见邻居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 中新到达位置周围的可见性,并检查 paint 中的 visibilities.get(x, y)查看是否已发现某个图 block 。这是实现:

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);
}
}

关于Java 迷宫 - 仅播放路线可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36288492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com