On this page:
Functional Worlds
Functional Worlds: The Model
Functional Worlds: The View
Functional Worlds: The Game End
Functional Worlds: The Big Bang
Mutable Worlds
Mutable Worlds: The Model
Mutable Worlds: The View
Mutable Worlds: The Game End
Mutable Worlds: The Big Bang
Mutable Applets
The World Applet Class
The Applet Web Page
Model Applet.html
Sound World
Sound World: The Model
Sound World: The View
Sound World: The Game End
Sound World: The Big Bang
Sound Applets
The World Applet Class (from impsoundworld)
Applet Web Page (from impsoundworld)
Model Applet.html (from impsoundworld):
Two Samples
Version: 5.2.1

Worlds

This collection of five libraries (together with the world-images library and optionally the tunes library) allows a novice programmer to model the behavior of an interactive graphics-based game where the game behavior changes in reponse to the key presses, mouse clicks, and as time elapses (monitored by the ticking clock). The programmer also designs the view of the game at each clock tick, or as it changes in response to the key and mouse events.

More advanced programmers will enjoy augmenting their game with sound effects and musical themes.

Functional Worlds

The javalib.funworld library is designed to support the design of interactive graphics-based game using functional programming style.

Functional Worlds: The Model

The programmer designs a class that extends the abstract class World. This class consists of the objects of the game and any information the programmer needs to report on the game progress (e.g. the current score for the game). The game actions are implemented by overriding the following methods:

World onTick()

This method produces a new instance of the world as it should be after one tick of the clock has passed.

World onKeyEvent(String ke)

This method produces the world in response to the user pressing a key on the keyboard. For each key we define the resulting new world, and return the current world for all irrelevant key presses.

World onMouseClicked(Posn p)

This method responds to a mouse click anywhere on the game’s canvas. The event handler that invokes the method provides the location of the mouse when it was clicked. The programmer defines the new world in response to this mouse click at the given point.

Additional mouse events that may be handled include onMouseEntered, onMouseExited, onMouseReleased, and onMousePressed, but we do not expect these to be used extensively.

Notice, that if the programmer does not need to respond to key events, or does not wish to run the timer, the default methods in the abstract class World just return the current instance of the World and no work needs to be done.

Functional Worlds: The View

To display the world image the programmer must implement the method

WorldImage makeImage()

that produces the image of the world to be displayed.

Functional Worlds: The Game End

Ending the game after a tick:

To check for the end of the game after each tick of the clock, the programmer can override the method

WorldEnd worldEnds()

This method produces an instance of a class WorldEnd that consists of a boolean value indicating whether the world is ending (false if the world goes on) and the WorldImage that represents the last image to be displayed - for example announcing the winner of the game, or the final score.

The world invokes this method automatically after each tick and checks whether the world should end.

Ending the game after a key event or a mouse event:

The game can also end when a key event or a mouse event is processed. In that case when the method that handles the event determines that the game should end, it invokes the method

World endOfWorld(String s)

The String argument is the message announcing the end of the game (and possibly the result of the game). The method produces a new instance of the class WorldEnd with the boolean value true and the last image generated by the method

public WorldImage lastImage(String s)

that the programmer can override. By default, the method lastImage just invokes the makeImage method for the current world and ignores the provided String.

Functional Worlds: The BigBang

Finally, to run the world game , the program invokes the method

boolean bigBang(int width, int height, double speed)

within one of the test methods in the Examples class.

If the programmer does not wish to run the timer and respond to the ticks, he can invoke the bigBang method with the time 0.0, or omit the last argument entirely.

Mutable Worlds

Mutable Worlds: The Model

The javalib.impworld library is designed to support the design of interactive graphics-based game using mutable (imperative) programming style.

The programmer designs a class that extends the abstract class World. This class consists of the objects of the game and any information the programmer needs to report on the game progress (e.g. the current score for the game). The game actions are implemented by overriding the following methods:

void onTick()

This method mutates the state of the world as it should be after one tick of the clock has passed.

void onKeyEvent(String ke)

This method mutates the state of the world in response to the user pressing a key on the keyboard.

void onMouseClicked(Posn p)

This method mutates the state of the world in response to a mouse click at the given point on the game’s canvas. The event handler that invokes the method provides the location of the mouse when it was clicked.

Additional mouse events that may be handled include onMouseEntered, onMouseExited, onMouseReleased, and onMousePressed, but we do not expect these to be used extensively.

Notice, that if the programmer does not need to respond to key events, or does not wish to run the timer, the default methods implements in the abstract class World just do nothing.

Mutable Worlds: The View

To display the world image the programmer must implement the method

WorldImage makeImage()

that produces the image of the world to be displayed.

Mutable Worlds: The Game End

Ending the game after a tick:

To check for the end of the game after each tick of the clock, the programmer can override the method

WorldEnd worldEnds()

This method produces an instance of a class WorldEnd that consists of a boolean value indicating whether the world is ending (false if the world goes on) and the WorldImage that represents the last image to be displayed - for example announcing the winner of the game, or the final score.

The world invokes this method automatically after each tick and checks whether the world should end.

Ending the game after a key event or a mouse event:

The game can also end when a key event or a mouse event is processed. In that case when the method that handles the event determines that the game should end, it invokes the method

void endOfWorld(String s)

The String argument is the message announcing the end of the game (and possibly the result of the game). It is passed to the method:

public WorldImage lastImage(String s)

that the programmer can override. By default, the method lastImage just invokes the makeImage method for the current world and ignores the provided String. Typically one would place the given message onto the world image (at some desired location and in the specified color).

Mutable Worlds: The BigBang

Finally, to run the world game , the program invokes the method

void bigBang(int width, int height, double speed)

within one of the test methods in the Examples class.

If the programmer does not wish to run the timer and respond to the ticks, he can invoke the bigBang method with the time 0.0, or omit the last argument entirely.

Mutable Applets

The javalib.appletworld library allows the programmer to convert easily the games designed in the mutable (imperative) programming style into Java applets.

The WorldApplet Class

The game design is identical to the design of imperative games using the impworld library, with the code for setting up the world size and defining the initial world defined in an additional class that extends the WorldApplet class. The game can then be displayed in a web page as an interactive applet.

Once the game runs with the impworld library we change the imports to the appletworld library. We then define the class that extends the abstract class WorldApplet as follows:

/**

 * Applet wrapper for ... world

 */

public class MyWorldApplet extends WorldApplet{

 

  /** Produce a new MyWorld */

  public World getNewWorld(){

    ... initialize any fields you need ...

 

    // return the world you would use in the bigBang method

    return new MyWorld(...);

  }

 

  /** Set the size of this world */

  public void setWorldSize(){

    // the width you would use in the bigBang method

    this.WIDTH = 600;

 

    // the height you would use in the bigBang method

    this.HEIGHT = 300;

  }

 

  /** Optionally change the range of the slider

   * so that the highest tick rate is factor times as fast

   * as the default (here it is twice as fast)

   */

  public double setSpeedFactor(){

    return 2.0;

  }

}

We see that these three methods contain all the information contained in the invocation of the bigBang method when using the impworld library.

The Applet Web Page

To run the program we need to give the applet access to the libraries we use and define the web page that will display and run the applet. The following steps accomplish this:

ModelApplet.html

          <html>

  <head>

    <title>

      My World Applet

    </title>

  </head>

 

  <body>

    <h2> My World Applet </h2>

 

    <applet

      code="MyWorldApplet.class"

      archive="./appletworld/appletworld.jar,

               ./tester/tester.jar,

               ./colors/colors.jar"

width="600"

height="330">

Applet does not run

</applet>

 

<p>Add here any other content... </p>

<hr></hr>

 

  </body>

</html>

Sound World

The javalib.soundworld library extends the functionality of the impworld library by allowing the programmer to add musical background and sound effects to the game. The tunes library handles the design of the musical sequences and sound effects and provides the connection to the standard MIDI synthesizer that generates the sounds.

The programmer designs sound effects or a musical theme that is to be played as the game goes on, or when some special events happen in the game. The programmer then includes in the definition of the inTick events and the onKeyEvent events the collection of the tunes that should be played.

The MIDI system provides a synthesizer that can play up to 15 different musical instruments at a time, plus the percussion instruments. The selection of the instruments that play on the different channels of the MIDI synthesizer is called a program. The library provides the default program, but the programmer may change the MIDI program at any time during the game.

The design of the tunes library and some samples of how to design sound effects and musical themes is described in the documentation for the tunes library.

Sound World: The Model

As with the impworld library, the programmer designs a class that extends the abstract class World and implements the methods:

void onTick()

void onKeyEvent(String ke)

void onMouseClicked(Posn p)

However, the first two methods include the option of adding MIDI generated sound effects.

The class World contains two TuneBuckets that the programmer can use to define the music and sound to be played on each tick, and in response to a key press: the tickTunes and keyTunes.

If a programmer adds a Tune to the tickTunes the Tune (given by a collection of notes to be played on any of the currently programmed instruments) will play on for the specified duration.

The Tunes added to the keyTunes TuneBucket in response to a specific key press will play as long as this key remains pressed.

For now there is no option for adding sound effects on mouse clicks.

Sound World: The View

To display the world image the programmer must implement the method

WorldImage makeImage()

that produces the image of the world to be displayed.

Sound World: The Game End

Ending the game after a tick:

To check for the end of the game after each tick of the clock, the programmer can override the method

WorldEnd worldEnds()

This method produces an instance of a class WorldEnd that consists of a boolean value indicating whether the world is ending (false if the world goes on) and the WorldImage that represents the last image to be displayed - for example announcing the winner of the game, or the final score.

The world invokes this method automatically after each tick and checks whether the world should end.

Ending the game after a key event or a mouse event:

The game can also end when a key event or a mouse event is processed. In that case when the method that handles the event determines that the game should end, it invokes the method

void endOfWorld(String s)

The String argument is the message announcing the end of the game (and possibly the result of the game). It is passed to the method:

public WorldImage lastImage(String s)

that the programmer can override. By default, the method lastImage just invokes the makeImage method for the current world and ignores the provided String. Typically one would place the given message onto the world image (at some desired location and in the specified color).

Sound World: The BigBang

Finally, to run the world game , the program invokes the method

void bigBang(int width, int height, double speed)

within one of the test methods in the Examples class.

If the programmer does not wish to run the timer and respond to the ticks, he can invoke the bigBang method with the time 0.0, or omit the last argument entirely.

Sound Applets

The javalib.appletsoundworld library allows the programmer to convert easily the games designed in the mutable (imperative) programming style that include the sound effects or a musical background (defined with the javalib.soundlib library) into Java applets.

The WorldApplet Class (from impsoundworld)

The game design is identical to the design of imperative games using the impsoundworld library, with the code for setting up the world size and defining the initial world defined in an additional class that extends the WorldApplet class. The game can then be displayed in a web page as an interactive applet.

Once the game runs with the impsoundworld library we change the imports to the appletsoundworld library. We then define the class that extends the abstract class WorldApplet as follows:

/**

 * Applet wrapper for ... world

 */

public class MyWorldApplet extends WorldApplet{

 

  /** Produce a new MyWorld */

  public World getNewWorld(){

    ... initialize any fields you need ...

 

    // return the world you would use in the bigBang method

    return new MyWorld(...);

  }

 

  /** Set the size of this world */

  public void setWorldSize(){

    // the width you would use in the bigBang method

    this.WIDTH = 600;

 

    // the height you would use in the bigBang method

    this.HEIGHT = 300;

  }

 

  /** Optionally change the range of the slider

   * so that the highest tick rate is factor times as fast

   * as the default (here it is twice as fast)

   */

  public double setSpeedFactor(){

    return 2.0;

  }

}

We see that these three methods contain all the information contained in the invocation of the bigBang method when using the impsoundworld library.

Applet Web Page (from impsoundworld)

To run the program we need to give the applet access to the libraries we use and define the web page that will display and run the applet. The following steps accomplish this:

ModelApplet.html (from impsoundworld):

          <html>

  <head>

    <title>

      My World Applet

    </title>

  </head>

 

  <body>

    <h2> My World Applet </h2>

 

    <applet

      code="MyWorldApplet.class"

      archive="./appletsoundworld/appletsoundworld.jar,

               ./tester/tester.jar,

               ./colors/colors.jar"

width="600"

height="330">

Applet does not run

</applet>

 

<p>Add here any other content... </p>

<hr></hr>

 

  </body>

</html>

Two Samples

The DrawFaceSoundApplet.html illustrates how to play notes of longer duration, how to end the game either on tick, or on key event.

Note, thet even though the game canvas size is only 100 by 100, we must make the space for the frame bigger — at least 200 pixels wide and 230 pixels high - to leave the space for the controls.

_________________________________________________________________________________

The TickyTackSongApplet.html plays the familar tune with the familiar scene...