Class Apple
java.lang.Object
com.csse3200.game.minigames.snake.Apple
Represents an apple in the Snake game.
The apple is placed on a grid and can be randomly spawned in an unoccupied cell.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
getX()
Get the apples x positionint
getY()
Get the apples y positionboolean
isTouchingSnakeHead
(Snake snake) Checks to see if the apple is touching the snakes headvoid
setAppleLocation
(int newX, int newY) Set apple location.void
spawn()
Randomly spawns the apple at an unoccupied position on the grid.
-
Constructor Details
-
Apple
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
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.
-