<< Chapter < Page Chapter >> Page >

There is nothing new or unusual about the code in Listing 9 . Therefore, no explanation beyond the embedded comments should be needed. You might want to note the instance variables at the beginning ofthe class. They will be used in the code that I will describe later.

The update method

Unlike the previous program, which simply displayed what appeared to be static images, this program causes one of the images to change as the programruns. These changes are programmed into the update method, which is shown in Listing 10 .

Listing 10 . The update method.
public 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

Switch color between white and blue

Recall that (by default) the update method is executed once during each iteration of the game loop. (Other programming options are available regarding if, when, and how many times the update method is executed during one iteration of the game loop.)

The purpose of the update method is to execute the program logic.

In this case, the program logic is simple; to switch the color of the large spider in Figure 2 between white and blue on a regular schedule.

As you will see in the render method later, the color of the large spider is determined by the value of the instance variable named silohetteColor , which is initialized to white in Listing 9 . Recall that the target frame rate is set to 60 frames per second in Listing 9 . A variable named flashInterval is initialized to 128 in Listing 9 .

Also recall that the value of the incoming parameter named delta is the number of milliseconds since the last time that the update method was called. This value was used in a significant way in theearlier module titled A first look at sprite motion, collision detection, and timing control .

The program logic

Each time the update method is called, the incoming value of delta is added to the value in a variable named timeAccumulator . When the accumulated time meets or exceeds the value of flashInterval , the color is switched from white to blue, or from blue to white, depending onits current value. Also the time accumulator is set to zero and a new white/blue cycle begins.

The color switch should occur approximately every 128 milliseconds, or about eight times per second.

The render method

Recall that the execution of program logic in the update method does not cause the player's view of the game to change. It is not untilthe render method is executed that the images on the screen change.

The render method begins in Listing 11 .

Listing 11 . Beginning of 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);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);

Questions & Answers

what is biology
Hajah Reply
the study of living organisms and their interactions with one another and their environments
AI-Robot
what is biology
Victoria Reply
HOW CAN MAN ORGAN FUNCTION
Alfred Reply
the diagram of the digestive system
Assiatu Reply
allimentary cannel
Ogenrwot
How does twins formed
William Reply
They formed in two ways first when one sperm and one egg are splited by mitosis or two sperm and two eggs join together
Oluwatobi
what is genetics
Josephine Reply
Genetics is the study of heredity
Misack
how does twins formed?
Misack
What is manual
Hassan Reply
discuss biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles
Joseph Reply
what is biology
Yousuf Reply
the study of living organisms and their interactions with one another and their environment.
Wine
discuss the biological phenomenon and provide pieces of evidence to show that it was responsible for the formation of eukaryotic organelles in an essay form
Joseph Reply
what is the blood cells
Shaker Reply
list any five characteristics of the blood cells
Shaker
lack electricity and its more savely than electronic microscope because its naturally by using of light
Abdullahi Reply
advantage of electronic microscope is easily and clearly while disadvantage is dangerous because its electronic. advantage of light microscope is savely and naturally by sun while disadvantage is not easily,means its not sharp and not clear
Abdullahi
cell theory state that every organisms composed of one or more cell,cell is the basic unit of life
Abdullahi
is like gone fail us
DENG
cells is the basic structure and functions of all living things
Ramadan
What is classification
ISCONT Reply
is organisms that are similar into groups called tara
Yamosa
in what situation (s) would be the use of a scanning electron microscope be ideal and why?
Kenna Reply
A scanning electron microscope (SEM) is ideal for situations requiring high-resolution imaging of surfaces. It is commonly used in materials science, biology, and geology to examine the topography and composition of samples at a nanoscale level. SEM is particularly useful for studying fine details,
Hilary
cell is the building block of life.
Condoleezza Reply
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

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