Snaky
A snake game, three versions of AI included, implemented in python, pygame.
Install / Use
/learn @memoiry/SnakyREADME
Snaky
Snaky is a Snake game series including a basic one-player version and three versions of AI to play Snake automatically.
The project is implemented in Python.
If you like it, Please give it a star, Thanks!
Require
You should have pygame module installed.
Final Version Demo
<img src="./images/snaky_ai_v3.gif" width="350">Usage
Basic function - One player
In this version, you can control the movement of the snake to eat the apple to grow.
git clone https://github.com/memoiry/Snaky
cd Snaky
python snaky.py
<img src="https://ooo.0o0.ooo/2017/03/19/58ce1d1270940.gif" width="350">
It was actually manually controlled by myself(hard to control while recording the gif....so it's poor..)
AI version 1 - Based on Hamiltonian path
A perfect strategy, ensuring filling the screen, but the speed is slow.
python snaky_ai_v1.py
<img src="./images/snaky_ai_v1.gif" width="350">
AI version 2 - Based on BFS
A simple BFS strategy make the snake trapped in local optimal point and not considering future.
python snaky_ai_v2.py
<img src="./images/snaky_ai_v2.gif" width="350">
AI version 3 - Based on BFS(shortest path), forward checking and follow the tail(longest path).
In this AI version. The algorithm is constructed as follow.
To find snake S1's next moving direction D, the AI snake follows the steps below:
- Compute the shortest path
P1from snakeS1's head to the food. IfP1exists, go to step 2. Otherwise, go to step 4. - Move a virtual snake
S2(the same asS1) to eat the food along pathP1. - Compute the longest path
P2from snakeS2's head to its tail. IfP2exists, letDbe the first direction in pathP1. Otherwise, go to step 4. - Compute the longest path
P3from snakeS1's head to its tail. IfP3exists, let D be the first direction in pathP3. Otherwise, go to step 5. - Let
Dbe the direction that makes the snake the farthest from the food.
python snaky_ai_v3.py
<img src="./images/snaky_ai_v3.gif" width="350">
Enjoy!
Reference
Related Skills
node-connect
343.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
90.0kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
343.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
343.1kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
