An Education Oriented Java Package
The JGameGrid Framework
JGameGrid is a class library specially designed for programming courses with a focus on object oriented programming (OOP). The concept follows the well-known educational principles to keep the learning curve gently and motivate the student by analyzing, developing and playing computer based games. The recent research in game based learning (GBP) shows that using games in the classroom makes learning easier and less frustrating. Students may be so amused that they even do not realize they are learning. We want to make programming a pleasure and not a frustration.
When creating computer games you will encounter many classical problems of program design and implementation, and, what is most important for us, you realize how modern programming concepts like OOP is imperative to reduce the complexity of just the most simple game. The game figures are concrete realizations of software objects and it is natural to model their state and behavior by instance variables (attributes, fields) and methods.
Every kid knows playing video games is fun, but making them is serious business. Until now game programming was just too complicated for beginners and could badly serve in introductory programming courses. JGameGrid reduces the effort to create game-oriented Java applications by an order of magnitude compared to Java-API programming, without hiding essential parts of the application code or restrict the student by using a specific integrated IDE or a particular game window. With JGameGrid you may use your favorite programming environment, from very simple to very professional, and it us up to you how to design the user interface. JGameGrid gives you an important support for creating two-dimensional computer games but does not limit your imagination and liberty.
The concept and some method names of JGameGrid are heavily influenced by another great game framework GREENFOOT, developed at the University of Kent and the Deakin University, Melbourne, but no code from the Greenfoot source distribution is included in our library. While Greenfoot is a complete graphical based learning system with its own development environment based on BlueJ, JGameGrid is a Java library that can be used with any IDE and GUI builder.
Design principle explained by an example
In most cases the user application class extends the Actor class and overwrites the method act() that is called periodically by the game thread when the application is run. As with GREENFOOT a standard navigation area may be displayed containing the buttons Step, Run/Pause, Reset and a slider to select the simulation period. This makes the skeleton of a game extremely simple. In the following example a fish moves back and forth in a reef playground.
Following the basic principles of OOP, we first declare a class Fish that is a subclass of Actor. Because we override its method act() that is automatically called in every simulation cycle by the underlying animation machine, we move the fish to the neighbour cell just by calling move(). When the fish is at the border of the playground it must turn 180 degrees. All needed methods are inherited from the class Actor, so Fish becomes a rich class without great effort.
For the sake of simplicity the application class MyGame is derived from the class GameGrid and uses one of its constructors to design the visible window with 10 horizontal and 10 vertical cells of size 60 pixels. Red grid lines are displayed and a background image from the file reef.gif in the subdirectory sprites shows the rich underwater world from the Red Sea. Then a instance of Fish called nemo is created and added to the playground in cell location 2, 4. Finally show() displays the window.
The result of these few lines of code is a full-fledged animation program. Enjoy!
Execute the program locally using WebStart.
How to use the navigation area:
Clicking the Run button starts the animation and the button text changes to Pause. Clicking Pause halts the execution and the button text changes to Run. Clicking it resumes the animation. Clicking Step performs a single animation step. Clicking Reset restores the initial situation. The slider changes the time between two consecutive calls of act(), called simulation period. It works even while the animation is running.
Important remarks about the design strategy:
Many of the methods of JGameGrid can be implemented more or less easily by the user based on a few elementary library methods. This would be a good exercise for beginners, but we think that there is no need (and time) to reinvent the wheel. The student should better spend his effort in his specific application development.
JGameGrid's simulation thread "serves" actors by sequentially calling their act() method. The order of this sequence may be modified, but all actors must collaborate friendly and not execute long lasting code that would cause starvation of the following actor. The following flow diagram visualizes the principle.