开发者

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. 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. 开发者_JAVA百科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.


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.


  • 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.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜