In Java, I wrote a full-fledged model (and basic view and controller) for a disk/squares jewel game. The game, to simplify, is a simple kinematics physics simulator, where a user must flick a disk to hit jewels but miss other blocks.
There are three kinds of objects:
- squares – which are static objects that have a center and width and never move
- disks – which have a center, radius, and velocity.
- jewel – this is a special square and the goal is to get a disk to hit the jewel
- The initial screen consists of a collection of squares together with one or more jewels and one or more disks.
- The user can catch a disk (by touching it if using a GUI) and can move it horizontally.
- To also get vertical movement the user must flick it (in a GUI, let go of the disk, then rapidly touch it and flick it) to set its new velocity.
- Disks that hit squares become static and stick to the disk as if with super glue! Moreover, any disk that hits a static disk likewise becomes static.
- Disks, which have not been captured by being touched by the user, are governed by the force of gravity where the units of distance are meters.
- The goal of the game, given an initial configuration, is to flick the disks so that they hit the target square.
- Only one disk can be moving at a time.
- Once the user gets all of the jewels, or runs out of disks, or runs out of time, the game is over. If the user has collected all jewels, then they advance to the next level. Otherwise, they start over.
Abstract and note:
Check github and the screencast for an understanding of how the code works.
In brief – disks, blocks and jewels all extend GameObject. Velocity is expressed as a custom vector object. All these things are collected in a GameMap, which also checks to see if things collide and holds physics constants. GameDriver takes in updates of clock-time elapsed, chops them into defined slices of game-time, and updates the game one slice at a time. The flow of the update function looks something like this:
- Is the game still running?
- Am I being asked to update too large a slice a time?
- If so, update one time-slice, then restart this function by asking it to update (original – timeslice) time.
- Am I out of time?
- Am I out of free disks?
- For each disk that is not stuck:
Update its position by one timeslice
Check to see if it’s now frozen.
Check to see if it hit a jewel.Update the new velocity it has due to kinematics.
- Update all variables as appropriate
- Update game time elapsed.
Note that Android: Disks and Squares Game extends this model with a functional view and controller on Android.