<< Chapter < Page Chapter >> Page >

There is nothing new in the constructor in Listing 1 .

The main method

There is also nothing new in the main method in Listing 2 .

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

The overridden init method

The overridden init method is shown in Listing 3 . There is quite a bit of new material in Listing 3 .

Listing 3 . The overridden init method.
public void init(GameContainer gc) throws SlickException {ladybug = new Image("ladybug.png"); background = new Image("background.jpg");gc.setShowFPS(false);//disable FPS displaygc.setTargetFrameRate(60);//set frame rate }//end init

Two new Image objects

Listing 3 begins by instantiating two new Slick2D Image objects from the image files discussed earlier and saving those object's references in twoof the instance variables that were declared in Listing 1 .

(Note that in this case, the image files were located in the same folder as the source code for the program. Therefore, a path specification to the imagefiles was not needed.)

I will remind you again that the Slick2D Image class is different from the Image class in the standard edition Java library.

Don't display FPS

You may have noticed that the FPS display is missing from the upper-left corner of Figure 5 . That is because it was disabled by the call to the setShowFPS method in Listing 3 , passing false as a parameter to the method.

Set the target frame rate

The last statement in Listing 3 sets the target frame rate to 60 frames per second.

An empty update method

The update method in Listing 5 is empty so there is no point in showing it here.

The overridden render method

The overridden render method is shown in Listing 4 .

Listing 4 . The overridden render method.
public void render(GameContainer gc, Graphics g) throws SlickException{//Note that the names of the drawMode constants seem // to be backwards.//Draw the background and two versions of the// ladybug by calling a draw method of the Image // class.g.setDrawMode(g.MODE_NORMAL);//honors transparency background.draw(0,0);ladybug.draw(leftX,leftY,leftScale); g.setDrawMode(g.MODE_ALPHA_BLEND);//no transparencyladybug.draw(rightX,rightY,rightScale);//Draw a third version of the ladybug by calling // a drawImage method of the Graphics class.g.setDrawMode(g.MODE_NORMAL); g.drawImage(ladybug,middleX,middleY);}//end render

Draw the background and the leftmost ladybug

Listing 4 begins by calling the setDrawMode method on the incoming Graphics parameter to set the drawing mode to MODE_NORMAL as described earlier. Then it calls one of the overloaded draw methods of the background Image object and the ladybug Image object to draw the background and the leftmost ladybug in Figure 5 .

Notethat the drawing coordinates and the scale factor are passed to the draw method.

Also note that this drawing of the ladybug image honors transparent pixels.

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