How the game is started, paused and stopped Assignment Help

Assignment Help: >> Programming the game >> How the game is started, paused and stopped

How the game is started, paused and stopped:

Starting

Game defines a self-starting thread. The Game constructor creates a Thread instance, passing the new Game object itself as an argument to the Thread constructor, then starts this thread.

When the MIDlet creates a new Game, the latter starts itself unaided. The MIDlet then makes the game the current display.

Pausing

The MIDlet can be paused by an incoming phone call at any time and our game must obviously cater for such interruptions, otherwise users probably won't want to play it!

When an incoming call ends, the MIDlet's startApp() method will be called again. If we have simply written:

public void startApp()
{
Game game = new Game();
display.setCurrent(game);
}

then it will create a completely new instance of Game from scratch, instead of letting the user pick up the original game again where they left off.

To overcome this we use a variable to remember whether the game has already started, and if it has we don't create a new Game instance.

private boolean started = false;
...
public void startApp()
{
if (!isStarted()) // if the MIDlet has not run yet
{
game = new Game(this);

Display.getDisplay(this).setCurrent(game);

setStarted(true); // record game has been started
}
// else resume the original game
}

This works as we want, but there is still another problem to deal with. While the user is taking the phone call, the game thread will continue running! So when they return to the game they will discover it has moved on without them.

Obviously this is not acceptable, so to prevent it we must stop the game thread when the application is paused and restart it when the application resumes. This is done as follows. In U12MIDlet we include suitable statements in startApp() and pauseApp().


public void startApp()
{
if (!started) // if the MIDlet has not run yet
{
// start the game as shown previously
}
else // resume the original game
{
synchronized (game)
{
game.setRunning(true); // ask Game to restart game.notify();
}
}
}

public void pauseApp()
{
// request that the game pause game.setRunning(false);
}

The setRunning() method used in pauseApp() modifies a boolean variable running in Game. This variable keeps track of whether the game should be running or not. Each time the game loop in Game is executed, the thread checks this variable and if it is set to false, the thread calls wait() to put itself in a blocked state, so pausing the game. The game loop from Game is shown next:

while (!isRunning())
{
synchronized (this)
{

try
{

}

this.wait();

catch (Exception e)
{
e.printStackTrace();
}
}
}

Thus, when the MIDlet invokes game.setRunning(false), the game thread calls wait() and blocks. It remains blocked until the MIDlet invokes game.setRunning (true) and then game.notify() to wake it up again from its startApp() method.

There is one new Java idiom here that you haven't met before. If notify() is used to wake up the thread blocked by the wait(), both must refer to the same lock. In Unit 8 we used synchronized methods and the lock was the one belonging to the object the methods were invoked on. In this case we are using what's called a synchronized block, which has the structure:

synchronized (someObject)
{
//code
}

Stopping

Our game will stop naturally when the number of ticks expires and the game loop finishes. But what if the user exits before thatfi The destroyApp() method of the MIDlet should be able to stop the thread at that point.

This is not the same as putting the thread into a blocked state; we want it to actually terminate. How can this be done Well, just as we had a variable that indicated the thread should block, we can have another that indicates it should terminate. Then at the start of the game loop we include a statement that checks this variable. The break statement is a Java facility that allows us to immediately exit a loop.

while (gameTicks >= 0) // loop until time runs out
{
if (requestedToStop)
{
break; // jump out of loop
}

// rest of game loop code

If requestedToStop is true then the game loop will abruptly terminate. Of course, using break is not the way we usually exit from loops, but in this case it makes for a simple way to end the program.

If the user presses the Exit key, the commandAction() method in the MIDlet calls the MIDlet's destroyApp() method and the latter requests the game thread to stop.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your How the game is started, paused and stopped homework and assignments? Live How the game is started, paused and stopped experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer How the game is started, paused and stopped homework help, java assignment help and How the game is started, paused and stopped projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd