0

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, thanks for you answer! One Question: do you really mean that I should just notify the paddle that specific button was pressed or do I interpret the event already in the controller? I mean paddle.buttonPressed() or paddle.shoot()? And how does the paddle communicate back to the controller? I guess, I must set the controller as delegate for the paddle object. Is this really a nice design?
I don't see any reasons to notify the controller back. You should do something like: //On controller public void actionPerformed(MouseEvent evt){ paddle.notifyMove(evt.getX(), evt.getY()); //if a click paddle.shoot() } But you really just create the shot inside the processAI. Cause you game loop can't create objects inside the Input step,(probably you can get a ConcurrentCollectionModificationException cause you thread event will overlap the process that can be running, you will never know)
1
  • 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.

3 Comments

The model could also be the level if you plan on implementing multiple layouts. That's not addressed here, but thought I'd throw it out there.
Thanks for your answer! 1) I think I can live with Breakout being a JFrame, if that's your point. 2) Yeah, I agree. I just created them in the view because I thought they are view objects as they derive from Rectangle2D. How would you suggest to pass them to the view? When calling view.repaint()? Maybe overload this method to something like view.repaint(paddle, ball, bricks)? @Riggy: Brilliant idea, thanks! So the Level holds all its objects (paddle, ball, ..)?
@nubbel as far as passing the model to the view, the MainView class could receive references in the constructor or a separate method.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.