<< Chapter < Page Chapter >> Page >

Create the images

Listing 2 creates the ladybug Image object and the background Image object using code that you have seen before, and stores those object's references in the instance variablesnamed bug and background .

Get, save, and display the widths and heights of the images

Then Listing 2 calls accessor methods to get, save, and display the widths and the heights of the bug and background objects.

Set the target frame rate

Finally, Listing 2 calls the setTargetFrameRate method to set the target frame rate to 60 frames per second.

The update method

The overridden update method begins in Listing 3 .

The code in Listing 3 uses a very simple approach to cause the sprite to exhibit motion.

Listing 3 . Beginning of the update method for Slick0150a.
public void update(GameContainer gc, int delta) throws SlickException{//Compute new location for the sprite. bugX += bugXDirection*xStep;bugY += bugYDirection*yStep;

Compute new sprite locations

Each time the update method is called, Listing 3 computes new location coordinate values for the sprite, which are eitherincreased or decreased by the values stored in xStep and yStep .

Repetitive displays of the sprite in the new locations by the render method produces the impression that the sprite is moving.

Step values are independent of the frame rate

The step values are multiplied by the contents of direction variables in Listing 3 , each of which contains either +1 or -1, and the products are added to thecurrent location coordinates.

As you will see shortly, the algebraic signs of the direction variables are changed each time the sprite collides with an edge of the game window.

This code assumes a constant frame rate and does not correct for variations in the frame rate.In other words, the size of the step taken during each frame is the same regardless of how long it takes to complete a frame. If the computer is runningbelow the target frame rate, the sprite will appear to move more slowly than would be the case if the computer is running at the target frame rate.

Collision detection

The code in Listing 4 begins by detecting a collision of the right edge of the sprite with the right edge of the game window andreverses the sprite's direction of motion when a collision occurs.

Note that if the rightmost portion of the sprite actually tries to move beyond the right edge of the game window, it is stopped at the edge of the gamewindow.

Listing 4 . Detection of collision with right edge.
if(bugX+bugWidth>= backgroundWidth){ //A collision has occurred.bugXDirection = -1.0f;//reverse direction //Set the position to the right edge less the width// of the sprite. bugX = backgroundWidth - bugWidth;//Compute traversal time for the bug to make one// round trip across the game window and back. long currentTime = gc.getTime();traversalTime = currentTime - oldTime; oldTime = currentTime;}//end if

Compute and save the traversal time

Then the code in Listing 4 computes the elapsed time since the previous collision of the sprite with the right edge of the gamewindow and saves that time interval in the variable named traversalTime . That traversalTime value will be displayed when the render method is called producing the output shown in Figure 3 .

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