我的(基本)吃豆人游戏的面向对象设计
我正在尝试用 C++ 创建一个基本的 Pacman 游戏(我将在这个问题中使用 Java 语法,因为这更容易演示),但我找不到好的设计选项。
到目前为止我有 4 节课:
- 怪物:可以针对怪物特定的行为进行子类化,并包含怪物的所有逻辑
- 玩家:包含玩家逻辑
- 地图:包含表示地图的二维数组。该数组指定哪些位置是墙壁或吃豆人食物
- 游戏:包含玩家、地图和怪物列表。
为了简单起见:
public class Game {
Player player;
Map map;
ArrayList<Monster> monsters;
public Game() {
player = new Player();
map = new Map();
monsters = new ArrayList<Monster();
monsters.add(new ScaryMonster());
monsters.add(new DumpMonster());
}
public void update() {
player.update();
map.update();
for (Monster monster: monsters) {
monster.update();
}
public void draw() {
map.draw();
player.draw();
for (Monster monster: monsters) {
monster.draw();
}
}
所以我现在要做的就是创建一个 Game 对象并每次调用 update() 和 draw() 。很简单。但这不起作用。
假设我在玩家对象上调用 update() 并且玩家(当然是吃豆人)点击了食物。在这种情况下,地图对象应该收到通知(以及位置)以从二维数组中删除食物。假设玩家杀死了一个怪物,怪物的位置应该改变(怪物类有一个“位置字段”)。你还可以想象更多这样的情况。
一种选择是将地图和怪物对象作为参数传递给玩家对象的 update() 和 draw() 方法。并在map的方法调用中将玩家和怪物对象作为参数传递。但这听起来肯定不像是一个好的面向对象设计。
有什么好的 OO 方法可以解决这个问题吗?我正在考虑使用观察者模式(所以游戏是主题,玩家,地图和怪物是观察者),但这没有任何意义:这样观察者必须让主题知道任何变化,这是显然不是正确的使用方法。
任何提示将非常受欢迎。
非常感谢 :)
I'm trying to create a basic Pacman game in C++ (I'll use Java syntax in this question as this is somewhat easier to demonstrate), but I can't find a good design option.
So far I have 4 classes:
- Monster: Can be subclassed for monster-specific behaviour and contains all logic for the monsters
- Player: Contains player-logic
- Map: Contains a 2d array representing the map. This array specifies which positions are walls or Pacman food
- Game: contains a Player, a Map and a list of Monsters.
To keep it simple:
public class Game {
Player player;
Map map;
ArrayList<Monster> monsters;
public Game() {
player = new Player();
map = new Map();
monsters = new ArrayList<Monster();
monsters.add(new ScaryMonster());
monsters.add(new DumpMonster());
}
public void update() {
player.update();
map.update();
for (Monster monster: monsters) {
monster.update();
}
public void draw() {
map.draw();
player.draw();
for (Monster monster: monsters) {
monster.draw();
}
}
So all I have to do now is to create a Game object and call update() and draw() on it every time. Very simple. But it doesn't work.
Assume I call update() on the player-object and the player (which is the Pacman ofcourse) hits food. In that case, the map-object should get notified of this (and the position) to remove the food from the 2d-array. Assume the player kills a monster, the position of the monster should get changed (the Monster class has a "position field"). And you can imagine a lot more of these situations.
An option would be to pass the map and monster object as parameters in the update() and draw() method of the player object. And to pass the player and monster objects as parameters in the method calls of map. But that surely doesn't sound like a good OO design.
What's a good OO way to solve this? I was thinking about using the Observer pattern (so Game is the subject, player, map and monsters are observers), but that doesn't make any sense: that way the observers will have to let the subject know of any changes, which is obviously not the correct way of using this.
Any tips would be very welcome.
Thank you very much :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不尝试映射操作呢?
每个动作都会有反应。假设吃豆人碰到了食物。这是一个“击中食物”的动作,它反过来会做出反应(通知地图、食物或任何你喜欢的东西)食物不再存在。
现在想象一下吃豆人击中了一个怪物,这是另一个动作......对此会有什么反应?好吧,它可能会导致怪物死亡(调用 BeDeath 方法:P),或者可能导致吃豆人死亡……无论是什么,它都允许您将动作与游戏中的反应联系起来。
这意味着游戏的逻辑、规则将在游戏类中,此外游戏类已经知道所需的所有元素并且可以与每个元素进行通信。
编辑:一个简单的例子(非常简单,随着游戏变得更加复杂,您需要更好地考虑动作和反应结构)
或者如果您愿意,您也可以映射反应
请注意,要实现这一点,所有反应都必须共享一个通用签名(即,采用转换为预期参数的对象数组)
Why don't you try mapping actions?
Every action has a reaction. So let's say the pacman hits food. That's an action, "hitting food", which in turn has a reaction (notifying the map, or the food, or whatever you like) that the food is not there any longer.
Now imagine the pacman hits a monster, that's another action... what would be the reaction to that? Well it might cause the monster to get dead (a call to the BeDeath method :P) or it could cause the pacman to get death... whatever it is it allow you to chain actions to reactions in the game.
That means the logic of the game, the rules would be in the game class, who in addition already knows all the elements needed and can communicate with each one.
Edit: A simple example (very simple, as the game gets more complex you'll need to think better about actions and reactions structure)
Or if you like you could also map the reactions
Note that for that to work all you reactions must share a common signature (i.e, taking an array of objects that are cast to the expected parameters)