diff options
author | Peter Ward <peteraward@gmail.com> | 2009-09-28 10:30:13 +1000 |
---|---|---|
committer | Peter Ward <peteraward@gmail.com> | 2009-09-28 10:30:13 +1000 |
commit | 91da0245b57ebe54f9168fdbc1c97d5981232198 (patch) | |
tree | f09e48c3cb88cbe82367eb48bd1db6ce726d45bc | |
parent | a146b9615e4adea8eabcf14bc61ed8ca45246c12 (diff) |
Added a pretty apple image.
-rw-r--r-- | common.py | 15 | ||||
-rw-r--r-- | images/apple.png | bin | 0 -> 10421 bytes | |||
-rw-r--r-- | snake.py | 89 |
3 files changed, 79 insertions, 25 deletions
@@ -1,4 +1,19 @@ +import os + class Squares(object): EMPTY = '.' APPLE = '*' +class Sprites(object): + PREFIX = 'images' + def __getattribute__(self, name): + try: + return object.__getattribute__(self, name.upper()) + except AttributeError: + from pygame.image import load + filename = os.path.join(self.PREFIX, name.lower() + ".png") + image = load(filename).convert_alpha() + setattr(self, name, image) + return image +Sprites = Sprites() + diff --git a/images/apple.png b/images/apple.png Binary files differnew file mode 100644 index 0000000..69a00ea --- /dev/null +++ b/images/apple.png @@ -1,4 +1,6 @@ from __future__ import division + +import string import random from common import * @@ -7,6 +9,19 @@ import pygame pygame.init() from pygame.locals import * +def scale_aspect((source_width, source_height), (target_width, target_height)): + source_aspect = source_width / source_height + target_aspect = target_width / target_height + if source_aspect > target_aspect: + # restrict width + width = target_width + height = width / source_aspect + else: + # restrict height + height = target_height + width = height * source_aspect + return (width, height) + class SnakeEngine(object): EDGE_COLOR = (255, 255, 255) EDGE_WIDTH = 1 @@ -21,33 +36,43 @@ class SnakeEngine(object): self.width = width self.height = height + self.letters = list(string.lowercase) + self.letters.reverse() + self.bots = {} self.new_game(rows, columns, n_apples) + def get_random_position(self): + x = random.randint(0, self.columns - 1) + y = random.randint(0, self.rows - 1) + return (x, y) + def new_game(self, rows, columns, n_apples): + self.rows = rows + self.columns = columns + # make board 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) + x, y = self.get_random_position() self.board[y][x] = Squares.APPLE # make board surface - board_aspect = columns / rows - screen_aspect = self.width / self.height - if board_aspect > screen_aspect: - # restrict width - self.board_width = self.width - self.board_height = self.board_width / board_aspect - else: - # restrict height - self.board_height = self.height - self.board_width = self.board_height * board_aspect - + self.board_width, self.board_height = scale_aspect( + (columns, rows), (self.width, self.height) + ) self.surface = pygame.Surface((self.board_width, self.board_height)) - def add_bot(self, name, bot): + # load sprites + 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 add_bot(self, bot): """ A bot is a callable object, with this method signature: def bot_callable( @@ -56,19 +81,27 @@ class SnakeEngine(object): ): return random.choice('RULD') """ - self.bots[name] = bot + letter = self.letters.pop() + self.bots[letter] = bot - def remove_bot(self, name): - del self.bots[name] + for i in xrange(self.rows * self.columns): + x, y = self.get_random_position() + if self.board[y][x] == Squares.EMPTY: + break + else: + raise KeyError, "Could not insert snake into the board." - def draw_board(self): - rows = len(self.board) - assert rows > 0 - columns = len(self.board[0]) + self.board[y][x] = letter + return letter - xscale = self.board_width / columns - yscale = self.board_height / rows + def remove_bot(self, letter): + del self.bots[letter] + def draw_board(self): + xscale = self.board_width / self.columns + yscale = self.board_height / self.rows + + # Draw grid. for y, row in enumerate(self.board): for x, cell in enumerate(row): left = int(x * xscale) @@ -76,11 +109,17 @@ class SnakeEngine(object): w = int((x + 1) * xscale) - left h = int((y + 1) * yscale) - top r = Rect(left, top, w, h) + + # Draw a square. pygame.draw.rect(self.surface, self.EDGE_COLOR, r, self.EDGE_WIDTH) + # Draw the things on the square. + if cell == Squares.APPLE: + self.surface.blit(self.apple, r.topleft) + def run(self): - # Draw the grid. + # Draw the board. self.draw_board() # Center the board. @@ -95,6 +134,6 @@ if __name__ == '__main__': from bots import random_bot game = SnakeEngine(8, 16, 10) - game.add_bot('Bob', random_bot) + game.add_bot(random_bot) game.run() |