Thanks to visit codestin.com
Credit goes to github.com

Skip to content

AI-powered Snake game in Python, demonstrating various search algorithms (A*, BFS, DFS, Greedy, Online Search) with visualizations using Pygame.

License

Notifications You must be signed in to change notification settings

Ta-Pc/AI-Snake-Game

AI-Powered Snake Game with Search Algorithm Visualizations

License Code Style: black

A Python-based implementation of the classic Snake game, featuring AI agents powered by various search algorithms and interactive visualizations of their exploration process.


✨ AI in Action: Visualization vs. Gameplay

This project features two distinct modes for interacting with the AI. You can either watch the algorithm's "thought process" in the Visualization Mode or see it take control of the snake in the Autonomous Gameplay Mode.

1. Pathfinding Visualization (AI Vision) 2. Autonomous Gameplay (AI Player)
AI Pathfinding Visualization AI Autonomous Gameplay

This mode is an educational tool designed to show how the A* search algorithm works. The colored squares visualize the "open set" (nodes to be explored) and "closed set" (nodes already explored). It provides a clear, step-by-step look at the AI's decision-making process as it finds the most optimal path to the food.

This mode demonstrates the practical application of the A* algorithm. Here, the AI takes direct control of the snake and plays the game autonomously. This showcases the algorithm's real-time performance and its ability to act as a robust agent within the rules and dynamics of the game environment.


πŸš€ Features

This project is more than just a game; it's a comprehensive tool for exploring and understanding AI search algorithms.

Feature Description
🧠 Multiple AI Agents Switch between seven different AI algorithms, from simple heuristics like Greedy Best-First Search to complex pathfinding like A*.
πŸ“Š Real-Time Visualization Watch the AI's "thought process" in real-time as it explores the game board, providing a clear visual understanding of how each algorithm works.
πŸ•ΉοΈ Full Joystick Support Play the game or navigate all menus using a standard joystick, in addition to keyboard controls.
πŸ§‘β€πŸ’» Human Playable Mode Take control yourself and try to beat the high scores in a classic, polished Snake experience.
βš™οΈ Customizable Difficulty Adjust the game speed across four difficulty levels to provide a challenge for both human players and the AI agents.
πŸ† Persistent High Scores Your best scores for each difficulty level are saved and displayed, encouraging replayability.
🎨 Polished UI/UX A clean and intuitive user interface with a full main menu, level selection, and in-game pause functionality.

πŸ“Έ Gallery

Main Menu & Level Select
Main Menu Levels

Gameplay & High Scores
Human Player High Scores


πŸ› οΈ Tech Stack

Component Technologies
Core Language Python 3.7+
Libraries & Frameworks Pygame (for game logic, rendering, controls), NumPy (numerical operations), Matplotlib (visualizations)
Development Tools Pyinstrument (for optional code profiling), black (for code formatting)

🧠 Click to see Detailed Algorithm Explanations

AI Play Algorithms

  • Online Search (Reinforcement Learning with Danger/Success Memory): This agent learns by experience, remembering dangerous locations and successful paths. It uses a custom heuristic incorporating learned danger to guide its decisions, dynamically adapting to the game environment.
  • A Search (with Dynamic Difficulty Adjustment):* Employs the A* search algorithm with Manhattan distance as the heuristic. To manage search complexity and adapt to the game's dynamics, it incorporates a DFS component.
  • Breadth-First Search (BFS): A systematic algorithm that guarantees finding the shortest path to the food but can be computationally expensive.
  • Depth-First Search (DFS): Explores a single branch as deeply as possible before backtracking. While potentially faster than BFS, it doesn't guarantee finding the shortest path.
  • Greedy Best-First Search: A heuristic-driven approach that prioritizes the move seemingly closest to the food, which can miss optimal solutions.
  • Hamiltonian Cycle (Demonstration): A deterministic approach following a pre-calculated path that visits every grid cell exactly once.

AI Vision Algorithms (Visualization)

  • These algorithms focus on visualizing the search process itself, providing insights into how different strategies explore the game board to find a path to the food for A*, BFS, DFS, Greedy, and Online Search.

βš™οΈ Getting Started

Installation Instructions
  1. Prerequisites: Ensure you have Python 3.7+ installed.
  2. Clone the repository:
    git clone https://github.com/Ta-Pc/AI-Snake-Game.git
    cd AI-Snake-Game
  3. Set up a virtual environment:
    • On Linux/macOS:
      python3 -m venv .venv
      source .venv/bin/activate
    • On Windows:
      python -m venv .venv
      .venv\Scripts\activate
  4. Install dependencies:
    pip install -r requirements.txt
Running the Game & Controls
  • To run the main game:

    python main.py
  • Controls:

    Control Action
    Arrow Keys Control snake direction & navigate menus
    Joystick Control snake direction & navigate menus
    SPACE Pause / Resume the game
    ESC Return to the main menu or exit the visualiser

🌱 Future Enhancements

  • Implementing more advanced AI techniques (e.g., Q-learning, Deep Reinforcement Learning).
  • Adding more sophisticated game elements and levels.
  • Improving the user interface and visualization tools.
  • Incorporating sound effects and music.

###🀝 Contributing Contributions are welcome! Feel free to submit pull requests for bug fixes, new features, or improved documentation.

πŸ“„ License

This project is licensed under the Apache License, Version 2.0. See the LICENSE file for details.

Releases

No releases published

Packages

No packages published

Languages