Skip to content

MiniGameMenuScreen

rp3002 edited this page Aug 28, 2024 · 2 revisions

Overview

The MiniGameMenuScreen class presents the user with a menu of mini-games. It allows the user to select among various mini-games—Snake, Flappy Bird, and Underwater Maze—and navigate back to the main menu. This class utilises LibGDX's Scene2D module to manage UI elements and handle user input.

Class Definition

public class MiniGameMenuScreen implements Screen {
    private Stage stage;
    private BitmapFont font;
    private final GdxGame game;
    private Skin skin;
    private SpriteBatch batch;
    private Texture snakeTexture;
    private Texture skyTexture;
    private Texture waterTexture;
    private Texture backgroundTexture;
}

Imports

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.csse3200.game.GdxGame;

Methods

show()

  • Called when the screen is shown for the first time

  • Setup:

    • Initialises Stage, SpriteBatch, BitmapFont, and Skin.
    • Loads textures for the background and mini-game icons.
  • UI Elements:

    • Creates TextButton for exiting the menu and selecting mini-games.
    • Creates Image for mini-game icons (Snake, Flappy Bird, Underwater Maze).
  • Layout:

    • Arranges UI elements using Table.
    • Adds images and buttons to the table and positions it on the screen.
  • Listeners:

    • Adds click listeners to buttons and images to handle user input, such as changing the screen or updating UI colors.
  • Background Rendering:

    • Clears the screen with a specified colour.
    • Draws the background texture using SpriteBatch.
  • UI Updates:

    • Updates and renders the stage to reflect any changes.
  • Input Handling:

    • Checks for the Escape key to return to the main menu.

resize(int width, int height)

  • parameters: width and height of the new screen
  • Called when the screen is resized

pause()

  • Called when the game is paused

resume()

Called when the game is resumed after a pause

hide()

  • Called when the screen is hidden

dispose()

  • Called to dispose of resources to prevent memory leaks
Clone this wiki locally