简单的 Java 游戏中的 MVC?

发布于 2024-10-10 07:28:28 字数 978 浏览 1 评论 0原文

嘿,我正在用 Java 实现一个简单 Breakout 克隆,并尝试使其符合 MVC 模式。我承认,我有 Web 背景,只在 Php 框架 Symfony 中使用过 MVC,从未在桌面应用程序中使用过。所以我的问题是决定哪个实体负责哪些任务。

我将我的项目分为这些类:

  • Breakout extends JFrame (主类)启动 MainViewController 并将其视图添加到内容窗格。
  • MainViewController 扩展了 TimerTask 启动 MainView 并处理其鼠标和键盘事件。还运行游戏循环(计算游戏对象的位置和状态,即 BallPaddleBricks),是在这个地方?
  • MainView extends JPanel 只是在屏幕上绘制 BallPaddleBrick,这里没有任何逻辑。但是,它也会启动这些对象。恐怕这不正确,对吧?

最后,游戏元素:

  • Ball extends Ellipse2DPaddle extends Rectangle2DBrick 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 the MainViewController and adds its view to the content pane.
  • MainViewController extends TimerTask initiates the MainView 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, Bricks), is that right in this place?
  • MainView extends JPanel simply draws Ball, Paddle, Bricks 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 and Brick 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 技术交流群。

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

发布评论

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

评论(2

时光匆匆的小流年 2024-10-17 07:28:28

开发游戏是一个有点不同的概念,但仍然是 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.

送舟行 2024-10-17 07:28:28
  • 我会切换 Breakout 和 MainViewController。让控制器启动视图。
  • 你的模型实际上是球、桨和砖,所以我认为 MainViewController 应该创建它们而不是 MainView。
  • 否则,我不会强调模型的某些父类具有对绘制有用的方法。它不是视图和模型的完美分离,但它保持了简单。
  • I would switch Breakout and MainViewController. Let the controller initiate the view.
  • Your model is really Ball, Paddle and Bricks, so I think the MainViewController should create them rather than MainView.
  • Otherwise I would not stress about some of the model's parent's classes having method that are useful for drawling. It is not a perfect separation of view and model, but it keeps it simple.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文