简体   繁体   English

Java中的吃豆人迷宫

[英]Pacman maze in Java

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:我有基本游戏 window 与 pacman sprite 和 ghost sprites 绘制,pacman 用箭头键移动,不会超出 window 等的墙壁。现在我正在尝试构建迷宫,就像在这个图片:

吃豆人迷宫

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.我在这里只谈论边界和管道('T'标记),你不能通过 go 并且你必须在 go 周围。 Not the dots which the pacman eats yet.不是吃豆人吃的那些点。

Here are my questions:以下是我的问题:

1) What's the most efficient algorithm/method for creating this maze? 1)创建这个迷宫最有效的算法/方法是什么? 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?每次调用paint()方法时都必须绘制它,还是有办法只在游戏开始时绘制它而不再绘制?

2) How will this actually be drawn to the screen? 2)这将如何实际绘制到屏幕上? I assume the fillRect() will be used?我假设将使用fillRect()

3) Any hints on collision detection (so the pacman/ghosts can't go through the walls) would be helpful. 3) 任何有关碰撞检测的提示(因此 pacman/ghosts 不能 go 穿过墙壁)会有所帮助。

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谢谢

I wouldn't do it that way.我不会那样做的。

I'd draw the graphical map and then create a 2D data array which represents the map.我将绘制图形 map,然后创建一个代表 map 的二维数据数组。 The data map would be responsible for determining collisions, eating dots, where candy is and where the ghosts are.数据 map 将负责确定碰撞、吃点、糖果在哪里以及鬼魂在哪里。 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 数组在图形 map 上以正确的像素坐标显示所有内容。

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. IE 即使幽灵生成区域是空白的,pacman 也不允许在那里。 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.侧逃生的鬼魂和吃豆人的移动是不同的,糖果生成点是一个空白点,但如果你想保持灵活,你需要在每个 map 的基础上指出它的位置。

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.您需要记住的另一件事是 pacman 和幽灵通常位于点之间,因此包含代表它们在 1,2 和 1,3 之间占用的空间百分比的信息对于碰撞检测和确定很重要当您想从板上移除点、通电和糖果时。

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.正确使用此提示将允许您处理渲染和碰撞检测,并且还允许您以紧凑的方式表示迷宫。

  1. You can paint the map into a BufferedImage and just drawImage that on every paint().您可以将 map 绘制到 BufferedImage 中,然后在每个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.如果您对实心墙感到满意,您可以使用 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 (ie you press right, the pacman moves one square to the right). Pacman 游戏 map 由正方形组成,Pacman 和幽灵总是以动画的方式从一个正方形移动到相邻的正方形(即您按右,吃豆人向右移动一格)。 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) Just to give my advice on redrawing. 1)只是为了给我关于重绘的建议。 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.与重绘整个屏幕相比,这应该会提高性能。

The other answers have been reasonable for the other questions you have asked.对于您提出的其他问题,其他答案是合理的。

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

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