Skip to content

Snake Game is a simple grid-based game where the player controls a growing snake. The snake moves continuously in one direction, eats food to grow longer, and the game ends if it collides with the wall or with itself.

Notifications You must be signed in to change notification settings

hoaffam/SnakeGame

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Snake OpenGL - Quick Learning Guide

1. PROJECT OVERVIEW

What does this game do?

A classic Snake game where you control a snake to eat food, grow longer, and avoid hitting walls or yourself. Built with Modern OpenGL 3.3+ for graphics rendering.

Overall Architecture

┌─────────────────────────────────────────────────────────┐
│                    APPLICATION                          │
│  ┌───────────┐  ┌───────────┐  ┌───────────────────┐   │
│  │  Window   │  │   Input   │  │      Timer        │   │
│  │  (GLFW)   │  │ (Keyboard)│  │  (Delta Time)     │   │
│  └───────────┘  └───────────┘  └───────────────────┘   │
├─────────────────────────────────────────────────────────┤
│                    GAME LOGIC                           │
│  ┌───────────┐  ┌───────────┐  ┌───────────┐           │
│  │   Snake   │  │   Food    │  │   Grid    │           │
│  │ (Movement)│  │ (Spawning)│  │(Collision)│           │
│  └───────────┘  └───────────┘  └───────────┘           │
│  ┌─────────────────────────────────────────┐           │
│  │         Game State Machine              │           │
│  │  Menu → Playing → Paused → GameOver     │           │
│  └─────────────────────────────────────────┘           │
├─────────────────────────────────────────────────────────┤
│                    GRAPHICS (OpenGL)                    │
│  ┌───────────┐  ┌───────────┐  ┌───────────────────┐   │
│  │  Shader   │  │  Texture  │  │  SpriteRenderer   │   │
│  │  (GLSL)   │  │  (Images) │  │  (Draw Quads)     │   │
│  └───────────┘  └───────────┘  └───────────────────┘   │
└─────────────────────────────────────────────────────────┘

2. GAME FLOW DIAGRAM

┌──────────────────────────────────────────────────────────────┐
│                        MAIN LOOP                             │
└──────────────────────────────────────────────────────────────┘
                              │
                              ▼
┌──────────────────────────────────────────────────────────────┐
│ 1. START                                                     │
│    - Initialize GLFW (window library)                        │
│    - Create OpenGL Context                                   │
│    - Load GLAD (OpenGL function loader)                      │
│    - Load Shaders (vertex + fragment)                        │
│    - Create VAO/VBO (vertex data on GPU)                     │
└──────────────────────────────────────────────────────────────┘
                              │
                              ▼
         ┌────────────────────────────────────┐
         │      GAME LOOP (while running)     │
         └────────────────────────────────────┘
                              │
        ┌─────────────────────┼─────────────────────┐
        ▼                     ▼                     ▼
┌──────────────┐    ┌──────────────┐    ┌──────────────────┐
│ 2. INPUT     │    │ 3. UPDATE    │    │ 4. RENDER        │
│              │    │              │    │                  │
│ Poll GLFW    │    │ Move snake   │    │ glClear()        │
│ events       │    │ Check food   │    │ Bind shader      │
│ Handle keys  │    │ Check walls  │    │ Set uniforms     │
│ WASD/Arrows  │    │ Update state │    │ Draw quads       │
│              │    │ Delta time   │    │ Swap buffers     │
└──────────────┘    └──────────────┘    └──────────────────┘
        │                     │                     │
        └─────────────────────┴─────────────────────┘
                              │
                              ▼
                    ┌──────────────────┐
                    │ 5. EXIT          │
                    │ Delete VAO/VBO   │
                    │ Delete shaders   │
                    │ Terminate GLFW   │
                    └──────────────────┘

OpenGL Within The Flow

RENDER PHASE (OpenGL lives here):
─────────────────────────────────
1. glClear(GL_COLOR_BUFFER_BIT)     → Clear screen
2. shader.use()                      → Activate shader program
3. shader.setMat4("projection", p)   → Send matrix to GPU
4. texture.bind()                    → Bind texture
5. glBindVertexArray(VAO)            → Select vertex data
6. glDrawArrays(GL_TRIANGLES, 0, 6)  → Draw!
7. glfwSwapBuffers(window)           → Show on screen

3. FILE STRUCTURE EXPLAINED

snake-opengl/
│
├── src/
│   ├── main.cpp                 ⭐ ENTRY POINT - Start here!
│   │
│   ├── core/                    📦 ENGINE CORE
│   │   ├── application.cpp/hpp  → Main game loop lives here
│   │   ├── window.cpp/hpp       → GLFW window wrapper (OpenGL context)
│   │   ├── input.cpp/hpp        → Keyboard handling
│   │   └── timer.cpp/hpp        → Delta time calculation
│   │
│   ├── graphics/                🎨 OPENGL CODE (ALL OF IT!)
│   │   ├── shader.cpp/hpp       → Compile & use GLSL shaders
│   │   ├── texture.cpp/hpp      → Load images to GPU
│   │   ├── sprite_renderer.cpp  → Draw 2D quads (the workhorse)
│   │   ├── digit_renderer.hpp   → Draw numbers on screen
│   │   └── badge_renderer.hpp   → Draw champion badge
│   │
│   ├── game/                    🎮 PURE GAME LOGIC (No OpenGL!)
│   │   ├── game.cpp/hpp         → State machine (Menu/Play/Pause/Over)
│   │   ├── snake.cpp/hpp        → Snake movement & growth
│   │   ├── grid.cpp/hpp         → Coordinate conversion
│   │   ├── food.cpp/hpp         → Random food spawning
│   │   └── high_score.hpp       → Save/load high score
│   │
│   └── utils/
│       └── resource_manager.cpp → Load & cache shaders/textures
│
├── assets/
│   └── shaders/
│       ├── sprite.vert          → Vertex shader (positions)
│       └── sprite.frag          → Fragment shader (colors)
│
└── external/
    ├── glad/                    → OpenGL function loader
    └── stb_image.h              → Image loading library

Generated for Snake OpenGL Project - Quick Learning Guide

About

Snake Game is a simple grid-based game where the player controls a growing snake. The snake moves continuously in one direction, eats food to grow longer, and the game ends if it collides with the wall or with itself.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors