From 997b1a2cf7ff5ee5bdf362e4d06b560ad59be4e6 Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Mon, 28 Sep 2009 09:22:31 +1000 Subject: Initial commit. --- .hgignore | 2 ++ bots.py | 16 ++++++++++++++++ common.py | 4 ++++ snake.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 .hgignore create mode 100644 bots.py create mode 100644 common.py create mode 100644 snake.py diff --git a/.hgignore b/.hgignore new file mode 100644 index 0000000..85576b0 --- /dev/null +++ b/.hgignore @@ -0,0 +1,2 @@ +glob:*.pyc +glob:*.orig diff --git a/bots.py b/bots.py new file mode 100644 index 0000000..cf23602 --- /dev/null +++ b/bots.py @@ -0,0 +1,16 @@ +import random + +def random_bot(board, (x, y)): + height = len(board) + width = len(board[0]) + moves = [] + if x > 0: + moves.append('L') + if x < width - 1: + moves.append('R') + if y > 0: + moves.append('U') + if y < height - 1: + moves.append('D') + return random.choice(moves) + diff --git a/common.py b/common.py new file mode 100644 index 0000000..f19ebd9 --- /dev/null +++ b/common.py @@ -0,0 +1,4 @@ +class Squares(object): + EMPTY = '.' + APPLE = '*' + diff --git a/snake.py b/snake.py new file mode 100644 index 0000000..341a242 --- /dev/null +++ b/snake.py @@ -0,0 +1,46 @@ +import random + +from common import * + +import pygame +pygame.init() + +class SnakeEngine(object): + def __init__(self, rows, columns, n_apples, width=800, height=600, fullscreen=False): + super(SnakeEngine, self).__init__() + flags = 0 + if fullscreen: + flags |= pygame.FULLSCREEN + pygame.display.set_mode((width, height), flags) + + self.bots = {} + + self.new_game(rows, columns, n_apples) + + def new_game(self, rows, columns, n_apples): + self.board = [[Squares.EMPTY for x in xrange(columns)] for y in xrange(rows)] + for i in xrange(n_apples): + x = random.randint(0, columns - 1) + y = random.randint(0, rows - 1) + self.board[y][x] = Squares.APPLE + + def add_bot(self, name, bot): + """ + A bot is a callable object, with this method signature: + def bot_callable( + board=[[cell for cell in row] for row in board], + position=(snake_x, snake_y) + ): + return random.choice('RULD') + """ + self.bots[name] = bot + + def remove_bot(self, name): + del self.bots[name] + + def run(self): + pass + +if __name__ == '__main__': + game = SnakeEngine(8, 16, 10) + -- cgit v1.2.3