简单的 Java 游戏中的 MVC?
嘿,我正在用 Java 实现一个简单 Breakout 克隆,并尝试使其符合 MVC 模式。我承认,我有 Web 背景,只在 Php 框架 Symfony 中使用过 MVC,从未在桌面应用程序中使用过。所以我的问题是决定哪个实体负责哪些任务。
我将我的项目分为这些类:
Breakout extends JFrame
(主类)启动MainViewController
并将其视图添加到内容窗格。MainViewController 扩展了 TimerTask
启动MainView
并处理其鼠标和键盘事件。还运行游戏循环(计算游戏对象的位置和状态,即Ball
、Paddle
、Brick
s),是在这个地方?MainView extends JPanel
只是在屏幕上绘制Ball
、Paddle
、Brick
,这里没有任何逻辑。但是,它也会启动这些对象。恐怕这不正确,对吧?
最后,游戏元素:
Ball extends Ellipse2D
、Paddle extends Rectangle2D
和Brick extends Rectangle2D
提供了在屏幕上移动它们的方法,而且还提供了碰撞检测在这里完成。我再次怀疑这是正确的位置,将其移至控制器吗?
什么是模型?我想,正是这些游戏元素,因为它们代表了游戏期间唯一更改的数据。但这些都是控制器元素,因为它们也提供碰撞检测逻辑。它们最好的一点是,它们是在 view 类中启动的。我确信我的设计决策出了问题。
Hey, I'm implementing a simple Breakout clone in Java and try to make it conform to the MVC pattern. I confess, I come from a web background and have used MVC only in the Php framework Symfony, never in a desktop application. So my problem is to decide which entity is responsible for which tasks.
I divided my project into those classes:
Breakout extends JFrame
(main class) initiates theMainViewController
and adds its view to the content pane.MainViewController extends TimerTask
initiates theMainView
and handles its mouse and keyboard events. Also runs the game loop (calculates the position and state of the game objects, i.e.Ball
,Paddle
,Brick
s), is that right in this place?MainView extends JPanel
simply drawsBall
,Paddle
,Brick
s on the screen, no logic in here. But, it also initiates those objects. I'm afraid, this isn't correct, right?
Finally the game elements:
Ball extends Ellipse2D
,Paddle extends Rectangle2D
andBrick extends Rectangle2D
offer methods to move them on screen, but also collision detection is done here. Again, I doubt this is the right place, move it to the controller?
And what is the model? I guess, exactly those game elements because they represent the only data that is changed during the game. But those are controller elements since they offer collision detection logic, too. And the best thing about them is, they are initiated in the view class. I'm sure, something went wrong in my design decision.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
开发游戏是一个有点不同的概念,但仍然是 MVC。
您的模型是游戏的实体,例如球、桨和砖块。
游戏基本分为三步。
1° 读取输入(您的 ViewController 对其进行计数)
2° 流程件 AI(类似行为并使用控制器中的新值进行移动)
3° 在屏幕上绘制(您在屏幕上绘制所有实体)
在第一步中,如果用户输入向左或向右,您应该使用这些值更新桨实体。
碰撞应该在第二步中测试,对于你的例子,球应该测试它是否与任何砖块或桨相交来击退,球不需要用户动作来移动,所以它应该不断地向某个方向移动,直到它相交。
第三步只是在屏幕上绘制所有元素。
游戏的第一个对象应该在 View init 的 setup() 方法中创建,其他对象(例如桨射击或从破碎的砖块中掉落的特殊奖励)应该在第二步中创建,在这种情况下padle 控制器应该告诉 paddle 用户按下了按钮进行射击,在这个过程中,您创建射击实体并将其添加到实体游戏循环中,与砖块相同,但是当它注意到它得到时,它们会创建奖励被毁了。
Develop game is a bit different concept but still is a MVC.
Your models are the Entitys of the game, like ball, paddle and brick.
A game are basic three steps.
1° Read input (You ViewController take count of that)
2° Process Pieces AI (Like behaviours and moves with the new values from the controller)
3° Draw on screen (You draw all you entitys on the screen)
In the first step if the user enters left or right you should update the paddle entity with theses values.
The collision should be tested within the second step, the ball for your example, should test if it intersects any brick or the paddle to knock back, the ball don't need user actions to move, so it should move constantly in some direction until it intersects.
The third step is just for draw all elements on screen.
The first objects of the game should be created inside a setup() method in the View init, the others (like the paddle shooting or a special bonus dropping from a broken brick) should be created inside the second step, in the case of the padle the controller should tell to the paddle that the user pressed the button to shoot, inside the process you create the shots entities and add it to the entities game loop, the same to the brick, but they create the bonus when it notice it get destroyed.