Skip to content

Snake Grid

rp3002 edited this page Aug 26, 2024 · 1 revision

Overview

The SnakeGrid class is a designed specifically for the Snake mini-game. This extends the general Grid class and provides additional functionality such as checking for collisions and having grid positions for rendering.

Class: SnakeGrid

Package

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

Imports

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.csse3200.game.components.minigame.Grid;

Description

The SnakeGrid class represents a grid specifically tailored for the Snake mini-game. It includes default dimensions and methods that assist with snake-specific functionality, such as detecting collisions and having grid positions for rendering.

Constructor

SnakeGrid()

  • Description:
    • New instance of the SnakeGrid with default dimensions of 20x20 cells.

Methods

boolean isCollision(int x, int y)

  • Parameters:
    • int x and int y: The x-coordinate/ y-coordinate of the cell to check.
  • Returns:
    • boolean: true if the specified cell is occupied (indicating a potential collision), false otherwise.
  • Description:
    • Checks whether a particular cell on the grid is occupied. This is primarily used to detect collisions with the snake's body or other obstacles.

Vector2 tileToWorldPosition(Vector2 gridPosition, int cellSize)

  • Parameters:
    • Vector2 gridPosition: The position on the grid that needs to be converted to world coordinates.
    • int cellSize: The size of each cell in pixels.
  • Returns:
    • Vector2: The corresponding world position for rendering.
  • Description:
    • Calculate the offset to centre the grid and calculate the world position
Clone this wiki locally