-
Notifications
You must be signed in to change notification settings - Fork 1
Renderer Components
In the Snake mini-game, rendering is a crucial aspect, as it ensures that various game elements, such as the apple, grid, and snake, are visually represented on the screen. The game uses multiple renderer classes, each responsible for drawing specific components. These components utilise the SpriteBatch
from LibGDX to draw textures onto the screen.
Purpose: The Apple Renderer is responsible for drawing the apple on the game grid. It positions the apple on the grid based on its coordinates and ensures that the apple is rendered using the specified texture.
Rendered Element: The apple, which is a target for the snake to consume, is placed on the grid and displayed using an apple texture.
Methods:
-
renderApple()
: renders the apple on the grid.
Purpose: The Grid Renderer is tasked with rendering the grid background for the game. This grid serves as the playing field where the snake moves and the apple appears. The grid cells are drawn using a grass texture, giving the game a natural look.
Rendered Element: The game grid, which is made up of individual cells, is displayed using a grass texture to create a visual background for the game.
Methods:
-
renderGrid()
: Renders the grid background.
Purpose: The Snake Renderer is responsible for drawing the snake and its segments on the grid. It renders the snake's head, as well as its body segments, with appropriate textures and rotations based on the snake's direction and movement.
Rendered Element: The snake, including its head and body segments, is drawn on the grid. The renderer uses different textures for the head and body segments, adjusting for orientation and bends in the snake's body.
Methods:
-
renderSnake()
: Draws the snake’s head and body segments.- Calls
renderSnakeHead()
andrenderSnakeBody()
.
- Calls
-
renderSnakeHead(float offsetX, float offsetY)
: Draws the snake's head with rotation based on direction. -
renderSnakeBody(float offsetX, float offsetY)
: Draws the body segments with textures and rotations. -
getRotationForDirection(Direction direction)
: Calculates rotation angle for the snake's head. -
getBentRotation(Direction prevDirection, Direction currentDirection)
: Calculates rotation angle for bent body segments.
Purpose: The Snake Game Renderer is the overarching renderer that combines all individual renderers (Apple Renderer, Grid Renderer, Snake Renderer) to render the entire game. It ensures that the grid, apple, snake, and additional elements like the scoreboard are displayed in a cohesive manner.
Rendered Element: The entire game screen, including the grid, snake, apple, and scoreboard, is drawn together to create the full game experience.
Methods:
-
render(int score)
: Renders the entire snake game including grid, apple, snake, and scoreboard. -
loadAssets()
: Loads the textures and other assets required for rendering. -
unloadAssets()
: Unloads the assets used in the game to free resources. -
dispose()
: Disposes of resources used by the renderer, including textures and the SpriteBatch.