From 678f06a14ef733ce5f7265846a9fb14265a34bcd Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Mon, 28 Sep 2009 09:35:55 +1000 Subject: Started work on draw_board. --- snake.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/snake.py b/snake.py index 341a242..441e2ec 100644 --- a/snake.py +++ b/snake.py @@ -1,9 +1,11 @@ +from __future__ import division import random from common import * import pygame pygame.init() +from pygame.locals import * class SnakeEngine(object): def __init__(self, rows, columns, n_apples, width=800, height=600, fullscreen=False): @@ -13,6 +15,9 @@ class SnakeEngine(object): flags |= pygame.FULLSCREEN pygame.display.set_mode((width, height), flags) + self.width = width + self.height = height + self.bots = {} self.new_game(rows, columns, n_apples) @@ -38,9 +43,37 @@ class SnakeEngine(object): def remove_bot(self, name): del self.bots[name] + def draw_board(self): + rows = len(self.board) + assert rows > 0 + columns = len(self.board[0]) + + board_aspect = columns / rows + screen_aspect = self.width / self.height + if board_aspect > screen_aspect: + # restrict width + width = self.width + height = width / board_aspect + else: + # restrict height + height = self.height + width = height * board_aspect + + xscale = width / columns + yscale = height / rows + + for y, row in enumerate(self.board): + for x, cell in enumerate(row): + r = Rect(y * yscale, x * xscale, xscale, yscale) + print r + def run(self): - pass + self.draw_board() if __name__ == '__main__': - game = SnakeEngine(8, 16, 10) + from bots import random_bot + + game = SnakeEngine(16, 8, 10) + game.add_bot('Bob', random_bot) + game.run() -- cgit v1.2.3