Pytron
This commit is contained in:
parent
bb6f95bd2a
commit
471a1e0141
24
README.md
24
README.md
|
@ -1,4 +1,4 @@
|
|||
# Python Snake
|
||||
# Pytron
|
||||
|
||||
This program is a simple python game, made for implementing your own AIs.
|
||||
|
||||
|
@ -12,7 +12,7 @@ This program is a simple python game, made for implementing your own AIs.
|
|||
The first step is to clone the repository:
|
||||
|
||||
``` sh
|
||||
git clone https://gitea.tforgione.fr/tforgione/python-snake
|
||||
git clone https://gitea.tforgione.fr/tforgione/pytron
|
||||
```
|
||||
|
||||
Then, ensure you have `python3` and `pygame` installed. You can test you have everything by executing the following command:
|
||||
|
@ -40,16 +40,16 @@ There are two runnable scripts in the repository, which are mostly here to be ex
|
|||
|
||||
## 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.
|
||||
You need to write a class that derives from `snake.player.Player`. 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 `mydecision.py` at the root of this repository containing the following:
|
||||
For example, you can create a file `myplayer.py` at the root of this repository containing the following:
|
||||
|
||||
``` python
|
||||
from snake.player import Decision, Direction
|
||||
from snake.player import Player, Direction
|
||||
|
||||
class MyDecision(Decision):
|
||||
class MyPlayer(Player):
|
||||
def __init__(self):
|
||||
super(MyDecision, self).__init__()
|
||||
super(MyPlayer, self).__init__()
|
||||
|
||||
def action(self, game_map):
|
||||
# Do stuff
|
||||
|
@ -66,11 +66,9 @@ with contextlib.redirect_stdout(None):
|
|||
import pygame
|
||||
|
||||
from snake.map import Map
|
||||
from snake.game import Game
|
||||
from snake.game import Game, PositionPlayer
|
||||
from snake.window import Window
|
||||
from snake.player import Player, Direction, ConstantDecision, KeyboardDecision
|
||||
|
||||
from other import MyDecision
|
||||
from snake.player import Player, Direction, ConstantPlayer, KeyboardPlayer
|
||||
|
||||
def main():
|
||||
pygame.init()
|
||||
|
@ -79,8 +77,8 @@ def main():
|
|||
height = 40
|
||||
|
||||
game = Game(width, height, [
|
||||
Player(0, 0, (255, 0, 0), KeyboardDecision(Direction.RIGHT)),
|
||||
Player(width - 1, height - 1, (0, 255, 0), MyDecision()),
|
||||
PositionPlayer(1, KeyboardPlayer(Direction.RIGHT), [0, 0]),
|
||||
PositionPlayer(2, ConstantPlayer(Direction.LEFT), [width - 1, height - 1]),
|
||||
])
|
||||
|
||||
window = Window(game, 20)
|
||||
|
|
8
play.py
8
play.py
|
@ -4,10 +4,10 @@ import contextlib
|
|||
with contextlib.redirect_stdout(None):
|
||||
import pygame
|
||||
|
||||
from snake.map import Map
|
||||
from snake.game import Game, PositionPlayer
|
||||
from snake.window import Window
|
||||
from snake.player import Player, Direction, ConstantPlayer, KeyboardPlayer
|
||||
from tron.map import Map
|
||||
from tron.game import Game, PositionPlayer
|
||||
from tron.window import Window
|
||||
from tron.player import Player, Direction, ConstantPlayer, KeyboardPlayer
|
||||
|
||||
def main():
|
||||
pygame.init()
|
||||
|
|
|
@ -5,7 +5,7 @@ This module contains everything related to the game.
|
|||
from time import sleep
|
||||
from enum import Enum
|
||||
|
||||
from snake.map import Map
|
||||
from tron.map import Map
|
||||
|
||||
class Case(Enum):
|
||||
EMPTY = 0
|
||||
|
@ -61,7 +61,7 @@ class Game:
|
|||
"""
|
||||
Returns a new game from its width, height, and number of players.
|
||||
|
||||
Width and height are the number of blocs available in the snake map.
|
||||
Width and height are the number of blocs available in the tron map.
|
||||
"""
|
||||
self.width = width
|
||||
self.height = height
|
|
@ -7,7 +7,7 @@ from enum import Enum
|
|||
|
||||
class Direction(Enum):
|
||||
"""
|
||||
This enum represents the different directions a snake can go to.
|
||||
This enum represents the different directions a tron can go to.
|
||||
"""
|
||||
UP = 1
|
||||
RIGHT = 2
|
||||
|
@ -56,7 +56,7 @@ class KeyboardPlayer(Player):
|
|||
""""
|
||||
This is the key board interaction.
|
||||
|
||||
It uses Z, Q, S and D keys to move the snake.
|
||||
It uses Z, Q, S and D keys to move the tron.
|
||||
"""
|
||||
def __init__(self, initial_direction):
|
||||
"""
|
||||
|
@ -67,7 +67,7 @@ class KeyboardPlayer(Player):
|
|||
|
||||
def manage_event(self, event):
|
||||
"""
|
||||
Changes the direction of the snake depending on the keyboard inputs.
|
||||
Changes the direction of the tron depending on the keyboard inputs.
|
||||
"""
|
||||
import pygame
|
||||
if event.type == pygame.KEYDOWN:
|
||||
|
@ -82,7 +82,7 @@ class KeyboardPlayer(Player):
|
|||
|
||||
def action(self, game):
|
||||
"""
|
||||
Returns the direction of the snake.
|
||||
Returns the direction of the tron.
|
||||
"""
|
||||
return self.direction
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
"""
|
||||
This module contains the classes that helps us watch a game of snake.
|
||||
This module contains the classes that helps us watch a game of tron.
|
||||
"""
|
||||
|
||||
import pygame
|
||||
|
@ -12,7 +12,7 @@ class Window:
|
|||
"""
|
||||
Creates a new window from a game
|
||||
|
||||
Factor is the size of a square of the snake.
|
||||
Factor is the size of a square of the tron.
|
||||
"""
|
||||
self.factor = factor
|
||||
|
Loading…
Reference in New Issue