<< Chapter < Page Chapter >> Page >

Run the program

I encourage you to copy the code from Listing 8 . Compile the code and execute it,making changes, and observing the results of your changes. Make certain that you can explain why your changes behave as they do.

Summary

In this module, you learned how to use objects of the SpriteSheet class and the Animation class to perform simple sprite sheet animation.

What's next?

In the next module, you will learn how to use objects of the SpriteSheet class and the Animation class to perform more complex sprite sheet animations than was the case in this module.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Slick0180: Sprite sheet animation, part 1
  • File: Slick0180.htm
  • Published: 02/05/13
  • Revised: 06/08/15 for 64-bit
Disclaimers:

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.

Complete program listing

A complete listing of the program discussed in this module is provided in Listing 8 .

Listing 8 . Source code for Slick0180 .
/*Slick0180.java Copyright 2013, R.G.BaldwinUses one row of sprites from a sprite sheet along with an Animation object to draw an animation of a dog playing.By default, the program displays one cycle of five sprites per second. Clock time is displayed below theanimation. The time that each image of the dog is displayed isindependent of the frame rate. Demonstrate this by changing the value of targetDelta and observing therelationship between the animation times and the clock. For best results, keep the targetDelta less than thedisplay time for each sprite (duration). 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.SpriteSheet;import org.newdawn.slick.Animation; import org.newdawn.slick.Color;public class Slick0180 extends BasicGame{ Image spriteSheetImage = null;float spriteSheetWidth; float spriteSheetHeight;int spritesPerRow = 5; int spritesPerColumn = 2;int targetDelta = 16;//msec int duration = 200;//time to display each spritelong totalTime = 0;//accumulate total time for display SpriteSheet spriteSheet;Animation animation; int spriteWidth;int spriteHeight; float spriteX = 0;//sprite drawing locationfloat spriteY = 0; //----------------------------------------------------//public Slick0180(){ //Call to superclass constructor is required.super("Slick0180, Baldwin."); }//end constructor//----------------------------------------------------// public static void main(String[]args) throws SlickException{AppGameContainer app = new AppGameContainer( new Slick0180(),450,120,false);app.start();//this statement is required }//end main//----------------------------------------------------// @Overridepublic void init(GameContainer gc) throwsSlickException { spriteSheetImage = new Image("Slick0180a1.png");//Enlarge the sprite sheet. Image temp = spriteSheetImage.getScaledCopy(580,224);spriteSheetImage = temp; spriteSheetWidth = spriteSheetImage.getWidth();spriteSheetHeight = spriteSheetImage.getHeight(); System.out.println("spriteSheetWidth: " + spriteSheetWidth); System.out.println("spriteSheetHeight: " + spriteSheetHeight); spriteWidth = (int)(spriteSheetWidth/spritesPerRow);spriteHeight = (int)(spriteSheetHeight/spritesPerColumn);//Instantiate a new spriteSheet object based on the // width and height of the tiles.spriteSheet = new SpriteSheet(spriteSheetImage, spriteWidth,spriteHeight); //Create a new animation based on a selection of// sprites from the sprite sheet. animation = new Animation(spriteSheet,0,//first column 0,//first row4,//last column 0,//last rowtrue,//horizontal duration,//display timetrue//autoupdate );gc.setShowFPS(true);//show FPS ////set frame rategc.setTargetFrameRate((int)(1000/targetDelta)); //set drawing locationspriteX = spriteWidth; spriteY = 0;}//end init //----------------------------------------------------//@Override public void update(GameContainer gc, int delta)throws SlickException{ //Note that the following is for clock time display// only. It does not effect the animation. totalTime += delta;//update total time accumulator}//end update //----------------------------------------------------//public void render(GameContainer gc, Graphics g) throws SlickException{g.setDrawMode(g.MODE_NORMAL); g.setBackground(Color.gray);//Draw the currently selected animation image at the // specified locationanimation.draw(spriteX,spriteY); g.drawString("totalTime = "+totalTime/1000,10f,100f);}//end render }//end class Slick0180//======================================================//

-end-

Questions & Answers

How we are making nano material?
LITNING Reply
what is a peer
LITNING Reply
What is meant by 'nano scale'?
LITNING Reply
What is STMs full form?
LITNING
scanning tunneling microscope
Sahil
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
Bob
The nanotechnology is as new science, to scale nanometric
brayan
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Damian
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
Renato
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
?
Kyle
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
Adin
why?
Adin
what school?
Kyle
biomolecules are e building blocks of every organics and inorganic materials.
Joe
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
research.net
kanaga
sciencedirect big data base
Ernesto
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
Bharti
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
Daniel
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
Maciej
characteristics of micro business
Abigail
for teaching engĺish at school how nano technology help us
Anassong
How can I make nanorobot?
Lily
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
NANO
how can I make nanorobot?
Lily
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
s.
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
Tarell
what is the actual application of fullerenes nowadays?
Damian
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
Tarell
what is the Synthesis, properties,and applications of carbon nano chemistry
Abhijith Reply
Mostly, they use nano carbon for electronics and for materials to be strengthened.
Virgil
is Bucky paper clear?
CYNTHIA
carbon nanotubes has various application in fuel cells membrane, current research on cancer drug,and in electronics MEMS and NEMS etc
NANO
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry course in your pocket!





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