Skip to Main Content
Killer Game Programming in Java
book

Killer Game Programming in Java

by Andrew Davison
May 2005
Intermediate to advanced content levelIntermediate to advanced
998 pages
26h
English
O'Reilly Media, Inc.
Content preview from Killer Game Programming in Java

The Worm-Chasing Application

Figure 3-3 shows a class diagram for WormChase, including all its variables and methods.

WormChase in detail

Figure 3-3. WormChase in detail

The main() function in WormChase reads the requested FPS from the command line, converting it to a delay in nanoseconds, which is passed to the WormChase() constructor:

    public static void main(String args[])
    {
      int fps = DEFAULT_FPS;
      if (args.length != 0)
        fps = Integer.parseInt(args[0]);

      long period = (long) 1000.0/fps;
      System.out.println("fps: " + fps + "; period: " +period+ " ms");

      new WormChase(period*1000000L);    // ms --> nanosecs
    }

The WormChase constructor creates the WormPanel canvas, as well as two text fields for displaying the number of boxes added to the scene (jtfBox) and the current time (jtfTime). These text fields can be updated via two public methods:

    public void setBoxNumber(int no)
    {  jtfBox.setText("Boxes used: " + no);  }

    public void setTimeSpent(long t)
    {  jtfTime.setText("Time Spent: " + t + " secs"); }

setBoxNumber() is called from the Obstacles object when a new box (obstacle) is created. setTimeSpent() is called from WormPanel.

The pausing, resumption, and termination of the game are managed through window listener methods (WormChase implements WindowListener). Pausing is triggered by window deactivation or iconification; the application resumes when the window is activated or de-iconified, and the clicking of the window ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Java Game Development with LibGDX: From Beginner to Professional

Java Game Development with LibGDX: From Beginner to Professional

Lee Stemkoski

Publisher Resources

ISBN: 0596007302Supplemental ContentErrata Page