<< Chapter < Page Chapter >> Page >
Listing 4 . Beginning of the update method.
public void update(GameContainer gc, int delta) throws SlickException{//Move all the sprites to their new positions. for(int cnt = 0;cnt<sprites.size();cnt++){ //Get a reference to the current Sprite01 object.Sprite01 thisSprite = sprites.get(cnt); //Ask the sprite to move according to its// properties thisSprite.move();//Ask the sprite to bounce off the edge if necessary thisSprite.edgeBounce(backgroundWidth,backgroundHeight); }//end for loop

Nothing new in this code fragment

The is nothing new in the code fragment shown in Listing 4 . The new code begins in Listing 5 .

An overview of the code

Before getting down into the details of the code, I will give you a descriptive overview.

In the outer-most layer, the program uses a for loop to examine every sprite in the population looking for red or infected sprites.

When it finds an infected sprite, it decreases the value of its life expectancy. Then it uses an inner for loop to test that sprite against every sprite in the population looking for collisions.

Ignore collision with an infected red sprite

If the infected sprite collides with another infected sprite, it ignores the collision and keeps searching the population, looking for collisions withhealthy sprites.

Collision with a healthy green sprite

If the infected sprite collides with a healthy (green) sprite, it causes that sprite to become exposed to the disease and plays a sound effect. (As you will see later, sprites that are exposed to the disease don't always contract the disease.)

The state of the population

When the infected sprite has been tested for a collision with every healthy sprite, four kinds of sprites exist in the population:

  1. Healthy sprites that have not been exposed to the disease.
  2. Healthy sprites that have been exposed to the disease.
  3. Infected sprites that still have some remaining life.
  4. Infected sprites whose life property is less than or equal to zero, meaning that they are dead.

A cleanup pass

An Iterator is used to make a cleanup pass through the population.

Exposed sprites are either converted to infected sprites or cleared of theexposure on the basis of a random value that has a maximum value of probabilityOfInfection (see Listing 1 ) .

Dead sprites are removed from the population.

The code to accomplish all of this begins with the for loop in Listing 5 .

Listing 5 . Process collisions.
//Search for and process collisions between // infected (red) sprites and healthy (green)// sprites. Declare the green sprite to be exposed to // the disease when a collision occurs.for(int ctr = 0;ctr<sprites.size();ctr++){ //Get a reference to the Sprite01 object.Sprite01 testSprite = sprites.get(ctr); if(testSprite.getImage().equals(redBallImage)){//This is an infected sprite. Reduce its life. testSprite.setLife(testSprite.getLife() - 1);// Do the following for every sprite in the// ArrayList object. for(int cnt = 0;cnt<sprites.size();cnt++){ //Get a reference to the Sprite01 object.Sprite01 thisSprite = sprites.get(cnt);//Test for a collision between this sprite and // the infected test sprite.boolean collision = thisSprite.isCollision(testSprite);//Process a collision if it has occurred.// Exclude collisions between the testSprite // and itself and with other infected sprites.if((collision == true)&&(!thisSprite.getImage(). equals(redBallImage))){//A collision has occurred, set exposed to true thisSprite.setExposed(true);//Play a sound to indicate that a collision // has occurred.blaster.play(); }//end if}//end for loop}//end if on redBallImage

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