A fully-featured AI Snake Game in Python 3 using Tkinter. Learned Python GUI, game logic.
342
Snake Game Project!
Papia Gamers Snake Game is a modern, interactive Python 3 game designed to run on desktop or via Docker containers. It is built entirely using the Tkinter GUI framework, making it simple yet visually appealing. The game includes an AI mode that automatically moves the snake toward the food, as well as manual control using Arrow keys or W, A, S, D. Players can select the difficulty level from Easy, Medium, or Hard, adjusting the snake’s speed to suit beginners or advanced users.
The project structure is minimal, containing only the essential files: game.py, the Dockerfile, and optional assets if you want to include images or sounds. The Dockerfile allows developers and players to run the game in a consistent environment across Linux, macOS, and Windows (with proper GUI support). The use of Docker ensures there are no dependency conflicts and eliminates the need to manually configure Python versions or packages.
Upon launching, the game presents a main menu where users can start the game, choose the difficulty level, or exit. The main menu is styled with Tkinter buttons featuring hover effects and sound cues simulated via the system bell. The snake game itself supports fullscreen mode, ensuring an immersive experience, and the score panel continuously updates with the current score and selected level.
Players can interact with the snake either manually or allow the AI to control it. The AI is designed to follow a simple strategy, moving the snake toward the nearest food while avoiding direct collisions. While simple, this approach demonstrates foundational AI logic and can be enhanced further for smarter pathfinding and obstacle avoidance.
How to Use / Run the Game
Clone the repository
git clone https://github.com/papiagamers/snake-game.git
cd snake-game
Run locally using Python 3
python3 game.py
Make sure Python 3 and Tkinter are installed on your system.
#* Linux only: Requires X11 forwarding for GUI display. For Windows/macOS, you may use VNC or XQuartz. *
Code: Main Snake Movement
def next_turn(snake_obj, food_obj):
global score, direction, food
# AI movement
if ai_mode:
ai_move()
x, y = snake_obj.coordinates[0]
if direction == "up": y -= SPACE_SIZE
elif direction == "down": y += SPACE_SIZE
elif direction == "left": x -= SPACE_SIZE
elif direction == "right": x += SPACE_SIZE
snake_obj.coordinates.insert(0, [x, y])
square = game_canvas.create_rectangle(x, y, x + SPACE_SIZE, y + SPACE_SIZE, fill=SNAKE_COLOR)
snake_obj.squares.insert(0, square)
# Food check
if x == food.coordinates[0] and y == food.coordinates[1]:
score += 1
update_score()
game_canvas.delete(food.id)
food = Food()
else:
del snake_obj.coordinates[-1]
game_canvas.delete(snake_obj.squares[-1])
del snake_obj.squares[-1]
if check_collisions(snake_obj):
game_over()
else:
window.after(SPEED, next_turn, snake_obj, food)
Supported Features
AI-driven snake movement
Manual control (Arrow / WASD)
Main menu and level selection
Fullscreen support
Score tracking
Sound cues using system bell
Easy Docker deployment
Why Docker? Docker ensures you can run this Python 3 game without worrying about local environment differences. Whether you are on Linux, macOS, or Windows, the container keeps everything self-contained. This is particularly useful for demonstrations, classroom use, or sharing the game with others.
Future Enhancements
Network multiplayer support
Smarter AI with pathfinding
Sound effects and music
Custom skins and themes
Content type
Image
Digest
sha256:a3a5df968…
Size
391.4 MB
Last updated
9 months ago
docker pull papiagamers/snake-game