# astralranger/multi-dimensional-snake_game architecture

> Architecture diagram of the GitHub repository astralranger/multi-dimensional-snake_game, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/astralranger/multi-dimensional-snake_game
- Repository: https://github.com/astralranger/multi-dimensional-snake_game

## Overview

##### Main Components
- Entry Point & Game Loop (main.java): Represents the initialization and continuous update cycle. It manages the JFrame, game initialization (init() & initGame()), and the game loop (updating state and repainting).
- Game Logic – Snake (Snake.java & LinkedVector.java): Contains the snake’s movement, growth, collision detection, directional changes, and linked body segments (using Vector and LinkedVector).
- Rendering (Graphics3D.java): Handles 3D projection computations, rotations, and drawing the cube along with its faces, grid tiles, and dynamic elements (snake, fruit, obstacles).
- UI Components (MainMenu.java & MenuOverlay.java): Provides the main menu, overlays, and input handling for starting the game, modifying settings, and displaying game over screens.
- Game Objects (Object.java): Represents in-game entities like fruit and obstacles, including collision checking and spawning.

##### Relationships & Interactions
- The entry point (main.java) initializes the game environment, linking the game loop with the rendering engine (Graphics3D) and UI layers (MainMenu/MenuOverlay).
- The Assets class serves as a configuration hub, providing shared styling and high score management across game components.
- The Snake component interacts with the Game Objects via collision detection – calling methods in Object.java to determine if a fruit is eaten or a collision with borders or itself has occurred.
- Rendering (Graphics3D) is tightly coupled with game state: it takes updated data from the Snake and other game objects to calculate new projections (with methods like update(), drawCube(), rotateX(), and rotateY()).
- User interactions in the MainMenu/MenuOverlay trigger game state changes (e.g., starting a new game, updating difficulty), passing control from the UI to the game logic in main.java.

##### Architectural Patterns & Design Principles
- Separation of Concerns: Each file/class has a distinct responsibility (e.g., data management, rendering, game logic, UI).
- Object-Oriented Programming: The project employs encapsulation (classes for Assets, Snake, etc.), inheritance (MainMenu extends MenuOverlay), and composition (LinkedVector building the snake body).
- Event-Driven and Game Loop Architecture: The main loop dynamically updates and repaints the game components based on user input and internal state changes.

##### Relevant Technologies, Frameworks, or Libraries
- Java and Swing: For windowing and graphical user interface.
- Core Java APIs: For file I/O (saving/loading high scores), handling events, and basic graphical rendering using Graphics2D.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Game Engine Layer
    subgraph "Game Engine Layer"
        mainLoop["Main Loop / Entry (main.java)"]:::engine
    end

    %% Domain/Business Logic Layer
    subgraph "Domain/Business Logic"
        snake["Snake (Snake.java)"]:::logic
        linkedVector["LinkedVector (LinkedVector.java)"]:::logic
        gameObjects["Game Objects (Object.java)"]:::logic
    end

    %% Rendering Layer
    subgraph "Rendering Layer"
        rendering["Rendering Engine (Graphics3D.java)"]:::render
    end

    %% Assets & Data Layer
    subgraph "Assets & Data Layer"
        assets["Asset Manager (Assets.java)"]:::assets
    end

    %% UI Layer
    subgraph "UI Layer"
        mainMenu["Main Menu (MainMenu.java)"]:::ui
        menuOverlay["Menu Overlay (MenuOverlay.java)"]:::ui
    end

    %% Connections from Game Engine to other layers
    mainLoop -->|"initializes"| snake
    mainLoop -->|"initializes"| linkedVector
    mainLoop -->|"triggers-update"| rendering
    mainLoop -->|"activates"| mainMenu
    mainLoop -->|"activates"| menuOverlay

    %% UI input back to Game Engine
    mainMenu -->|"inputEvent"| mainLoop
    menuOverlay -->|"inputEvent"| mainLoop

    %% Assets configuration connections
    assets -->|"configures"| mainMenu
    assets -->|"configures"| menuOverlay
    assets -->|"configures"| rendering

    %% Domain interactions
    snake -.->|"uses"| linkedVector
    snake -->|"collisionCheck"| gameObjects
    snake -->|"stateUpdate"| rendering
    linkedVector -->|"bodyUpdate"| rendering

    %% Styles for components
    classDef engine fill:#a2d2ff,stroke:#023e8a,stroke-width:2px;
    classDef logic fill:#ffafcc,stroke:#d62828,stroke-width:2px;
    classDef render fill:#caffbf,stroke:#2b9348,stroke-width:2px;
    classDef assets fill:#ffe066,stroke:#ff9f1c,stroke-width:2px;
    classDef ui fill:#bde0fe,stroke:#0077b6,stroke-width:2px;

    %% Click Events
    click mainLoop "https://github.com/astralranger/multi-dimensional-snake_game/blob/main/main.java"
    click snake "https://github.com/astralranger/multi-dimensional-snake_game/blob/main/Snake.java"
    click linkedVector "https://github.com/astralranger/multi-dimensional-snake_game/blob/main/LinkedVector.java"
    click rendering "https://github.com/astralranger/multi-dimensional-snake_game/blob/main/Graphics3D.java"
    click assets "https://github.com/astralranger/multi-dimensional-snake_game/blob/main/Assets.java"
    click mainMenu "https://github.com/astralranger/multi-dimensional-snake_game/blob/main/MainMenu.java"
    click menuOverlay "https://github.com/astralranger/multi-dimensional-snake_game/blob/main/MenuOverlay.java"
    click gameObjects "https://github.com/astralranger/multi-dimensional-snake_game/blob/main/Object.java"
```
