Skip to content

Chapter 7

Sandra Moen edited this page Jul 17, 2019 · 1 revision

Side-Scrolling Games

This chapter teaches about infinite scrolling backgrounds, parallax and difficulty ramps.

As suggested by the book the following features have been added:

  • Give the player hit points
  • Moving the player continuously rather than discretely
  • Engine sounds for enemies
  • Visual effect when the player moves upwards
  • Enemies move in the y-direction
  • Store highscore in a text file
  • A menu, which displays all-time highscore
  • A restart is displayed on game over

Bonus feature:

  • More parallax

menuscreen

Infinite Scrolling

What the game means by a side-scrolling game is simply that the game appears to be moving constantly in a direction (from right to left). This is done by having a copy of each background assets used and moving them side-by-side across the screen.

sideScrolling

As shown in the figure above when the blue background moves completely clear of the green game screen, it is repositioned behind the red, and vice versa. It is also important that the beginning and the end of the background are seamless, which means the background can be placed side by side itself without creating a noticeable boundary.

The algorithm in a background's act method:

// if moved completely past left edge of screen: shift right, past other instance
if ( getX() + getWidth() < 0) {
    moveBy(2 * getWidth(), 0);
}

To save memory it is important that the texture asset is not loaded twice, but rather copied.

Parallax

"Parallax scrolling is a technique in computer graphics where background images move past the camera more slowly than foreground images, creating an illusion of depth in a 2D scene and adding to the sense of immersion in the virtual experience." -- wikipedia.org

The Whispered World

"A side view of the layers used for parallax scrolling in the Whispered World" >-- wikipedia.org

As a bonus feature, a Parallax class was created and implemented, the game now has 4 layers of background, which quickly adds depth to the world. Some consideration should be used for the color schemes such that the background will remain a background and not become too noisy

Difficulty Ramps

Ramping the difficulty simply means changing the difficulty over time or by other events. Plane Dodger becomes increasingly difficult over time for every time a new enemy is spawned up to a cap.

The algorithm in LevelScreen, which is run each time an enemy spawns:

enemySpawnInterval -= 0.10f;
enemySpeed += 10;

if (enemySpawnInterval < .50f)
    enemySpawnInterval = .50f;

if (enemySpeed > 400)
    enemySpeed = 400;

Bugs

Unfortunately, the book does not explain how to better manage assets. This becomes a problem with the sparkle animation which is triggered when the player collects a star. What happens is that game 'lags' for a second or so upon pickup. This occurs because the game has to read the texture from the file and run the setup and animation algorithm for each animation. To ameliorate this a TexturePacker and AssetManager may be implemented (not shown in any of the chapters of the book).

New Imports

No new import

Previous | Next

Clone this wiki locally