Skip to content

Background Image

Mohamad Dabboussi edited this page Oct 2, 2023 · 3 revisions

Function: getBackgroundTexture() in MainGameScreen

Purpose: The getBackgroundTexture() function is used to retrieve the appropriate background texture based on the selected game level.

Implementation:

  1. Initialization: A texture variable named background is initialized.

    Texture background;
  2. Determine Texture Based on Selected Level: The function uses a switch statement to determine which texture to load based on the selectedLevel:

    • Ice (Level 1):

      case 1:
          background = ServiceLocator.getResourceService().getAsset("images/ice_bg.png", Texture.class);

      image

    • Lava (Level 2):

      case 2:
          background = ServiceLocator.getResourceService().getAsset("images/lava_bg.png", Texture.class);

      image

    • Default (Other Levels):

      default:
          background = ServiceLocator.getResourceService().getAsset("images/desert_bg.png", Texture.class);

      image

  3. Return Background Texture:

    return background;

The background texture is determined by the selected game level, which is fetched using:

int selectedLevel = GameLevelData.getSelectedLevel();

Read Also Game Level Data

This texture is then loaded during the loadAsset() function and used for rendering as:

backgroundTexture = getBackgroundTexture();

Rendering Background Texture

Within the render(float delta) method, the background texture is drawn on the screen:

@Override
public void render(float delta) {
    ...
    // Begin the batch
    batch.begin();

    // Draw the background texture.
    batch.draw(backgroundTexture, 0, 0, viewportWidth, viewportHeight);

    // End the batch
    batch.end();
    ...
}

This section of the render() method first sets up the rendering batch, then draws the backgroundTexture covering the entire viewport, and finally concludes the batch.

Clone this wiki locally