Class Apple

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

public class Apple extends Object
Represents an apple in the Snake game. The apple is placed on a grid and can be randomly spawned in an unoccupied cell.
  • Constructor Details

    • Apple

      public Apple(Grid grid)
      Creates an apple associated with a specific grid. The apple is initially spawned at a random unoccupied position on the grid.
      Parameters:
      grid - The grid on which the apple is placed.
  • Method Details

    • spawn

      public void spawn()
      Randomly spawns the apple at an unoccupied position on the grid. The apple will not be placed in a cell that is already occupied.
    • isTouchingSnakeHead

      public boolean isTouchingSnakeHead(Snake snake)
      Checks to see if the apple is touching the snakes head
      Parameters:
      snake - : the game snake
      Returns:
      True if head is touching else false
    • getX

      public int getX()
      Get the apples x position
      Returns:
      x position
    • getY

      public int getY()
      Get the apples y position
      Returns:
      y position
    • setAppleLocation

      public void setAppleLocation(int newX, int newY)
      Set apple location. Used for testing and debugging only.