Jump to content
  • Advertisement
Sign in to follow this  
Crabtastic

[java] Model-View-Control Pattern in Java Games

This topic is 4411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been doing some Java programming and started doing game programing. I'm not trying to do anything fancy (mostly 2D toys for testing out AI ideas), but I wanted to build my own basic game library and practice some OO design along the way. I chose the Model-Viewer-Controller pattern as how I wanted to organize the code; however, I've run into a problem. Specifically, I can't figure out a good way to separate out the controller and the viewer classes. From my limited knowledge of how mouse events are handled in Java, it seems like a JPanel is the most natural choice to be both a viewer and a controller since it will be the surface for rendering to (the viewer) as well as the component that handles mouse clicks (the controller). Right now, my plan is to have a superclass which itself extend JPanel (let's call it ViewerController) that contains the viewer code. I wil then have the specific control code appear in that class's subclasses (say, PacManVC). Then, I will have a model interface that will enforce a standard way of communicating between the model and the visualization code in ViewerController. Does that sound reasonable from a design perspective, given what I can work with off the shelf in Java? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
Hmm.. I don't know if a game's code can adapt well in MVC.. It seems to me that such a pattern can't be applied successfuly on such paradigm, or even if it does, the benefits won't compensate for the resulting complexity.

The only pattern I've seen in games - I say "only" because I haven't been in touch with others, not that it is the only one that exists - is the MDA: Model, Dynamics, Aesthetics.

Model designes the game logical classes, like Entity, GameObject, Enemy, Player, etc. Dynamics is the implementation of the relationships between these classes and the "engine", and also - most importantly - the implementation of the game's rules. Aesthetics, as the name suggests, deals with the user experience, from input to output: playability, sound, video, ai, etc.

However, despite the fact that such a pattern exists, it by no means suggests that totally reusable code can be written for it, as in the MVC paradigm. MVC in standard desktop apps aim to free the end programmer of tedious and repetitive work, while designing software artifacts that are reusable in future projects. I fail to see the same benefit applied to a game's code, though. You may certainly have your own reusable, "toolkit-like" classes, but they will always require tweaks from one game to another.

Just my $0.02, though [grin]

Share this post


Link to post
Share on other sites
I always though MVC was more of a web/graphical thing then a game pattern, but hey looks like I'm wrong.

In any case regarding MVC, usually i just group JPanels and have their own controllers, and event handlers etc.

However Swing was actually designed with MVC in mind as apparent from this page: http://en.wikipedia.org/wiki/Model-view-controller , so you should have no problems getting swing to work with the pattern.

Though i find adding an extra handler class just adds more uneeded work


(in response to Son of Cain)
I originally was going to write the exact same thing. then after researching interestingly enough i found a couple of articles that made sensible use of MVC within a game :o. So uh, I wont say anything....

However just because I found these articles doenst meen I'm converted, but as I look at my code I've actually used some of these concepts unintentionally...

Share this post


Link to post
Share on other sites
Thanks for the quick replies, Hard Rock and Son of Cain.

I took a game programming class and my teacher had interesting ideas about the design of games that proved useful when I was working on the final project (the class was in Python and we used library like PyGame). He spent some time emphasizing the desirability of MVC, but then went on to ignore it when he was actually building his sample game during the lectures. I thought he was just trying to save time during lectures and I figured I could pursue it one my own. But, I guess there's a good reason for that, because of the character of games. I 've been working on implementing it and it just seems like a headache. Thanks for confirming my suspicions.

Also thanks for the reference to the Model, Dynamics, Aesthetics. I'll look into that.

Best.


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!