views:

240

answers:

8

For quite awhile I have been trying to make a simple "game" in Java that is really just an applet with a square and a grid. What I want it to do in the end is the user clicks and the square will move to where the user clicked rounded to the nearest grid square.

The problem is I am a self taught beginner and I am having a hard time figuring out how to actually structure the program, some examples:

  • should I have a separate class listening for mouse clicks?
  • When I receive a click should I send it to some other object that represents the box and let it decide what it wants to do or just call some function that makes the box move?

I really want to learn all this "when to use what" stuff for myself so any links or general advice is appreciated.

+2  A: 

One main principle of good software development is the Single Responsibility Priciple. It states that a function or class should only have one responsibility.

This way your classes and objects shouldn't become too big and unmanageable.

Ikke
This is also known as having High Cohesion. A class should only do one thing. (A class shouldn't represent the game board AND the pieces, if you were writing a game of chess, for example.)
JasCav
It's also one meaningless principle, based on a circular argument over definition of "responsibility".
ima
+1  A: 

I think one of the most important concepts to master when developing software is the concept or Orthogonality. It's not the simplest definition, but in essence it means that one component (such as reading mouse clicks) shouldn't be directly tied to an unrelated component (moving a square on the screen).

In your case, the code reading mouse clicks should be separate from the code that actually moves the box. Whether you implement this as inner/anonymous classes or not is up to you. But if you follow the Orthogonality principle, it will be easy to change at a later date should you change your mind.

Jason Nichols
for that MVC pattern (model view controller) or MVP pattern (model view presenter) colud be used..And not to write new post..i suggest TDD(test driven desig) and DDD(domain driven design) for any object oriented language..I is not easy proccess learning those things..but it is benifitial
shake
A: 

Step 1 - find the demo applets supplied by Sun. http://java.sun.com/applets/jdk/

Step 2 - read those demo applets. At least three. Preferably all of them.

One you've read several applets, you should see a little more clearly how to organize programs. You can then ask questions with a lot more focus pointing to specific applet examples and your specific programming problem.

S.Lott
The demo applets are not well written.
Tom Hawtin - tackline
@Tom Hawtin: Real working code beats the kind of discussion that can circulate around a question this vague. Your comment, however true, isn't helpful. Do you have a better set of applet examples?
S.Lott
+2  A: 

What you're really asking is how to develop a game, which is notably different from a typical Java application. However, I'll give you a few ideas to at least point you in the right direction.

  • Take advantage of the fact that Java is an object-oriented language. That is, objects should each have their own responsibility.
  • Separate your game into three key layers: the application layer, the game logic layer, and the presentation layer.

    • The application layer should contain all of your helpers and generic subsystems, things like random number generators, text parsers, file access modules, mesh loaders, etc.
    • The game logic layer should implement all of the rules of your game, and be responsible for maintaining canonical state. Basically, when you press the "W" on the keyboard to move forward, the game logic layer should receive MOVE_FORWARD_REQUEST from the UI.
    • The presentation layer should be responsible for two things: getting input, and rendering your world. When it gets input, like the "W" key, it should map that to an action, and send that to the game logic layer to be processed. Then, it should render the world based on whatever the game logic told it to do.

Game development is obviously an entire realm with many books dedicated to it. One of my favorites is Game Coding Complete, which does focus on C/C++, but should give you a good idea about how you ought to structure your game.

Good luck!

Ed Altorfer
Thanks for the examples and the book link. This is just what I need to get me going on the right track.
Nagrom_17
I wish you the very best. Everyone has to start somewhere...it takes a while to really understand how to structure programs. When you're getting started though, you can always forego that and just hack it together to get SOMETHING working. You'll probably see what's wrong with it yourself. :)
Ed Altorfer
A: 

Yeah, I'm a beginner programmer myself. Yeah, segregating functionality across multiple classes is a good way to reduce complexity and increase cohesion of individual classes.

Increasing cohesion good because by having more complex data structure your algorithms become less complex and your code is less dependent on each other.

For instance in your case it might be a good idea to separate the classes in accordance to MVC (Model View Controler).

  • You have a Model which represents the way your game data is structured.
  • You have a Viewer which present your Model in what ever form you please.
  • Have a Controller which picks up changes in the Model (via Listeners) and then updates the Viewer

Now you can change your Model and add extra functionality requiring only small changes in the way the Viewer works.

There are many Patterns out there but there isn't a hard rule when to use one over the other. There are some cases in which you can use several and there are cases in which will require you to chose one design pattern over the other.

Daniel Fath
I would strongly recommend against trying to use MVC for a game. Model-View-Controller is more suited to medium-size web applications.
Ed Altorfer
Well, both AWT and Swing essentially force MVC on you once you start using components, so unless you willfully subvert that paradigm, you're essentially "doing" MVC whenever you're doing a Swing (AWT) GUI.
Carl Smotricz
If I recall correctly Swings forces you to use MVC on JTRee and a couple of others, rest are Model-Delegate. It was more of an example of a possibility than a real suggestion. Besides Logic/Presentation/Application layers are the rough equivalent Model/View/Controller. At least the Model/View Part.
Daniel Fath
As a concrete example, this game was specifically designed to illustrate MVC: http://robotchase.sourceforge.net/
trashgod
A: 
Carl Smotricz
A: 

Every beginning Java programmer should start with the Sun Tutorials. They are quite good.

Another good source, especially among free sources, is Bruce Eckel's "Thinking in Java", available from http://www.mindview.net/Books/TIJ/.

But the latter is a little dated compared to the former. That is why I recommend both.

Matt J.
A: 
... the user clicks and the square will move to where the user clicked rounded,
to the nearest grid square.

Here is an example of doing this in a scaled view.

trashgod