summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xpygame_snake.py137
-rw-r--r--[-rwxr-xr-x]snake.py124
2 files changed, 138 insertions, 123 deletions
diff --git a/pygame_snake.py b/pygame_snake.py
new file mode 100755
index 0000000..d1639aa
--- /dev/null
+++ b/pygame_snake.py
@@ -0,0 +1,137 @@
+#!/usr/bin/env python
+
+from __future__ import division
+
+import time
+
+import pygame
+pygame.init()
+from pygame.locals import *
+
+from snake import SnakeEngine
+
+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 PygameSnakeEngine(SnakeEngine):
+ EDGE_COLOR = (255, 255, 255)
+ EDGE_WIDTH = 1
+
+ def __init__(self, rows, columns, n_apples, width=800, height=600, fullscreen=False):
+ flags = 0
+ if fullscreen:
+ flags |= pygame.FULLSCREEN
+ self.screen = pygame.display.set_mode((width, height), flags)
+
+ self.width = width
+ self.height = height
+
+ super(PygameSnakeEngine, self).__init__(rows, columns, n_apples)
+
+ def new_game(self, rows, columns, n_apples):
+ super(PygameSnakeEngine, self).new_game(rows, columns, n_apples)
+
+ # make board surface
+ self.board_width, self.board_height = scale_aspect(
+ (columns, rows), (self.width, self.height)
+ )
+ self.surface = pygame.Surface((self.board_width, self.board_height))
+
+ # load sprites
+ xscale = self.board_width / self.columns
+ yscale = self.board_height / self.rows
+
+ 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 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)
+ top = int(y * yscale)
+ 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)
+
+ elif cell.isalpha(): # Snake...
+ colour = self.bots[cell.lower()][1]
+ self.surface.fill(colour, r)
+
+ if cell.isupper(): # Snake head
+ self.surface.blit(self.eyes, r.topleft)
+
+ def run(self):
+ clock = pygame.time.Clock()
+
+ running = True
+ while running and self.bots:
+ for event in pygame.event.get():
+ if event.type == pygame.QUIT or \
+ (event.type == pygame.KEYDOWN and event.key == K_ESCAPE):
+ running = False
+ break
+ if not running: break
+
+ # Clear the screen.
+ self.screen.fill((0, 0, 0))
+ self.surface.fill((0, 0, 0))
+
+ # Draw the board.
+ self.draw_board()
+
+ # Center the board.
+ x = (self.width - self.board_width) / 2
+ y = (self.height - self.board_height) / 2
+ self.screen.blit(self.surface, (x, y))
+
+ # Update the display.
+ pygame.display.flip()
+ clock.tick(20)
+
+ # Let the snakes move!
+ self.update_snakes()
+
+ if running:
+ time.sleep(2)
+
+ # Early window close, late process cleanup.
+ pygame.display.quit()
+
+if __name__ == '__main__':
+ from bots import *
+ from oldbot import BotWrapper
+
+ game = PygameSnakeEngine(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()
+
diff --git a/snake.py b/snake.py
index e61530c..48048d8 100755..100644
--- a/snake.py
+++ b/snake.py
@@ -2,7 +2,6 @@
from __future__ import division
-import time
import string
from random import randint
from collections import deque
@@ -11,36 +10,9 @@ import traceback
from common import *
-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
-
- def __init__(self, rows, columns, n_apples, width=800, height=600, fullscreen=False):
+ def __init__(self, rows, columns, n_apples):
super(SnakeEngine, self).__init__()
- flags = 0
- if fullscreen:
- flags |= pygame.FULLSCREEN
- self.screen = pygame.display.set_mode((width, height), flags)
-
- self.width = width
- self.height = height
self.letters = list(string.lowercase)
self.letters.reverse()
@@ -71,23 +43,6 @@ class SnakeEngine(object):
x, y = self.get_random_position()
self.board[y][x] = Squares.APPLE
- # make board surface
- self.board_width, self.board_height = scale_aspect(
- (columns, rows), (self.width, self.height)
- )
- self.surface = pygame.Surface((self.board_width, self.board_height))
-
- # load sprites
- xscale = self.board_width / self.columns
- yscale = self.board_height / self.rows
-
- 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, colour=None):
"""
A bot is a callable object, with this method signature:
@@ -118,34 +73,6 @@ class SnakeEngine(object):
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)
- top = int(y * yscale)
- 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)
-
- elif cell.isalpha(): # Snake...
- colour = self.bots[cell.lower()][1]
- self.surface.fill(colour, r)
-
- if cell.isupper(): # Snake head
- self.surface.blit(self.eyes, r.topleft)
-
def update_snakes(self, directions_id=id(directions)):
assert id(directions) == directions_id, \
"The common.directions dictionary has been modified since startup..."
@@ -197,52 +124,3 @@ class SnakeEngine(object):
print '-'*60
self.remove_bot(letter)
- def run(self):
- clock = pygame.time.Clock()
-
- running = True
- while running and self.bots:
- for event in pygame.event.get():
- if event.type == pygame.QUIT or \
- (event.type == pygame.KEYDOWN and event.key == K_ESCAPE):
- running = False
- break
- if not running: break
-
- # Clear the screen.
- self.screen.fill((0, 0, 0))
- self.surface.fill((0, 0, 0))
-
- # Draw the board.
- self.draw_board()
-
- # Center the board.
- x = (self.width - self.board_width) / 2
- y = (self.height - self.board_height) / 2
- self.screen.blit(self.surface, (x, y))
-
- # Update the display.
- pygame.display.flip()
- clock.tick(20)
-
- # Let the snakes move!
- self.update_snakes()
-
- if running:
- time.sleep(2)
-
- # Early window close, late process cleanup.
- pygame.display.quit()
-
-if __name__ == '__main__':
- from bots import *
- from oldbot import BotWrapper
-
- game = SnakeEngine(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()
-