<< Chapter < Page Chapter >> Page >

That concludes the discussion of the update method for this program.

The render method

Listing 8 shows the render method for this program.

Listing 8 . The render method.
public void render(GameContainer gc, Graphics g) throws SlickException{//set the drawing mode to honor transparent pixels g.setDrawMode(g.MODE_NORMAL);//Draw the background to erase the previous picture.background.draw(0,0); //Draw every sprite in the array.for(int cnt = 0;cnt<sprites.length;cnt++){ //Ask the sprite to draw itself.sprites[cnt].draw();}//end for loop }//end render}//end class Slick0200

The draw method of the Sprite01 class

The thing that is new about the code in Listing 8 is the call to the draw method of the Sprite01 class. That draw method is shown in Listing 9 .

Listing 9 . The draw method of the Sprite01 class.
//This method causes the sprite to be drawn each time // it is called.public void draw(){ image.draw(X,Y,scale,colorFilter);}//end draw

Little reduction in complexity

In this case, moving the call to the draw method of the Image class from the render method to the Sprite01 class didn't do much to reduce the complexity of the program. However, that is because Ikept the draw method in the Sprite01 class very simple.

I could have made it much more capable and more complex by including additional functionality. For example, I could have caused the draw method to call the drawFlash method (see Slick0160: Using the draw and drawFlash methods ) of the Image class when the life property value goes to zero. In that case, only a silhouette of the dead sprite would be drawn in place of the actual image of the sprite.

That concludes the discussion of the render method.

Run the program

I encourage you to copy the code from Listing 10 and Listing 11 . 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 develop a sprite class from which you can instantiate and animate swarms of sprite objects.

What's next?

In the next module, you will learn how to use the Sprite01 class from this module to write a predator/prey simulation program involving thousandsof sprites, collision detection, and sound effects.

Miscellaneous

This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Slick0200: Developing a sprite class
  • File: Slick0200.htm
  • Published: 02/06/13
  • Revised: 10/03/15
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.

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