summaryrefslogtreecommitdiff
path: root/console_snake.py
blob: 8c327e554c9f49eeefa89dfceeefe9798577e94e (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python

from __future__ import division

import time

import curses

from common import *
from snake import SnakeEngine

class ConsoleSnakeEngine(SnakeEngine):
    def new_game(self, *args):
        super(ConsoleSnakeEngine, self).new_game(*args)

        self.window = curses.initscr()
        curses.start_color()

        curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
        curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)

        self.EMPTY_COLOUR = curses.color_pair(0)
        self.APPLE_COLOUR = curses.color_pair(1)
        self.SNAKE_COLOUR = curses.color_pair(4)

    def draw_board(self):
        # Draw grid.
        for y, row in enumerate(self.board):
            for x, cell in enumerate(row):
                char = '.'
                colour = self.EMPTY_COLOUR

                # Draw the things on the square.
                if cell == Squares.APPLE:
                    char = '@'
                    colour = self.APPLE_COLOUR

                elif cell.isalpha(): # Snake...
#                    colour = self.bots[cell.lower()][1]
                    char = cell
                    colour = self.SNAKE_COLOUR

                self.window.addstr(y, x, char, colour)

    def run(self):
        while self.bots:
            # Clear the screen.
            self.window.erase()

            # Draw the board.
            self.draw_board()

            # Update the display.
            self.window.refresh()
            time.sleep(0.025)

            # Let the snakes move!
            self.update_snakes()

def main(*args):
    from bots import *
    from oldbot import BotWrapper

    game = ConsoleSnakeEngine(25, 25, 50)
    game.add_bot(right_bot)
    game.add_bot(random_bot)
    game.add_bot(random_bounds_bot)
    game.add_bot(random_square_bot)
    game.add_bot(BotWrapper('oldbots/peter.py'))
    game.run()

if __name__ == '__main__':
    curses.wrapper(main)