Java 建造迷宫
起初我认为这很容易,但当我开始这样做时,我不知道如何继续下去。我的想法是使用嵌板,然后画粗线,但是怎样才能正确地画出墙,使我的角色不超过这些墙?我简直无法想象我怎么能做到这一点。下面是一个迷宫的草图,以说明我将如何做:Java 建造迷宫,java,events,awt,frame,paint,Java,Events,Awt,Frame,Paint,起初我认为这很容易,但当我开始这样做时,我不知道如何继续下去。我的想法是使用嵌板,然后画粗线,但是怎样才能正确地画出墙,使我的角色不超过这些墙?我简直无法想象我怎么能做到这一点。下面是一个迷宫的草图,以说明我将如何做: 我刚从一个框架开始,但仍在努力掌握这样做的想法。首先,您需要一个表示迷宫的数据结构。然后你就可以担心画画了 我建议你上这样的课: class Maze { public enum Tile { Start, End, Empty, Blocked }; priv
我刚从一个
框架开始
,但仍在努力掌握这样做的想法。首先,您需要一个表示迷宫的数据结构。然后你就可以担心画画了
我建议你上这样的课:
class Maze {
public enum Tile { Start, End, Empty, Blocked };
private final Tile[] cells;
private final int width;
private final int height;
public Maze(int width, int height) {
this.width = width;
this.height = height;
this.cells = new Tile[width * height];
Arrays.fill(this.cells, Tile.Empty);
}
public int height() {
return height;
}
public int width() {
return width;
}
public Tile get(int x, int y) {
return cells[index(x, y)];
}
public void set(int x, int y, Tile tile) {
Cells[index(x, y)] = tile;
}
private int index(int x, int y) {
return y * width + x;
}
}
然后我会用方块(正方形)而不是线来画这个迷宫。一个黑色的方块用于阻塞的瓷砖,一个透明的方块用于空瓷砖
要画画,就这样做
public void paintTheMaze(graphics g) {
final int tileWidth = 32;
final int tileHeight = 32;
g.setColor(Color.BLACK);
for (int x = 0; x < maze.width(); ++x) {
for (int y = 0; y < maze.height(); ++y) {
if (maze.get(x, y).equals(Tile.Blocked)) (
g.fillRect(x*tileWidth, y*tileHeight, tileWidth, tileHeight);
}
}
)
}
public void painthemaze(图g){
最终int tileWidth=32;
最终整数tileHeight=32;
g、 设置颜色(颜色为黑色);
对于(int x=0;x
首先,您需要一个表示迷宫的数据结构。然后你就可以担心画画了
我建议你上这样的课:
class Maze {
public enum Tile { Start, End, Empty, Blocked };
private final Tile[] cells;
private final int width;
private final int height;
public Maze(int width, int height) {
this.width = width;
this.height = height;
this.cells = new Tile[width * height];
Arrays.fill(this.cells, Tile.Empty);
}
public int height() {
return height;
}
public int width() {
return width;
}
public Tile get(int x, int y) {
return cells[index(x, y)];
}
public void set(int x, int y, Tile tile) {
Cells[index(x, y)] = tile;
}
private int index(int x, int y) {
return y * width + x;
}
}
然后我会用方块(正方形)而不是线来画这个迷宫。一个黑色的方块用于阻塞的瓷砖,一个透明的方块用于空瓷砖
要画画,就这样做
public void paintTheMaze(graphics g) {
final int tileWidth = 32;
final int tileHeight = 32;
g.setColor(Color.BLACK);
for (int x = 0; x < maze.width(); ++x) {
for (int y = 0; y < maze.height(); ++y) {
if (maze.get(x, y).equals(Tile.Blocked)) (
g.fillRect(x*tileWidth, y*tileHeight, tileWidth, tileHeight);
}
}
)
}
public void painthemaze(图g){
最终int tileWidth=32;
最终整数tileHeight=32;
g、 设置颜色(颜色为黑色);
对于(int x=0;x
要创建随机迷宫还是一个固定迷宫?你已经有了碰撞检测的形式吗?你想创建随机迷宫还是一个固定迷宫?你已经有了一种形式的碰撞检测吗?但是禁止越过这堵墙
怎么样?不,我不是,我将使用键盘监听器来控制我的角色
然后你需要另一个类来存储你的角色在迷宫中的位置(他的x和y坐标)以及当他们按下时(说左)检查左侧的单元格(x-1,y)是否未使用maze.get()方法进行平铺。但是禁止越过这堵墙如何
?不,我将使用键侦听器控制我的角色
,然后需要另一个类来存储角色在迷宫中的位置(他的x和y坐标)当他们按下(比如说左键)时,用maze.get()方法检查他们左边(x-1,y)的单元格是否是平铺的。