<< Chapter < Page Chapter >> Page >

Sick but not yet dead

The only new code in Listing 2 is the call to the setLife method at the end. In the earlier module titled Slick0210: Collision detection and sound , the life property of a sprite was always either 0 or 1. A sprite with a life property value of 0 was dead. A sprite with a life property value of 1 was alive.

This program is more nuanced and uses values other than 0 and 1 for the infected red sprites. A value of 0 still means that a sprite is dead. Any otherpositive value means that the sprite is sick and dying but not yet dead.

The value assigned to the life property for this sprite is a random value between 0 and infectedSpriteLife . This is one of the property values that has an impact on the extent to which the diseasespreads through the population. The longer an infected sprite lives after becominginfected, the more healthy sprites it will infect and the more aggressive will be the disease.

You can modify this value (see Listing 1 ) and recompile the program to experiment with different values.

Remainder of the init method

The remainder of the init method is shown in Listing 3 .

Listing 3 . Remainder of the init method.
//Populate the ArrayList object with green sprites. // Make the initial position random. Make the initial// direction of motion random. Make the speed // (step size) random. Make the size (scale) random.// Make the color filter white (do nothing). for(int cnt = 0;cnt<numberSprites;cnt++){ sprites.add(new Sprite01(greenBallImage,//image backgroundWidth*random.nextFloat(),//positionbackgroundHeight*random.nextFloat(),//position (random.nextFloat()>0.5) ? 1f : -1f,//direction (random.nextFloat()>0.5) ? 1f : -1f,//direction random.nextFloat()*maxStepSize,//step sizerandom.nextFloat()*maxStepSize,//step size 1.0f,//scalenew Color(1.0f,1.0f,1.0f)));//color filter }//end for loopgc.setTargetFrameRate(targetFPS);//set frame rate }//end init

A population of healthy sprites

Listing 3 uses a for loop to add numberSprites (see Listing 1 ) healthy sprites to the population. This is another property that has an impacton the spread of the disease. Everything else being equal, the more sparse the population, the more difficult it is for the disease to get a foothold in thefirst place and the more difficult it is for the disease to spread if it does get a foothold.

The frame rate

Listing 3 also sets the frame rate to the value of targetFPS (see Listing 1 ) . Note that I slowed this program down to the standard movie frame rate of 24 fps (as opposed to the typical 60 fps) mainly because I wanted to run the simulation more slowly. In other words, I wanted it to be possible tosee the disease spread through the population. Also, it is a fairly demanding program so it may not run at 60 fps on some machines.

End of the init method

Listing 3 also signals the end of the init method.

The update method

The update method begins in Listing 4 . This is the method where most of the added complexity in this program resides.

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