简体   繁体   English

MVC在一个简单的Java游戏中?

[英]MVC in a simple Java game?

Hey, I'm implementing a simple Breakout clone in Java and try to make it conform to the MVC pattern. 嘿,我正在Java中实现一个简单的 Breakout克隆,并尝试使其符合MVC模式。 I confess, I come from a web background and have used MVC only in the Php framework Symfony, never in a desktop application. 我承认,我来自网络背景,并且仅在Php框架Symfony中使用过MVC,从不在桌面应用程序中使用过。 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. Breakout extends JFrame (主类)启动MainViewController并将其视图添加到内容窗格。
  • MainViewController extends TimerTask initiates the MainView and handles its mouse and keyboard events. MainViewController extends TimerTask启动MainView并处理其鼠标和键盘事件。 Also runs the game loop (calculates the position and state of the game objects, ie Ball , Paddle , Brick s), is that right in this place? 还运行游戏循环(计算游戏对象的位置和状态,即BallPaddleBrick s),就在这个地方吗?
  • MainView extends JPanel simply draws Ball , Paddle , Brick s on the screen, no logic in here. MainView extends JPanel只是在屏幕上绘制BallPaddleBrick ,这里没有逻辑。 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. Ball extends Ellipse2DPaddle extends Rectangle2DBrick extends Rectangle2D提供了在屏幕上移动它们的方法,但是这里也进行了碰撞检测。 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. 我敢肯定,我的设计决定出了问题。

Develop game is a bit different concept but still is a MVC. 开发游戏的概念有些不同,但仍然是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) 1°读取输入(您的ViewController会对此进行计数)

2° Process Pieces AI (Like behaviours and moves with the new values from the controller) 2°加工件AI(与控制器的新值类似的行为和移动)

3° Draw on screen (You draw all you entitys on the screen) 在屏幕上绘制3°(您在屏幕上绘制所有实体)

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. 游戏的第一个对象应该在View init中的setup()方法中创建,其他(如拍击或从破碎的砖中掉落的特殊奖励)应该在第二步内创建,如果是padle控制器应告诉桨用户按下按钮进行拍摄,在创建镜头实体的过程中将其添加到实体游戏循环中,与砖块相同,但是当它注意到它时它们会创建奖励销毁。

  • I would switch Breakout and MainViewController. 我会切换Breakout和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. 您的模型实际上是Ball,Paddle和Bricks,因此我认为MainViewController应该创建它们而不是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. 它不是视图和模型的完美分离,但它保持简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM