<< Chapter < Page Chapter >> Page >

A screen shot of the output from the program while the large spider is in its blue state is shown in Figure 3 .

Figure 3 . Another output from the program named Slick0160b.
Missing image.

What you have learned

In the previous module, you learned how to make sprites move at a constant speed in front of an image in the face of widely varying frame rates. You alsolearned about a rudimentary form of collision detection.

What you will learn

In this module, you will learn about using the draw , drawCentered , and drawFlash methods of the Image class.

General background information

The Slick2D Image class defines about ten overloaded versions of the draw method. We will investigate several of them in this module.

The class also defines three overloaded versions of the drawFlash method along with a method named drawCentered . We will also investigate some of them.

Discussion and sample code

The program named Slick0160a

Beginning of the Slick0160a class

Will discuss in fragments

A complete listing of this program is provided in Listing 13 . As is my custom, I will break this program down and discuss it in fragments.

Listing 1 shows the beginning of the class down through the constructor.

Listing 1 . Beginning of the Slick0160a class.
import org.newdawn.slick.AppGameContainer; import org.newdawn.slick.BasicGame;import org.newdawn.slick.GameContainer; import org.newdawn.slick.Graphics;import org.newdawn.slick.Image; import org.newdawn.slick.SlickException;import org.newdawn.slick.Color; public class Slick0160a extends BasicGame{Image rabbit = null;float rabbitWidth;float rabbitHeight; //Frame rate we would like to see and maximum frame // rate we will allow.int targetFPS = 60; //----------------------------------------------------//public Slick0160a(){//constructor //Set the titlesuper("Slick0160a, baldwin"); }//end constructor//----------------------------------------------------//

As usual, it is necessary to declare several import directives that point to classes in the Slick2D library. Also, as in several previous modules, thenew class extends the Slick2D class named BasicGame .

Listing 1 declares several instance variables, initializing some of them.

The constructor simply sets the title on the game window.

The main method

The main method is shown in Listing 2 . There is nothing in Listing 2 that you haven't seen in several previous modules.

Listing 2 . The main method.
public static void main(String[] args)throws SlickException{ AppGameContainer app = new AppGameContainer(new Slick0160a(),512,537,false); app.start();}//end main

The init method

The init method is shown in Listing 3 .

There is nothing in Listing 3 that you haven't seen in previous modules.

Listing 3 . The init method.
@Override public void init(GameContainer gc)throws SlickException { rabbit = new Image("rabbit.png");rabbitWidth = rabbit.getWidth(); rabbitHeight = rabbit.getHeight();System.out.println("rabbitWidth: " + rabbitWidth); System.out.println("rabbitHeight: " + rabbitHeight); gc.setShowFPS(false) ;gc.setTargetFrameRate(targetFPS);//set frame rate }//end init

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Anatomy of a game engine. OpenStax CNX. Feb 07, 2013 Download for free at https://legacy.cnx.org/content/col11489/1.13
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Anatomy of a game engine' conversation and receive update notifications?

Ask