Skip to content

Renderer Components

rp3002 edited this page Aug 29, 2024 · 4 revisions

Snake Mini-Game Rendering Components

Overview

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.

1. AppleRenderer

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.

Screenshot 2024-08-29 at 1 56 43 AM

Methods:

  • renderApple(): renders the apple on the grid.

2. GridRenderer

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.

Screenshot 2024-08-29 at 2 00 25 AM

Methods:

  • renderGrid(): Renders the grid background.

3. SnakeRenderer

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.

Screenshot 2024-08-29 at 1 58 24 AM

Methods:

  • renderSnake(): Draws the snake’s head and body segments.

    • Calls renderSnakeHead() and renderSnakeBody().
  • 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.

4. SnakeGameRenderer

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.

Screenshot 2024-08-29 at 1 56 54 AM

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.

UML Diagram for Apple, grid, snake and SnakeGame Rendering

Screenshot 2024-08-29 at 10 56 54 AM
Clone this wiki locally