summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Ward <peteraward@gmail.com>2009-09-28 09:35:55 +1000
committerPeter Ward <peteraward@gmail.com>2009-09-28 09:35:55 +1000
commit678f06a14ef733ce5f7265846a9fb14265a34bcd (patch)
treecec8de676510e54eaac1fd6fe0577495b0455d70
parent997b1a2cf7ff5ee5bdf362e4d06b560ad59be4e6 (diff)
Started work on draw_board.
-rw-r--r--snake.py37
1 files 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()