{"id":26944699,"url":"https://github.com/sameerjain/ai-snake-game","last_synced_at":"2026-05-09T02:33:55.456Z","repository":{"id":207218292,"uuid":"718239367","full_name":"SameerJain/AI-snake-game","owner":"SameerJain","description":"Implemented a Q-learning agent to play and learn the classic Snake game, leveraging PyTorch for Neural Network. Visualized using Matplotlib, showcasing agent's evolving performance over time. Built UI using Pygame to visualize Snake, including food, and current score.","archived":false,"fork":false,"pushed_at":"2024-09-12T04:04:20.000Z","size":81,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-02T18:18:03.598Z","etag":null,"topics":["anaconda","conda","matplotlib","neural-network","numpy","pygame","python3","pytorch","q-learning"],"latest_commit_sha":null,"homepage":"https://www.youtube.com/watch?v=RaI2uuSOi6c","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SameerJain.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-11-13T17:10:44.000Z","updated_at":"2024-09-12T04:30:58.000Z","dependencies_parsed_at":null,"dependency_job_id":"cabb7685-ce29-4684-8fca-ead487bd10fe","html_url":"https://github.com/SameerJain/AI-snake-game","commit_stats":null,"previous_names":["sameerjain/ai-snake-game"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/SameerJain/AI-snake-game","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SameerJain%2FAI-snake-game","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SameerJain%2FAI-snake-game/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SameerJain%2FAI-snake-game/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SameerJain%2FAI-snake-game/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SameerJain","download_url":"https://codeload.github.com/SameerJain/AI-snake-game/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SameerJain%2FAI-snake-game/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273416254,"owners_count":25101806,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["anaconda","conda","matplotlib","neural-network","numpy","pygame","python3","pytorch","q-learning"],"created_at":"2025-04-02T18:18:05.728Z","updated_at":"2026-05-09T02:33:50.415Z","avatar_url":"https://github.com/SameerJain.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"**Video Demonstration:**\nhttps://www.youtube.com/watch?v=RaI2uuSOi6c\n\n\n# Snake Game Q-Learning AI\nThis project implements an AI agent that learns to play the classic Snake game using Q-learning, a type of reinforcement learning algorithm. The AI improves its performance over time by learning from its experiences in the game. NOTE: This uses Anaconda\n\n![Screenshot 2024-09-12 000142](https://github.com/user-attachments/assets/e300d3eb-b5f4-41e3-8bca-62c1dd5e1d7d)\n\n![i](https://github.com/user-attachments/assets/f4590a7e-2564-447c-838d-77f5bc090a36)\n\n![ui](https://github.com/user-attachments/assets/47bee492-8a45-4511-b356-6954971b5aae)\n\n\n## Requirements\n\nPython 3.7+\n\nAnaconda\n\nPyTorch\n\nPygame\n\nMatplotlib\n\nNumPy\n\n\n## How to get started\n1) Download the zip and extract the file\n\n2) Run Anaconda:\n`conda create -n pygame_env`\n\n3) Run the AI Agent:\n`python agent.py`\n\n4) Watch the AI learn!\n\n## Project Structure\n\n**snake_game.py:** Implements the Snake game using Pygame.\n\n**model.py:** Defines the neural network model for Q-learning.\n\n**agent.py:** Implements the Q-learning agent that interacts with the game.\n\n**plot.py:** Provides functionality to visualize the training progress.\n\n\n### Contains:\n\nGame initialization and reset\n\nFood placement\n\nGame state updates\n\nCollision detection\n\nUI rendering\n\n\n## How it Works\n\nThe Q-learning process works as follows:\n\n**State Representation:**\nThe game state is represented as a vector of 11 binary values in get_state() method.\nThis includes information about danger directions, current direction, and food location.\n\n\n**Q-Network:**\nThe Q-function is approximated using a neural network (Linear_QNet class).\nIt takes the state as input and outputs Q-values for each possible action.\n\n\n**Action Selection:**\nThe agent uses an epsilon-greedy strategy in get_action() method.\nWith probability epsilon, it chooses a random action (exploration).\nOtherwise, it chooses the action with the highest Q-value (exploitation).\nEpsilon decreases over time to favor exploitation as the agent learns.\n\n\n**Learning Process:**\nAfter each action, the agent receives a reward and observes the new state.\nThe experience (state, action, reward, next_state) is stored in memory.\nThe Q-network is updated using experience replay in train_long_memory():\n\nA batch of experiences is randomly sampled from memory.\nThe Q-values are updated using the Bellman equation:\nQ(s,a) = r + γ * max(Q(s',a'))\nThe network is trained to minimize the difference between predicted and target Q-values.\n\n\n**Game Integration:**\nThe SnakeGameAI class provides the environment for the agent.\nIn each step, the game updates based on the agent's action and returns the new state and reward.\n\n\n**Visualization:**\nThe plot() function provides real-time visualization of the agent's performance.\nIt shows how the score improves over time as the agent learns.\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsameerjain%2Fai-snake-game","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsameerjain%2Fai-snake-game","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsameerjain%2Fai-snake-game/lists"}