Skip to content

Snake Game

rp3002 edited this page Aug 27, 2024 · 2 revisions

Overview

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 This SnakeGame class overall ensures smooth gameplay and an amazing player experience.

Class: SnakeGame

Package

package com.csse3200.game.components.minigame.snake;

Imports

import java.util.List;

Description

The SnakeGame class is responsible for:

  • Snake's movement
  • Spawning of the apples
  • Score calculation
  • Game state (whether the game is over)

Constructor

SnakeGame(Snake snake, Apple apple, SnakeGrid snakeGrid)

  • 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.

Methods

boolean getIsGameOver()

  • Returns:
    • boolean: true if the game is over, false otherwise.
  • Description:
    • Getter for the isGameOver variable

void setIsGameOver(boolean state)

  • 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

int getScore()

  • Returns:
    • int: The current score of the game.
  • Description:
    • Returns the current score of the game, which increases as the snake consumes apples.

void attemptEatFruit()

  • 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.

int calculateScore()

  • 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)

void set_score(int value)

  • Parameters:
    • int value: The new score value.
  • Description:
    • Sets the score (used for testing only)

boolean boundaryDetection()

  • 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.

boolean snakeCollisionDetection()

  • 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.

Sequence Diagram

Screenshot 2024-08-27 at 12 02 11 PM
Clone this wiki locally