From b981ab7e032af8764b4ad2d8c6678dccc87ea0e4 Mon Sep 17 00:00:00 2001 From: Peter Ward Date: Thu, 19 Jul 2012 20:12:30 +1000 Subject: Make engine subpackage. --- snakegame/console_snake.py | 73 -------------------- snakegame/engines/__init__.py | 0 snakegame/engines/curses.py | 73 ++++++++++++++++++++ snakegame/engines/pygame.py | 155 ++++++++++++++++++++++++++++++++++++++++++ snakegame/engines/pyglet.py | 118 ++++++++++++++++++++++++++++++++ snakegame/pygame_snake.py | 155 ------------------------------------------ snakegame/pyglet_snake.py | 118 -------------------------------- 7 files changed, 346 insertions(+), 346 deletions(-) delete mode 100755 snakegame/console_snake.py create mode 100644 snakegame/engines/__init__.py create mode 100644 snakegame/engines/curses.py create mode 100644 snakegame/engines/pygame.py create mode 100644 snakegame/engines/pyglet.py delete mode 100755 snakegame/pygame_snake.py delete mode 100755 snakegame/pyglet_snake.py diff --git a/snakegame/console_snake.py b/snakegame/console_snake.py deleted file mode 100755 index 1db5e38..0000000 --- a/snakegame/console_snake.py +++ /dev/null @@ -1,73 +0,0 @@ -#!/usr/bin/env python - -from __future__ import division - -import time - -import curses - -from common import * -from snake import SnakeEngine - -class ConsoleSnakeEngine(SnakeEngine): - def new_game(self, *args): - super(ConsoleSnakeEngine, self).new_game(*args) - - self.window = curses.initscr() - curses.start_color() - - curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) - curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK) - - self.EMPTY_COLOUR = curses.color_pair(0) - self.APPLE_COLOUR = curses.color_pair(1) - self.SNAKE_COLOUR = curses.color_pair(4) - - def draw_board(self): - # Draw grid. - for y, row in enumerate(self.board): - for x, cell in enumerate(row): - char = '.' - colour = self.EMPTY_COLOUR - - # Draw the things on the square. - if cell == Squares.APPLE: - char = '@' - colour = self.APPLE_COLOUR - - elif cell.isalpha(): # Snake... -# colour = self.bots[cell.lower()][1] - char = cell - colour = self.SNAKE_COLOUR - - self.window.addstr(y, x, char, colour) - - def run(self): - while self.bots: - # Clear the screen. - self.window.erase() - - # Draw the board. - self.draw_board() - - # Update the display. - self.window.refresh() - time.sleep(0.025) - - # Let the snakes move! - self.update_snakes() - -def main(*args): - import sys - from processbot import BotWrapper - - rows, columns, apples = map(int, sys.argv[1:4]) - game = ConsoleSnakeEngine(rows, columns, apples) - for filename in sys.argv[4:]: - bot = BotWrapper(filename) - game.add_bot(bot) - game.run() - -if __name__ == '__main__': - curses.wrapper(main) - diff --git a/snakegame/engines/__init__.py b/snakegame/engines/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/snakegame/engines/curses.py b/snakegame/engines/curses.py new file mode 100644 index 0000000..1db5e38 --- /dev/null +++ b/snakegame/engines/curses.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python + +from __future__ import division + +import time + +import curses + +from common import * +from snake import SnakeEngine + +class ConsoleSnakeEngine(SnakeEngine): + def new_game(self, *args): + super(ConsoleSnakeEngine, self).new_game(*args) + + self.window = curses.initscr() + curses.start_color() + + curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK) + curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK) + + self.EMPTY_COLOUR = curses.color_pair(0) + self.APPLE_COLOUR = curses.color_pair(1) + self.SNAKE_COLOUR = curses.color_pair(4) + + def draw_board(self): + # Draw grid. + for y, row in enumerate(self.board): + for x, cell in enumerate(row): + char = '.' + colour = self.EMPTY_COLOUR + + # Draw the things on the square. + if cell == Squares.APPLE: + char = '@' + colour = self.APPLE_COLOUR + + elif cell.isalpha(): # Snake... +# colour = self.bots[cell.lower()][1] + char = cell + colour = self.SNAKE_COLOUR + + self.window.addstr(y, x, char, colour) + + def run(self): + while self.bots: + # Clear the screen. + self.window.erase() + + # Draw the board. + self.draw_board() + + # Update the display. + self.window.refresh() + time.sleep(0.025) + + # Let the snakes move! + self.update_snakes() + +def main(*args): + import sys + from processbot import BotWrapper + + rows, columns, apples = map(int, sys.argv[1:4]) + game = ConsoleSnakeEngine(rows, columns, apples) + for filename in sys.argv[4:]: + bot = BotWrapper(filename) + game.add_bot(bot) + game.run() + +if __name__ == '__main__': + curses.wrapper(main) + diff --git a/snakegame/engines/pygame.py b/snakegame/engines/pygame.py new file mode 100644 index 0000000..cf07297 --- /dev/null +++ b/snakegame/engines/pygame.py @@ -0,0 +1,155 @@ +#!/usr/bin/env python + +from __future__ import division + +import os +import time + +import pygame +pygame.init() +from pygame.locals import * + +from common import * +from snake import SnakeEngine + +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() + +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, + **kwargs): + 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, + **kwargs) + + 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) + +if __name__ == '__main__': + import sys + from processbot import BotWrapper + + rows, columns, apples = map(int, sys.argv[1:4]) + game = PygameSnakeEngine(rows, columns, apples) + for filename in sys.argv[4:]: + bot = BotWrapper(filename) + game.add_bot(bot) + game.run() + + # Early window close, late process cleanup. + pygame.display.quit() + + diff --git a/snakegame/engines/pyglet.py b/snakegame/engines/pyglet.py new file mode 100644 index 0000000..5a17ac0 --- /dev/null +++ b/snakegame/engines/pyglet.py @@ -0,0 +1,118 @@ +#!/usr/bin/env python + +from __future__ import division + +import pyglet +pyglet.resource.path = ['images'] +pyglet.resource.reindex() + +from pyglet import gl + +import common +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 PygletSnakeEngine(SnakeEngine, pyglet.window.Window): + EDGE_COLOR = (255, 255, 255, 255) + EDGE_WIDTH = 2 + + def __init__(self, rows, columns, n_apples, *args, **kwargs): + super(PygletSnakeEngine, self).__init__(rows, columns, n_apples, *args, **kwargs) + + gl.glEnable(gl.GL_BLEND) + gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) + + pyglet.clock.schedule_interval(lambda t: self.update_snakes(), 0.025) + + def new_game(self, rows, columns, n_apples): + super(PygletSnakeEngine, self).new_game(rows, columns, n_apples) + + # make board surface + self.board_width, self.board_height = scale_aspect( + (columns, rows), (self.width, self.height) + ) + + # load sprites + xscale = self.board_width / self.columns + yscale = self.board_height / self.rows + + self.apple = pyglet.resource.image('apple.png') + self.apple.size = scale_aspect( + (self.apple.width, self.apple.height), + (xscale, yscale) + ) + self.eyes = pyglet.resource.image('eyes.png') + self.eyes.size = scale_aspect( + (self.eyes.width, self.eyes.height), + (xscale, yscale) + ) + + def on_draw(self): + self.clear() + + 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 = self.height - int(y * yscale) + right = int((x + 1) * xscale) + bottom = self.height - int((y + 1) * yscale) + r = (left, top, right, top, right, bottom, left, bottom) + + # Draw a square. + gl.glLineWidth(self.EDGE_WIDTH) + pyglet.graphics.draw(4, gl.GL_LINE_LOOP, + ('v2f', r), + ('c4B', self.EDGE_COLOR * 4)) + + # Draw the things on the square. + if cell == common.Squares.APPLE: + w, h = self.apple.size + self.apple.blit(left + (xscale - w) / 2.0, top - h, width=w, height=h) + + elif cell.isalpha(): # Snake... + colour = self.bots[cell.lower()][1] + (255,) + gl.glPolygonMode(gl.GL_FRONT, gl.GL_FILL) + pyglet.graphics.draw(4, gl.GL_POLYGON, + ('v2f', r), + ('c4B', colour * 4), + ) + + if cell.isupper(): # Snake head + w, h = self.eyes.size + self.eyes.blit(left, top - h, width=w, height=h) + + def update_snakes(self, *args): + if not self.bots: + pyglet.app.exit() + super(PygletSnakeEngine, self).update_snakes(*args) + + def run(self): + pyglet.app.run() + +if __name__ == '__main__': + import sys + from processbot import BotWrapper + + rows, columns, apples = map(int, sys.argv[1:4]) + game = PygletSnakeEngine(rows, columns, apples) + for filename in sys.argv[4:]: + bot = BotWrapper(filename) + game.add_bot(bot) + game.run() + diff --git a/snakegame/pygame_snake.py b/snakegame/pygame_snake.py deleted file mode 100755 index cf07297..0000000 --- a/snakegame/pygame_snake.py +++ /dev/null @@ -1,155 +0,0 @@ -#!/usr/bin/env python - -from __future__ import division - -import os -import time - -import pygame -pygame.init() -from pygame.locals import * - -from common import * -from snake import SnakeEngine - -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() - -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, - **kwargs): - 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, - **kwargs) - - 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) - -if __name__ == '__main__': - import sys - from processbot import BotWrapper - - rows, columns, apples = map(int, sys.argv[1:4]) - game = PygameSnakeEngine(rows, columns, apples) - for filename in sys.argv[4:]: - bot = BotWrapper(filename) - game.add_bot(bot) - game.run() - - # Early window close, late process cleanup. - pygame.display.quit() - - diff --git a/snakegame/pyglet_snake.py b/snakegame/pyglet_snake.py deleted file mode 100755 index 5a17ac0..0000000 --- a/snakegame/pyglet_snake.py +++ /dev/null @@ -1,118 +0,0 @@ -#!/usr/bin/env python - -from __future__ import division - -import pyglet -pyglet.resource.path = ['images'] -pyglet.resource.reindex() - -from pyglet import gl - -import common -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 PygletSnakeEngine(SnakeEngine, pyglet.window.Window): - EDGE_COLOR = (255, 255, 255, 255) - EDGE_WIDTH = 2 - - def __init__(self, rows, columns, n_apples, *args, **kwargs): - super(PygletSnakeEngine, self).__init__(rows, columns, n_apples, *args, **kwargs) - - gl.glEnable(gl.GL_BLEND) - gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA) - - pyglet.clock.schedule_interval(lambda t: self.update_snakes(), 0.025) - - def new_game(self, rows, columns, n_apples): - super(PygletSnakeEngine, self).new_game(rows, columns, n_apples) - - # make board surface - self.board_width, self.board_height = scale_aspect( - (columns, rows), (self.width, self.height) - ) - - # load sprites - xscale = self.board_width / self.columns - yscale = self.board_height / self.rows - - self.apple = pyglet.resource.image('apple.png') - self.apple.size = scale_aspect( - (self.apple.width, self.apple.height), - (xscale, yscale) - ) - self.eyes = pyglet.resource.image('eyes.png') - self.eyes.size = scale_aspect( - (self.eyes.width, self.eyes.height), - (xscale, yscale) - ) - - def on_draw(self): - self.clear() - - 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 = self.height - int(y * yscale) - right = int((x + 1) * xscale) - bottom = self.height - int((y + 1) * yscale) - r = (left, top, right, top, right, bottom, left, bottom) - - # Draw a square. - gl.glLineWidth(self.EDGE_WIDTH) - pyglet.graphics.draw(4, gl.GL_LINE_LOOP, - ('v2f', r), - ('c4B', self.EDGE_COLOR * 4)) - - # Draw the things on the square. - if cell == common.Squares.APPLE: - w, h = self.apple.size - self.apple.blit(left + (xscale - w) / 2.0, top - h, width=w, height=h) - - elif cell.isalpha(): # Snake... - colour = self.bots[cell.lower()][1] + (255,) - gl.glPolygonMode(gl.GL_FRONT, gl.GL_FILL) - pyglet.graphics.draw(4, gl.GL_POLYGON, - ('v2f', r), - ('c4B', colour * 4), - ) - - if cell.isupper(): # Snake head - w, h = self.eyes.size - self.eyes.blit(left, top - h, width=w, height=h) - - def update_snakes(self, *args): - if not self.bots: - pyglet.app.exit() - super(PygletSnakeEngine, self).update_snakes(*args) - - def run(self): - pyglet.app.run() - -if __name__ == '__main__': - import sys - from processbot import BotWrapper - - rows, columns, apples = map(int, sys.argv[1:4]) - game = PygletSnakeEngine(rows, columns, apples) - for filename in sys.argv[4:]: - bot = BotWrapper(filename) - game.add_bot(bot) - game.run() - -- cgit v1.2.3