Snake Component - UQcsse3200/2024-studio-2 GitHub Wiki
The Snake
class in the snake mini-game is the central component which defines the structure and the behaviour of the snake.
This Snake
class handles the movement, growth, and directional control of the snake on the grid while interacting with the other game components, such as the apple.
package com.csse3200.game.components.minigame.snake;
import com.csse3200.game.components.minigame.Direction;
import com.csse3200.game.components.minigame.Grid;
import com.csse3200.game.components.minigame.GridCell;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.List;
import java.util.ArrayList;
The Snake
class handles the snake's position, movement, growth, and directional control within the game grid. The Snake
class also includes methods for handling user input, updating the snake's direction, and ensuring the snake grows it's length when it consumes an apple.
Snake(Grid grid, int startX, int startY, Direction startDirection, int startLength, float movePeriod)
-
Parameters:
-
Grid grid
: The snake moves on this grid -
int startX
: initial x-coordinate of the snake's head. -
int startY
: initial y-coordinate of the snake's head. -
Direction startDirection
: initial direction in which the snake is moving. -
int startLength
: initial length of the snake. -
float movePeriod
: time period between each movement of the snake.
-
-
Description:
- Initialises a new snake on the grid, starting at a given position and moving in a particular direction, with an initial length and movement period.
-
Parameters:
-
Direction direction
: The new direction for the snake to move.
-
-
Description:
- Sets the direction the snake is moving
-
Returns:
-
Direction
: The current direction in which the snake is moving.
-
-
Description:
- Get the current direction of the snake.
-
Parameters:
-
Direction direction
: The direction input by the player.
-
-
Description:
- Update snake direction with checks, ensuring the snake cannot reverse direction (e.g., from UP to DOWN).
-
Parameters:
-
Direction direction
: The direction in which the snake should move.
-
-
Description:
- moves the snake in a direction, adding a new segment to the snake's body and updating the grid to reflect the snake's new position. If the snake has reached its maximum length, the last segment is removed.
-
Description:
- grows the snake (increase it's length). This typically occurs when the snake consumes an apple.
-
Parameters:
-
float dt
: The time elapsed since the last update.
-
-
Description:
- Updates the snake's position based on the elapsed time and its movement period.
-
Returns:
-
int
: The x-coordinate/ y-coordinate of the snake's head.
-
-
Description:
- Retrieves the x-coordinate/ y-coordinate of the snake's head.
-
Returns:
-
List<Segment>
: A list of all the segments that make up the snake's body.
-
-
Description:
- Returns the full snake in segments
-
Parameters:
-
int x
: x-coordinate of the segment. -
int y
: y-coordinate of the segment.
-
-
Description:
- Represents a single segment of the snake's body, storing its position on the grid.
-
Returns:
-
int
: x-coordinate/ y-coordinate of the segment.
-
-
Description:
- Retrieves x-coordinate/ y-coordinate of the segment.