如何用 C++ 编写 PacMan 地图

发布于 2024-10-08 22:35:25 字数 61 浏览 2 评论 0原文

谁能告诉我如何用 C++ 创建 PacMan 地图。 对于开始使用有什么建议吗?

提前致谢。

Can anyone plese tell me How do i create a Map for PacMan in C++.
Any Suggestions for getting m Started.?

Thanx in Advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-10-15 22:35:25

这是一个相当广泛的问题,取决于其他要求。基本上我会选择一个二维数组来简单地表示单元格。每个单元格将包含以下其中之一:

  1. 一堵墙
  2. 单向墙(代表怪物通过的门)
  3. 一个空单元格
  4. 一个装有食物的单元格
  5. 一个“传送单元”,将吃豆人发送到不同的地方(代表两个左侧和右侧的单元格)

怪物和吃豆人本身不会出现在地图中。相反,每个人都应该在地图上有自己的位置。

这不需要 C++,它只需要 C,但如果您故意想要 C++ 解决方案,您可以用向量替换数组,或者在本例中是向量的向量。

怪物将如何移动

如果您想从地图上的 B 点到达 A 点,您可以使用多种算法中的任何一种来找到地图内的最短路径 - 就像A* 算法。但是,在这种情况下,您需要考虑一些事项:

  1. 吃豆人本身总是在移动,这会改变从一步到另一步的路径。
  2. 你不希望所有的怪物都有相同的路径,否则你不会真的有几个怪物,因为它们都会移动相同的路径。
  3. 你不希望怪物非常有效,因为那样的话游戏就会变得太难并且不会有太多乐趣。

我建议当怪物到达“十字路口”时使用一些随机决策,以及对吃豆人本身的普遍偏见。因此,如果怪物位于有 3 个方向的点,例如东、西、北,而吃豆人在北,我会做出随机决定,有 40% 的机会向北,30% 向东,30% % 向西。

That's quite a wide question and depends on additional requirements. Basically I would go for a two-dimensional array that will simply represent the cells. Each cell would contain either:

  1. A wall
  2. A one-way wall (to represent the gate through which the monsters go)
  3. An empty cell
  4. A cell with food
  5. A "teleportation cell" that will send the pacman to a different place (to represent the two cells on the left and right sides)

The monsters and the pacman itself will not be represented in the map. Instead each should just have his position in the map.

This doesn't require C++, it only requires C, but if you deliberately want a C++ solution you can replace the arrays with a vector, or in this case, a vector of vectors.

How will the monsters move

If you want to reach point A from point B in a map, you can use any of a variety of algorithms that will find you the shortest path within the map - like the A* algorithm. However, in this case, you need to take a few things into account:

  1. The pacman itself is always on the move, which changes the path from one step to another.
  2. You don't want all the monsters to have the same path, otherwise you won't really have several monsters, because all of them will move the same.
  3. You don't want the monsters to be extremely effective, because then the game will become too hard and will not be much fun.

I would suggest using some random decisions when the monster reaches a "crossroad", along with a general bias towards the pacman itself. So if the monster is at a point where there are 3 directions, like East, West and North, and the pacman is to the north, I would make a random decision with 40% chance to the north, 30% to the east and 30% to the west.

橙幽之幻 2024-10-15 22:35:25

这里有一个提示:

// 1 = wall
// 0 = no wall

int map[6][6] = {{ 1, 1, 1, 1, 1, 1 },
                 { 1, 0, 0, 0, 0, 1 },
                 { 1, 0, 0, 0, 0, 1 },
                 { 1, 0, 0, 0, 0, 1 },
                 { 1, 0, 0, 0, 0, 1 },
                 { 1, 1, 1, 1, 1, 1 }};

Here's a tip:

// 1 = wall
// 0 = no wall

int map[6][6] = {{ 1, 1, 1, 1, 1, 1 },
                 { 1, 0, 0, 0, 0, 1 },
                 { 1, 0, 0, 0, 0, 1 },
                 { 1, 0, 0, 0, 0, 1 },
                 { 1, 0, 0, 0, 0, 1 },
                 { 1, 1, 1, 1, 1, 1 }};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文