Class Snake

java.lang.Object
com.csse3200.game.minigames.snake.Snake

public class Snake extends Object
Represents the snake in the Snake mini-game, managing its position, movement, and growth.
  • 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

      public void setDirection(Direction direction)
      Sets the direction the snake is moving
      Parameters:
      direction - the direction to set to
    • getDirection

      public Direction getDirection()
      Returns the current direction of the snake's head.
      Returns:
      The direction of the snake's head.
    • updateDirectionOnInput

      public void updateDirectionOnInput(Direction direction)
      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

      public void move(Direction direction)
      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

      public List<Snake.Segment> getBodySegments()
      Returns:
      a list of the snake's body segments
    • getLastSegment

      public Snake.Segment getLastSegment()
      Returns:
      the last snake body segment or null if none exist
    • getMovePeriod

      public float getMovePeriod()
    • updateMovePeriod

      public void updateMovePeriod(float period)