To compile this you will need to ensure the bluetooth api is part of your wireless toolkit.
To compile this you will need to ensure the bluetooth api is part of your wireless toolkit.
Congrats...
You have just downloaded a Re-useable game framework.
The plan is over the next few weeks to take you from this framework to a complete BlueTooth two game in the the shape of Absorbed.
First Create a new Project in your wirless toolkit and give it the Midlet class name of GameMIDlet
Now copy the src & res directories from the FrameWork directory to your new project directory.
Once you have done this build the project and run it in your wireless toolkit.
A splash screen should appear, and after a short pause the main menu screen should appear.
If you've got that far then move on and we'll discuss the res & src directories you copied from Framework directory.
The Res file contains the following:
en.txt A text file (in english) containing all the text which will be displayed within the game.
i2.png A graphics file conating menu icons
p.png Plastic Cow Games logo
t.png Title of the game
The src file contains the following:
GameMIDlet.java Entry point of the game
GMCanvas.java This is where most of the work is done. Contains the main game loop etc
SplashScreen.java Displays a splash screen
Dictionary.java This loads the en.txt file and assigns values to each line in the text file
GameEffects.java Handles sound, including loading the music / effects. More on this later
Standard.java Handles functions such as displaying text in boxes, saving to RMS etc.
BGLayer.java Handles multi directional scrolling.
BlueTooth.java An almost empty file for now. We'll deal with this later.
A closer look at the source
===========================
GameMIDlet.java
---------------
StartApp() When the midlet is started for the first time, a splash screen is displayed otherwise the Game Canvas thread is started.
PauseApp() When the midlet is paused (due to incoming call or other event) this method is called. If the Game Canvas is the active display then the thread is stopped.
DestroyApp() This method is called when exiting the midlet. It stores the current game info (if required) and stops the game thread.
init() Called by the SplashScreen. Creates an instance of GMCanvas which is our game canvas.
splashScreenDone() A call back for SplashScreen which starts our game canvas thread
menuListQuit() A method used to manually exit the game
createImage(String) Creates an image from a file name with exception trapping incase the file doesnt exist etc.
And thats it. Not much to it really.
GMCanvas.java - Extends Canvas Implements Runnable
-------------
Before we start to look at this I should mention the pre-processor directives.
I've left them in for anyone who's interested but commented out the code so that this will run without any problems on the wireless toolkit.
Otherwise just ignore any line begining //#
Class Variables
There are comments in the code giving an idea of what they do.
GMCanvas(GameMIDlet)
Constructor:- not doing a great deal here, just ensuring the gameMode is set and key events are allowed.
keyReleased(int keyCode)
keyPressed(int keyCode)
I decided not to use getKeyStates() and made my own. This is because I found some issues with using getKeyStates and keyReleased() & keyPressed() methods.
Softkeys are used to navigate some portions of the menu and pause the game. This is dealt with in keyPressed().
My keyStates variable gets updated in these two methods.
changePause()
Used when a manual attempt to pause the game is made. ie via softkeys.
init()
I use this to load the bulk of the graphics. At the moment it just loads the menu icons.
start()
stop()
Deal with issues for starting and stopping the thread for this runnable.
run()
The main game loop I guess. Starts off by initializing some counters for the frame timer then starts the while loop.
First do some bluetooth stuff to retrieve any sent data, but we can ignore this for now.
Then call tick(), draw() and then flushGraphics().
Also calculates the framerate and does its best to ensure the game runs at a set framerate.
tick()
This is a state driven game framework and nothing shows this more than the tick() method with the game state being held in a variable called gameMode.
Have a quick look at this method, we'll go into it in more depth later.
updateMenu(boolean up, boolean down, boolean fire)
Handles game menu navigation and what to do when you select an option. Basically you pass in the input from the user and the action is performed here.
I must admit this isnt the best piece of code i've ever written but it does the job and chances are you'll want to write your own menu code anyway.
I'll explain this a little as it looks quite confusing at first.
Variables used are:
optionPage - holds the menu option page you are on
option - holds the option you are on within that option page
MAX_OPTIONS[] - holds the number of options for each page
options[] - holds whether or not an option is available - i've tied this in with the optionPage variable so it can look untidy at times.
playInput(int p, int d)
This is going to handle the players input and process his key presses. Not much here yet though!
btAbort()
More bluetooth stuff to handle abort and disconnect from a bluetooth game.
sendBlueTooth()
getKeys(boolean bSend)
Sets some key pressed variable based on my keyStates variable. Also makes a call to send this info if the game is 2 player.
drawTitle(Graphics g)
drawCredit(Graphics g)
drawMenu(Graphics g)
drawSoftKey(Graphics g, int l, int r)
draw()
You dont need to be Eintien to guess that these methods draw something on the screen. draw() is the main method and again is state driven.
Towards the end of this method the frame rate is displayed.
storeGame()
Performs some game state checks to see if we need to store the game info in the RMS.
setPosition()
collTest()
restartLevel(int lvl)
I'll go into these later when we introduce more elements to the framework.