我被教导不要将大多数方法放在一般的“系统”而他们去哪里呢? [英] I've been taught not to place most methods in a general "System" class but where do they go instead?

查看:76
本文介绍了我被教导不要将大多数方法放在一般的“系统”而他们去哪里呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在玩一个游戏。将几乎所有游戏逻辑放在 Game 类中似乎是最自然的,它创建了所有的对象,并且大部分都是getter和setter方法。这实际上是错的吗?我应该尝试强制一些游戏逻辑进入其他类,如 Player 的过程轮流作为 Player



在典型的游戏中,如果通过鼠标点击触发玩家拍摄,您将如何从点击事件转到更新屏幕状态以摧毁目标对象的方法将在哪里找到对象和对方互相对话?



我应该摆脱游戏类,并以某种方式让其他对象都互相调用?

解决方案

描述问题的方式揭示您将使用属于其他对象的逻辑来重载课程 Game



第一个信号出现在您说其他对象主要是吸烟者和设置者。这意味着这些对象只是保持状态但缺乏有趣行为的数据结构。这表明您仍然必须深入思考如何在对象之间分配责任。



第二个证据在您的问题的第二段。这表明模型和GUI之间的分离差。验证是否有足够的分离(或不)是一个很好的测试来证明您可以从脚本中玩游戏(至少是它的非平凡部分)即不运行任何GUI代码。游戏模型只有在无头模式下才能正常设计。



最后,摆脱游戏 class将是一个坏的决定,因为你的模型不会有任何一个实体存在于你想建模的现实中。



我认为您描述的游戏中心设计反映了您查看域名的方式。如果你只看游戏,你就会模拟出来。然而,游戏通常是丰富的模拟,充满了有趣的对象,其行为远远超出了他们与玩家的互动。因此,我会建议对其他对象,他们知道什么,他们可以执行什么任务,如何与他人合作等进行更多思考。换句话说,不要编程游戏,编程游戏,元素,规则,阶段,战略,人物等。也不要低估任何实体;所有这些都有一些有趣的东西可以增加,即使在棋盘上的单个瓷砖也有助于您创造的小宇宙。没有其他人可以提供的东西,没有其他对象可以提供代码和优雅的经济。


I'm making a game. It seems most natural to put almost all the game logic in the Game class, have it create all the objects and have mostly getters and setter methods in them. Is this actually wrong? Should I try to force some of the game logic into the other classes like the process of Player taking his turn as a method of Player?

In a typical game, if a player shooting is triggered by a mouse click, how would you go from the click event to updating the screen state to destroying the target object in terms of where the methods would be located and objects calling each other?

Should I even be getting rid of the Game class and somehow having the other objects all calling each other?

解决方案

The way you describe the problem reveals that you are overloading the class Game with logic that should belong in other objects.

The first signal appears when you say that other objects "have mostly getters and setters". That means that those objects are just data structures holding state but lacking interesting behavior. This indicates that you still have to think deeper on how you distribute responsibilities among objects.

The second evidence is in the second paragraph of your question. It suggests poor separation between model and GUI. A good test to verify whether there is adequate separation (or not) is to prove that you could play the Game (at least non-trivial parts of it) from a script, i.e., without exercising any GUI code. The game model will only be properly designed if it is full functional in "headless" mode.

Finally, getting rid of the Game class would be a bad decision because your model would not have any representation of an entity that exists in the "reality" you are trying to model.

I think that the Game-centric design you describe reflects your way of looking at the domain. If you only see "the game" you will model just that. However, games are usually rich simulations full of interesting objects whose behaviors go far beyond their interactions with the player. I would therefore recommend more thinking on the other objects, what they know, what tasks could they carry on, how would they collaborate with others, etc. In other words, do not program the game, program its actors, elements, rules, stages, strategies, characters, etc. Do not underestimate any entities either; all of them have something interesting to add to the whole, even a single tile in the chess board has something to contribute to the small universe you are creating. Not something that anybody else could provide, something that no other object could provide with economy of code and elegance.

这篇关于我被教导不要将大多数方法放在一般的“系统”而他们去哪里呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆