<< Chapter < Page Chapter >> Page >

In this module, you will learn how to make sprites move at a constant speed in front of an image in the face of widely varying frame rates. You will also learn about a rudimentary form of collision detection.

General background information

The update and render methods

Following initialization, the Slick2D game engine switches back and forth between an update method and a render method.

We write code to control the state of the game in the update method. We write code to display the state of the game in the render method.

A sprite

According to one definition, a sprite is a computer graphic that may be moved on-screen and otherwise manipulated as a single entity.

We will use the image of the ladybug shown in Figure 1 as a sprite in two different programs that I will explain in this module.

Figure 1 . Image of a ladybug.
Missing image.

We will cause that sprite to move in front of the background image shown in Figure 2 .

Figure 2 . Background image.
Missing image.

A bouncing sprite

In particular, we will cause the sprite to bounce around inside the game window like a pool ball on a pool table. Whenever it strikes an edge of the gamewindow, it will bounce off in the opposite direction. This process will continue until the program is terminated.

The target frame rate

As you learned in an earlier module, a GameContainer method named setTargetFrameRate can be called in an attempt to cause the program to run at a constant frame rate.

This method can slow the frame rate down to the target value on fast computers. However, it cannot speed the frame rate up to the target value onslower computers.

Therefore, a call to the setTargetFrameRate method should actually be viewed as setting the maximum frame rate that the program will run.

An appearance of achieving the target frame rate

Sometimes it is important to cause the program to give the appearance of running at the target frame rate even if it is actually running slower.

One example is when a sprite is moving across the game window. It is often desirable to cause the sprite to move at the same overall speed regardless ofthe speed of the computer. For example, you probably wouldn't want a missile to take a long time to reach its target on a slow computer and a short time toreach its target on a fast computer.

Accuracy versus graphic quality

I will explain a program in this module that is designed to achieve such a result. The upside is that you can often achieve the appearance of the targetframe rate in terms of the overall speed of the sprite. The downside is that the motion of the sprite may be less smooth than would be the case if the computerwere actually running at the target frame rate.

The parameter named delta

Each time the update method is called, it receives an incoming parameter named delta whose value is the number of milliseconds thathave elapsed since the most recent call to the update method. In the case of a highly varying frame rate, such as may occur when the render method is required to draw a complex and constantly changing scene, the value of delta may vary significantly from one call to the next ofthe update method.

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