From 494e519103aa8bde7a8c28e02ae8178cb69d7a17 Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Mon, 28 Sep 2009 10:51:50 +1000 Subject: Started drawing snakes. --- images/eyes.png | Bin 0 -> 4037 bytes snake.py | 23 ++++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 images/eyes.png mode change 100644 => 100755 snake.py diff --git a/images/eyes.png b/images/eyes.png new file mode 100644 index 0000000..643158c Binary files /dev/null and b/images/eyes.png differ diff --git a/snake.py b/snake.py old mode 100644 new mode 100755 index 0f7ad5e..f49d831 --- a/snake.py +++ b/snake.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + from __future__ import division import string @@ -68,9 +70,12 @@ class SnakeEngine(object): xscale = self.board_width / self.columns yscale = self.board_height / self.rows - image = Sprites.APPLE - new_size = scale_aspect(image.get_size(), (xscale, yscale)) - self.apple = pygame.transform.smoothscale(image, new_size) + def load_image(image): + new_size = scale_aspect(image.get_size(), (xscale, yscale)) + return pygame.transform.smoothscale(image, new_size) + + self.apple = load_image(Sprites.APPLE) + self.eyes = load_image(Sprites.EYES) def add_bot(self, bot): """ @@ -82,7 +87,6 @@ class SnakeEngine(object): return random.choice('RULD') """ letter = self.letters.pop() - self.bots[letter] = bot for i in xrange(self.rows * self.columns): x, y = self.get_random_position() @@ -91,7 +95,9 @@ class SnakeEngine(object): else: raise KeyError, "Could not insert snake into the board." - self.board[y][x] = letter + colour = (255, 0, 0) + self.bots[letter] = (bot, (x, y), colour) + self.board[y][x] = letter.upper() return letter def remove_bot(self, letter): @@ -118,6 +124,13 @@ class SnakeEngine(object): if cell == Squares.APPLE: self.surface.blit(self.apple, r.topleft) + elif cell.isalpha(): # Snake... + colour = self.bots[cell.lower()][2] + self.surface.fill(colour, r) + + if cell.isupper(): # Snake head + self.surface.blit(self.eyes, r.topleft) + def run(self): # Draw the board. self.draw_board() -- cgit v1.2.3