简体   繁体   English

Java迷宫游戏—如何不走墙

[英]Java maze game— How to not go through the wall

So for a class project, I'm making a solvable maze game (in Java). 因此,对于一个课堂项目,我正在制作一个可解决的迷宫游戏(使用Java)。 I can randomly generate and display the maze without a problem, and the same goes for the user/player's representation. 我可以随机生成并显示迷宫,而不会出现问题,用户/玩家的表示也一样。 However, the problem I'm having is with the individual walls of the maze. 但是,我遇到的问题是迷宫的各个壁。

I need to make sure that the player can't go through the walls. 我需要确保玩家不能穿过墙壁。 I've looked around, and it seems a lot of people have similar problems, but they are using a grid structure to make their mazes. 我环顾四周,似乎很多人都有类似的问题,但是他们使用网格结构制造迷宫。 I'm not --my maze's walls are just lines, so I can't do what everybody else is doing, (just see if a certain cell in the maze is already occupied --I don't have cells to check). 我不是-我的迷宫的墙只是线条,所以我无法做其他人正在做的事情((看看迷宫中的某个牢房是否已经被占用了–我没有要检查的牢房)。

What I do have is both end points of the line --the starting x, starting y, ending x, and ending y-- and the current point of the upper left corner of the circle that represents the player. 我所拥有的是该线的两个端点-起点x,起点y,终点x和终点y-以及代表玩家的圆圈左上角的当前点。 I also have the point of the proposed new upper left corner of the circle. 我也有建议的新的圆圈左上角的要点。

I need to know if the player is going to cross any of the lines that represent walls. 我需要知道玩家是否会越过代表墙壁的任何线。 At the moment, I loop through an array that contains all of the walls. 此刻,我遍历了包含所有墙的数组。 Given the current player position and proposed player position, I need to find out if that involves crossing a wall. 给定当前玩家位置和建议的玩家位置,我需要找出是否涉及过墙。 Any tips/hints/help would be much appreciated. 任何提示/提示/帮助将不胜感激。 Thank you in advance! 先感谢您!

You can construct a line segment (x1, x2), where x1 is the player's current position, and x2 is the location the player is attempting to move to. 您可以构造线段(x1,x2),其中x1是玩家的当前位置,x2是玩家试图移动的位置。 Any move through a wall would have this line segment intersect the wall segment. 穿过墙的任何移动都会使该线段与墙段相交。 You can therefore validate a move by checking (x1, x2) for intersection against all maze walls (a linear-time task). 因此,您可以通过检查(x1,x2)与所有迷宫墙的交点来验证移动(线性时间任务)。

Checking segment intersection is easy, and there is a nice answer explaining how to implement it. 检查线段交点很容易,并且有一个很好的答案解释了如何实现它。

If you can add any additional information (for example maximum length of walls), you can store the maze in such a way that you would not have to query all walls for intersection. 如果您可以添加任何其他信息(例如,墙壁的最大长度),则可以以不必查询所有墙壁相交的方式存储迷宫。 For example, if walls are either horizontal or vertical, and moves are as well, you need only check horizontal walls in case of a vertical move. 例如,如果墙壁是水平的或垂直的,并且移动也是如此,则在垂直移动的情况下只需要检查水平的墙壁即可。

Hope this helps! 希望这可以帮助!

Assuming your world is 2D, 假设您的世界是2D,

[] [] []|[]
      -----
[] [] [] []

[] [] [] []

[] [] [] []

and | and --- are walls 和---是墙

Method #1: enlarge your grid to include the wall intersections. 方法1:扩大网格以包括墙的相交处。 So the size of the row and columns will be row+(row-1),col+(col-1) 所以行和列的大小将是row +(row-1),col +(col-1)

this is your new array representation: Original row = 4, new row = 4+3 = 7. This will also create walls to check for diagonal movements. 这是您的新数组表示形式:原始行= 4,新行= 4 + 3 =7。这还将创建墙以检查对角线的移动。

[ ][ ][ ][ ][ ][|][ ]
[ ][ ][ ][ ][-][-][-]
[ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]
[ ][ ][ ][ ][ ][ ][ ]

Method #2: using the 4x4 representation, create a wall class that stores the adjacent tiles coordinate. 方法2:使用4x4表示法,创建一个存储相邻图块坐标的墙类。 And after that create a list to store a list of walls. 之后,创建一个列表以存储墙列表。

Wall(Point x,Point y); // constructor
Wall wall1 = new Wall(new Point(0,2),new Point(0,3));
Wall wall2 = new Wall(new Point(0,2),new Point(1,2));
Wall wall3 = new Wall(new Point(0,3),new Point(1,3));

this representation, allows you to create rules such that 0,0 can go to 1,1 but 0,1 cannot go to 1,0. 此表示形式允许您创建规则,以使0,0可以达到1,1,而0,1不能达到1,0。 If you want a unidirectional wall, which might sound silly but who knows? 如果您想要单向墙,这听起来可能很愚蠢,但是谁知道呢? You can modify your wall class constructor: 您可以修改墙类构造函数:

Wall(Point from,Point to);

Collision checking for method 1: when moving your character, after you move, if it lands on the wall, don't allow the movement. 方法1的碰撞检查:移动角色时,移动后,如果它落在墙上,则不允许移动。

Collision checking for method 2: when moving your character, cache where the character is from, and where the character will move to if the move is allowed, construct a new Wall object base on that and check if the Wall list contains the new Wall object. 方法2的碰撞检查:移动角色时,缓存角色的来源以及允许移动的角色将移动到的位置,在此基础上构造一个新的Wall对象,并检查Wall列表是否包含新的Wall对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM