<< Chapter < Page Chapter >> Page >

Complete program listing

Complete listings of the programs discussed in this module are shown in Listing 13 and Listing 14 below.

Listing 13 . Source code for Slick0160a.java.
/*Slick0160a.java Copyright 2012, R.G.BaldwinCalls the draw method several times in succession to illustrate the various options available with the drawmethod. Also illustrates flipping an image. Tested using JDK 1.7 under WinXP*********************************************************/ 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//----------------------------------------------------// public static void main(String[]args) throws SlickException{AppGameContainer app = new AppGameContainer( new Slick0160a(),512,537,false);app.start(); }//end main//----------------------------------------------------// @Overridepublic 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 //----------------------------------------------------//@Override public void update(GameContainer gc, int delta)throws SlickException{ }//end update//----------------------------------------------------// public void render(GameContainer gc, Graphics g)throws SlickException{ //set the drawing mode to honor transparent pixelsg.setDrawMode(g.MODE_NORMAL); g.setBackground(Color.white);rabbit.draw(0f,0f);rabbit.draw(133f,0f,new Color(1.0f,0.0f,1.0f)); rabbit.draw(266f,0f,0.5f);rabbit.draw(335f,0f,128f,192);rabbit.draw(0f,133f); rabbit.draw(133f,133f,32f,32f,96f,96f);rabbit.draw(0f,266f,256f,532f,32f,32f,96f,96f,new Color(1.0f,1.0f,0.0f));rabbit.drawCentered(399f,266f);Image tempImage = rabbit.getFlippedCopy(true,false); tempImage.draw(266f,399f);}//end render }//end class Slick0160a//======================================================//

.

Listing 14 . Source code for Slick0160b.java.
/*Slick0160b.java Copyright 2012, R.G.BaldwinIllustrates the drawFlash method to draw an image in silhouette format and to cause the silhouette to switchback and forth between two or more colors. Tested using JDK 1.7 under WinXP*********************************************************/ 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 Slick0160b extends BasicGame{Image spider = null;float spiderWidth; float spiderHeight;Color silohetteColor = Color.white;long timeAccumulator = 0; long flashInterval = 128;//Target frame rate int targetFPS = 60;//----------------------------------------------------// public Slick0160b(){//constructor//Set the title super("Slick0160b, baldwin");}//end constructor //----------------------------------------------------//public static void main(String[] args)throws SlickException{ AppGameContainer app = new AppGameContainer(new Slick0160b(),384,240,false); app.start();}//end main //----------------------------------------------------//@Override public void init(GameContainer gc)throws SlickException { spider = new Image("spider.png");spiderWidth = spider.getWidth(); spiderHeight = spider.getHeight();System.out.println("spiderWidth: " + spiderWidth);System.out.println("spiderHeight: " + spiderHeight); gc.setShowFPS(false) ;gc.setTargetFrameRate(targetFPS);//set frame rate }//end init//----------------------------------------------------// @Overridepublic void update(GameContainer gc, int delta) throws SlickException{timeAccumulator += delta; if(timeAccumulator>= flashInterval){ //Reset accumulator and change color of spider// silhouette timeAccumulator = 0;if(silohetteColor.equals(Color.white)){ silohetteColor = Color.blue;}else{ silohetteColor = Color.white;}//end if }//end if}//end update //----------------------------------------------------//public void render(GameContainer gc, Graphics g) throws SlickException{//set the drawing mode to honor transparent pixels g.setDrawMode(g.MODE_NORMAL);g.setBackground(Color.red);//Draw the spider. spider.draw(0f,0f);//Draw a white silhouette of the spider.spider.drawFlash(133f,0f);//Draw a blue silhouette of the spider. spider.drawFlash(266f,0f,131f,128f,Color.blue);//Cause an enlarged version of the spider to flash// between white and blue silhouette at a rate // of 1/flashInterval.spider.drawFlash(0f,0f,262f,256f,silohetteColor); }//end render}//end class Slick0160b //======================================================//

-end-

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