Skip to content

Snake Component

rp3002 edited this page Aug 26, 2024 · 1 revision

Overview

The Snake class in the snake mini-game is the central component which defines the structure and the behaviour of the snake. This Snake class handles the movement, growth, and directional control of the snake on the grid while interacting with the other game components, such as the apple.

Class: Snake

Package

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

Imports

import com.csse3200.game.components.minigame.Direction;
import com.csse3200.game.components.minigame.Grid;
import com.csse3200.game.components.minigame.GridCell;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.List;
import java.util.ArrayList;

Description

The Snake class handles the snake's position, movement, growth, and directional control within the game grid. The Snake class also includes methods for handling user input, updating the snake's direction, and ensuring the snake grows it's length when it consumes an apple.

Constructor

Snake(Grid grid, int startX, int startY, Direction startDirection, int startLength, float movePeriod)

  • Parameters:
    • Grid grid: The snake moves on this grid
    • int startX: initial x-coordinate of the snake's head.
    • int startY: initial y-coordinate of the snake's head.
    • Direction startDirection: initial direction in which the snake is moving.
    • int startLength: initial length of the snake.
    • float movePeriod: time period between each movement of the snake.
  • Description:
    • Initialises a new snake on the grid, starting at a given position and moving in a particular direction, with an initial length and movement period.

Methods

void setDirection(Direction direction)

  • Parameters:
    • Direction direction: The new direction for the snake to move.
  • Description:
    • Sets the direction the snake is moving

Direction getDirection()

  • Returns:
    • Direction: The current direction in which the snake is moving.
  • Description:
    • Get the current direction of the snake.

void updateDirectionOnInput(Direction direction)

  • Parameters:
    • Direction direction: The direction input by the player.
  • Description:
    • Update snake direction with checks, ensuring the snake cannot reverse direction (e.g., from UP to DOWN).

void move(Direction direction)

  • Parameters:
    • Direction direction: The direction in which the snake should move.
  • Description:
    • moves the snake in a direction, adding a new segment to the snake's body and updating the grid to reflect the snake's new position. If the snake has reached its maximum length, the last segment is removed.

void grow()

  • Description:
    • grows the snake (increase it's length). This typically occurs when the snake consumes an apple.

void update(float dt)

  • Parameters:
    • float dt: The time elapsed since the last update.
  • Description:
    • Updates the snake's position based on the elapsed time and its movement period.

int getX() and int getY()

  • Returns:
    • int: The x-coordinate/ y-coordinate of the snake's head.
  • Description:
    • Retrieves the x-coordinate/ y-coordinate of the snake's head.

List<Segment> getBodySegments()

  • Returns:
    • List<Segment>: A list of all the segments that make up the snake's body.
  • Description:
    • Returns the full snake in segments

Inner Class: Segment

Segment(int x, int y)

  • Parameters:
    • int x: x-coordinate of the segment.
    • int y: y-coordinate of the segment.
  • Description:
    • Represents a single segment of the snake's body, storing its position on the grid.

int getX() and int getY()

  • Returns:
    • int: x-coordinate/ y-coordinate of the segment.
  • Description:
    • Retrieves x-coordinate/ y-coordinate of the segment.
Clone this wiki locally