-
Notifications
You must be signed in to change notification settings - Fork 1
MiniGameMenuScreen
rp3002 edited this page Aug 28, 2024
·
2 revisions
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.
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;
}
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;
-
Called when the screen is shown for the first time
-
Setup:
- Initialises
Stage
,SpriteBatch
,BitmapFont
, andSkin
. - Loads textures for the background and mini-game icons.
- Initialises
-
UI Elements:
- Creates
TextButton
for exiting the menu and selecting mini-games. - Creates
Image
for mini-game icons (Snake, Flappy Bird, Underwater Maze).
- Creates
-
Layout:
- Arranges UI elements using
Table
. - Adds images and buttons to the table and positions it on the screen.
- Arranges UI elements using
-
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.
- parameters: width and height of the new screen
- Called when the screen is resized
- Called when the game is paused
Called when the game is resumed after a pause
- Called when the screen is hidden
- Called to dispose of resources to prevent memory leaks