-
Notifications
You must be signed in to change notification settings - Fork 1
Snake Game
rp3002 edited this page Aug 27, 2024
·
2 revisions
The overall game logic for the Snake mini-game is managed by the SnakeGame
class. It handles the game's core mechanics including the
- Snake Character management
- Detecting the collisions
- tracking the player's score
- when the game actually terminates
This
SnakeGame
class also integrates various components of the game which includes: Snake
Apple
-
SnakeGrid
ThisSnakeGame
class overall ensures smooth gameplay and an amazing player experience.
package com.csse3200.game.components.minigame.snake;
import java.util.List;
The SnakeGame
class is responsible for:
- Snake's movement
- Spawning of the apples
- Score calculation
- Game state (whether the game is over)
-
Parameters:
-
Snake snake
: The snake character -
Apple apple
: The apple that the snake consumes. -
SnakeGrid snakeGrid
: The grid on which the game is played.
-
-
Description:
- Initialises a new instance of the
SnakeGame
, setting up the snake, apple, and grid. It also initialises the score to 0.
- Initialises a new instance of the
-
Returns:
-
boolean
:true
if the game is over,false
otherwise.
-
-
Description:
- Getter for the isGameOver variable
-
Parameters:
-
boolean state
: The new state of the game (true
for game over,false
for ongoing).
-
-
Description:
- Set the isGameOver function. Is true if over, else false
-
Returns:
-
int
: The current score of the game.
-
-
Description:
- Returns the current score of the game, which increases as the snake consumes apples.
-
Description:
- Attempts to eat the apple in the snake game if the snake has reached it. If the snake can eat the apple then a new apple will spawn, the snake will grow and the score will increase.
-
Returns:
-
int
: The value to add to the score when the snake eats an apple.
-
-
Description:
- A private method that defines the score increment when the snake consumes an apple. The current implementation adds 1 point per apple (Adds constant to the score)
-
Parameters:
-
int value
: The new score value.
-
-
Description:
- Sets the score (used for testing only)
-
Returns:
-
boolean
:true
if the snake has hit the boundary of the grid,false
otherwise.
-
-
Description:
- Detects if the snake it at the boundary of the grid, indicating a collision with the wall and triggering game-over conditions.
-
Returns:
-
boolean
:true
if the snake's head has collided with its own body,false
otherwise.
-
-
Description:
- Determines if the snake has run into itself. A collision results in the game ending.