You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Thomas Forgione baf286a48e
Fixed readme
2 months ago
snake Cleaned structure 2 months ago
.gitignore Initial commit 3 months ago Fixed readme 2 months ago Cleaned structure 2 months ago Cleaned structure 2 months ago

Python Snake

This program is a simple python game, made for implementing your own AIs.

Table of contents

How to download the game

The first step is to clone the repository:

git clone

Then, ensure you have python3 and pygame installed. You can test you have everything by executing the following command:

python3 -c "import pygame"
  • if you get a python3: command not found, it means you don’t have python3, you can install it on ubuntu like so:

    sudo apt install python3
  • if you get a ImportError: No module named 'pygame', it means you don’t have pygame, you can install it on ubuntu like so:

    sudo apt install python3-pip
    sudo pip3 install pygame

How to play the game

There are two runnable scripts in the repository, which are mostly here to be examples:

  • which shows how to create a game with a window and play against an AI.
  • which shows how to run a game with AIs without watching the interface, and thus, really fast.

How to create your own AI

You need to write a class that derives from snake.player.Decision. You have to define a constructor, so you can instanciate it, and implement the method action which takes as parameter the map of the game, and returns the direction you want to take.

For example, you can create a file at the root of this repository containing the following:

from snake.player import Decision, Direction

class MyDecision(Decision):
    def __init__(self):
        super(MyDecision, self).__init__()

    def action(self, game_map):
        # Do stuff
        return Direction.DOWN

and you can create an executable file containing the following:

#!/usr/bin/env python3

import contextlib
with contextlib.redirect_stdout(None):
    import pygame

from import Map
from import Game
from snake.window import Window
from snake.player import Player, Direction, ConstantDecision, KeyboardDecision

from other import MyDecision

def main():

    width = 40
    height = 40

    game = Game(width, height, [
        Player(0, 0, (255, 0, 0), KeyboardDecision(Direction.RIGHT)),
        Player(width - 1, height - 1, (0, 255, 0), MyDecision()),

    window = Window(game, 20)


if __name__ == '__main__':

and it should work.