Class Snake
java.lang.Object
com.csse3200.game.minigames.snake.Snake
Represents the snake in the Snake mini-game, managing its position, movement, and growth.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final record
Record that represents a segment of the snake's body -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionReturns the current direction of the snake's head.int
Returns current snake length.float
int
getX()
int
getY()
void
Moves the snake in a directionvoid
setDirection
(Direction direction) Sets the direction the snake is movingvoid
update
(float dt) Update the position of the snake after a given amount of time.void
updateDirectionOnInput
(Direction direction) Update snake direction based on an input direction.void
updateMovePeriod
(float period)
-
Constructor Details
-
Snake
public Snake(Grid grid, int startX, int startY, Direction startDirection, int startLength, float movePeriod) Initialises a new Snake with specified parameters.- Parameters:
grid
- The grid on which the snake moves.startX
- The starting x-coordinate of the snake's head.startY
- The starting y-coordinate of the snake's head.startDirection
- The initial direction the snake is facing.startLength
- The initial length of the snake.movePeriod
- The time interval between moves.
-
-
Method Details
-
setDirection
Sets the direction the snake is moving- Parameters:
direction
- the direction to set to
-
getDirection
Returns the current direction of the snake's head.- Returns:
- The direction of the snake's head.
-
updateDirectionOnInput
Update snake direction based on an input direction. If the input direction is reversed 180 degrees from the current direction of the snake, it will not change.- Parameters:
direction
- the direction to update to
-
move
Moves the snake in a direction- Parameters:
direction
- the direction to move in
-
getLength
public int getLength()Returns current snake length. Used only for testing. -
update
public void update(float dt) Update the position of the snake after a given amount of time.- Parameters:
dt
- time since the last update
-
getX
public int getX()- Returns:
- the x co-ordinate of the head
-
getY
public int getY()- Returns:
- the y coordinate of the head
-
getBodySegments
- Returns:
- a list of the snake's body segments
-
getLastSegment
- Returns:
- the last snake body segment or null if none exist
-
getMovePeriod
public float getMovePeriod() -
updateMovePeriod
public void updateMovePeriod(float period)
-