java Java中的吃豆子迷宫

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/622471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:05:57  来源:igfitidea点击:

Pacman maze in Java

javaalgorithm2dmaze

提问by Click Upvote

So I'm building the pacman game in Java to teach myself game programming.

所以我正在用 Java 构建 pacman 游戏来自学游戏编程。

I have the basic game window with the pacman sprite and the ghost sprites drawn, the pacman moves with the arrow keys, doesn't move beyond the walls of the window, etc. Now I'm trying to build the maze, as in this picture:

我有一个基本的游戏窗口,里面有吃豆子精灵和绘制的幽灵精灵,吃豆子用箭头键移动,不会移动到窗口的墙壁之外,等等。现在我正在尝试构建迷宫,如下所示图片:

Pacman maze

吃豆子迷宫

Without giving me the direct/complete solution to this, can someone guide me as to how this can be built? I'm talking only about the boundaries and the pipes('T' marks) here which you can't go through and you have to go around. Not the dots which the pacman eats yet.

没有给我直接/完整的解决方案,有人可以指导我如何构建它吗?我在这里只谈论边界和管道('T' 标记),你不能通过,你必须绕过。还不是吃豆人吃的点。

Here are my questions:

以下是我的问题:

1) What's the most efficient algorithm/method for creating this maze? Will it have to be drawn each time the paint() method is called or is there a way to draw it only at the start of the game and never again?

1)创建这个迷宫最有效的算法/方法是什么?每次调用paint()方法时都必须绘制它还是有办法只在游戏开始时绘制它而不再绘制它?

2) How will this actually be drawn to the screen? I assume the fillRect()will be used?

2) 这将如何实际绘制到屏幕上?我假设fillRect()将被使用?

3) Any hints on collision detection (so the pacman/ghosts can't go through the walls) would be helpful.

3)任何关于碰撞检测的提示(所以吃豆人/鬼魂不能穿过墙壁)都会有帮助。

4) Any hints on how the vacant space between the pipes will be calculated so the dots can be filled between them will also be very helpful.

4) 关于如何计算管道之间的空余空间以便在它们之间填充点的任何提示也将非常有帮助。

Thanks

谢谢

回答by Spencer Ruport

I wouldn't do it that way.

我不会那样做。

I'd draw the graphical map and then create a 2D data array which represents the map. The data map would be responsible for determining collisions, eating dots, where candy is and where the ghosts are. Once all the logic for everything is handled just use the 2D array to display everything in their proper pixel coordinates over the graphical map.

我会绘制图形地图,然后创建一个代表地图的二维数据数组。数据地图将负责确定碰撞、吃点、糖果在哪里以及鬼魂在哪里。处理完所有内容的所有逻辑后,只需使用 2D 数组在图形地图上以适当的像素坐标显示所有内容。

For example the user is pressing the left key. First you determine that pacman is at element 3, 3. Element 3, 2 contains information denoting a wall so you can implement the code to make him ignore the command.

例如,用户正在按下左键。首先,您确定 pacman 位于元素 3、3。元素 3、2 包含表示墙的信息,因此您可以实现代码以使他忽略该命令。

EDIT:

编辑:

Each element would represent about where a dot could be. For example:

每个元素将代表一个点可能在哪里。例如:

No, looking at the board I would say the array would look something like this.

不,看着董事会,我会说阵列看起来像这样。

d,d,d,d,d,d,d,d,d,d,d,d,w,w,d,d,d,d,d,d,d,d,d,d,d,d
d,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,d
p,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,p    
d,w,w,w,w,d,w,w,w,w,w,d,w,w,d,w,w,w,w,w,d,w,w,w,w,d    
d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d,d

And so on. You might want to pick a more flexible data structure than just characters however since some areas need to contain a bunch of different information. IE even though the ghost spawning area is blank, pacman isn't allowed in there. The movement of the ghosts and pacman is different for the side escapes, the candy spawn point is a blank spot but if you want to remain flexible you'll want to denote where this is on a per map basis.

等等。您可能想要选择比字符更灵活的数据结构,因为某些区域需要包含一堆不同的信息。IE 即使幽灵产卵区是空白的,也不允许吃豆子进入那里。鬼魂和吃豆人的移动对于侧逃是不同的,糖果生成点是一个空白点,但如果你想保持灵活,你需要在每张地图的基础上指出它的位置。

Another thing you'll want to remember is that pacman and the ghosts are often inbetween points so containing information that represents a percentage of a space they're taking up between 1,2 and 1,3 is important for collision detection as well as determining when you want to remove dots, powerups and candy from the board.

您要记住的另一件事是吃豆子和鬼魂通常位于点之间,因此包含代表它们在 1,2 和 1,3 之间占据的空间百分比的信息对于碰撞检测以及确定当你想从板上删除点、通电和糖果时。

回答by Click Upvote

Here's a hint for you:

这里有一个提示给你:

alt text http://www.freeimagehosting.net/uploads/66e4131f59.jpg

替代文字 http://www.freeimagehosting.net/uploads/66e4131f59.jpg

This hint, properly used, will allow you to handle rendering and collision detection, and will also allow you to represent the maze in a compact fashion.

如果使用得当,这个提示将允许您处理渲染和碰撞检测,并且还允许您以紧凑的方式表示迷宫。

回答by TrayMan

  1. You can paint the map into a BufferedImage and just drawImage that on every paint(). You'll get quite reasonable performance this way.

  2. If you are happy with the walls being solid, you can draw each square wall block with fillRect. If you wish to get the same look as in the picture, you need to figure how to draw the lines in the right way and use arcs for corners.

  3. The Pacman game map is made of squares and Pacman and the ghosts always move from one square to the neighbouring square in an animated step (i.e. you press right, the pacman moves one square to the right). That means that collision detection is easy: simply don't allow moves to squares that are not empty.

  4. I do not understand what you are trying to ask here.

  1. 您可以将地图绘制到 BufferedImage 中,然后在每个paint() 上绘制该地图。通过这种方式,您将获得相当合理的性能。

  2. 如果您对实心墙感到满意,则可以使用 fillRect 绘制每个方形墙块。如果您希望获得与图片相同的外观,则需要弄清楚如何以正确的方式绘制线条并使用圆弧作为拐角。

  3. 吃豆人游戏地图由方块和吃豆人组成,鬼魂总是以动画步骤从一个方块移动到相邻的方块(即你按向右,吃豆人向右移动一个方块)。这意味着碰撞检测很容易:只是不允许移动到非空方格。

  4. 我不明白你想在这里问什么。

回答by kgrad

1) Just to give my advice on redrawing. Something that you can do if you find redrawing the entire image is slow, is determine only the elements that have changed on the screen and redraw those. An approach for this would be the following: Determine the sprites that have moved. Determine (approximate) a rectangle around those sprites. Redraw those rectangles only. This way you are only refreshing parts of the screen and not the whole screen. This should result in an increase in performance over redrawing the entire screen.

1)只是为了给我重画的建议。如果您发现重绘整个图像很慢,您可以做的事情是仅确定屏幕上已更改的元素并重绘这些元素。一种方法如下: 确定已移动的精灵。确定(近似)围绕这些精灵的矩形。仅重绘这些矩形。这样你只会刷新屏幕的一部分,而不是整个屏幕。与重绘整个屏幕相比,这应该会提高性能。

The other answers have been reasonable for the other questions you have asked.

对于您提出的其他问题,其他答案是合理的。