-
Notifications
You must be signed in to change notification settings - Fork 4
Background Image
Purpose: The getBackgroundTexture()
function is used to retrieve the appropriate background texture based on the selected game level.
Implementation:
-
Initialization: A texture variable named
background
is initialized.Texture background;
-
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);
-
Lava (Level 2):
case 2: background = ServiceLocator.getResourceService().getAsset("images/lava_bg.png", Texture.class);
-
Default (Other Levels):
default: background = ServiceLocator.getResourceService().getAsset("images/desert_bg.png", Texture.class);
-
-
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();
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.