From 9050f0c0648c3ef2aa74edbf14504231ee0f8725 Mon Sep 17 00:00:00 2001 From: matts1 Date: Sun, 24 Mar 2013 19:32:08 +1100 Subject: added in ice creams and shrinking potions into common.py, engine.py, and the viewers for both pyglet and pygame --- .hgignore | 4 ++++ snakegame/common.py | 7 ++++-- snakegame/engine.py | 48 ++++++++++++++++++++++++++++---------- snakegame/images/icecream.png | Bin 0 -> 6346 bytes snakegame/images/shrinkpotion.png | Bin 0 -> 42082 bytes snakegame/viewers/pygame.py | 12 +++++++--- snakegame/viewers/pyglet.py | 27 ++++++++++++++------- 7 files changed, 72 insertions(+), 26 deletions(-) create mode 100644 snakegame/images/icecream.png create mode 100644 snakegame/images/shrinkpotion.png diff --git a/.hgignore b/.hgignore index 2bece3c..40ba352 100644 --- a/.hgignore +++ b/.hgignore @@ -4,3 +4,7 @@ syntax: glob docs/build docs/tutorial.pdf + +pyglet +old_matt.py +matt_bot.py diff --git a/snakegame/common.py b/snakegame/common.py index 92d0e62..9feb395 100644 --- a/snakegame/common.py +++ b/snakegame/common.py @@ -12,16 +12,19 @@ directions = { EMPTY = '.' APPLE = '*' WALL = '#' +ICE_CREAM = '+' +SHRINK_POTION = '-' +TELEPORTER = '?' is_empty = EMPTY.__eq__ is_apple = APPLE.__eq__ is_wall = WALL.__eq__ def is_vacant(cell): - return cell in (EMPTY, APPLE) + return cell in (EMPTY, APPLE, ICE_CREAM, SHRINK_POTION, TELEPORTER) def is_blocking(cell): - return cell not in (EMPTY, APPLE) + return not is_vacant(cell) def is_snake(cell): return cell in alphabet diff --git a/snakegame/engine.py b/snakegame/engine.py index ba6c24c..e4e6799 100644 --- a/snakegame/engine.py +++ b/snakegame/engine.py @@ -15,7 +15,7 @@ HARD_TIME_LIMIT = 1.0 class Engine(object): def __init__( self, - rows, columns, n_apples, + rows, columns, n_apples, n_ice_creams=0, n_shrink_potions=0, wrap=True, random=None, *args, **kwargs @@ -29,7 +29,7 @@ class Engine(object): self.wrap = wrap self.bots = {} - self.new_game(rows, columns, n_apples) + self.new_game(rows, columns, n_apples, n_ice_creams, n_shrink_potions) def get_random_position(self): x = self.random.randrange(0, self.columns) @@ -43,7 +43,12 @@ class Engine(object): self.board[y][x] = new return x, y - def new_game(self, rows, columns, n_apples): + def add_items(self, item, amount): + for i in xrange(amount): + x, y = self.get_random_position() + self.board[y][x] = item + + def new_game(self, rows, columns, n_apples, n_ice_creams, n_shrink_potions): self.game_ticks = 0 self.game_id = self.random.randint(0, sys.maxint) @@ -57,9 +62,9 @@ class Engine(object): # make board self.board = [[common.EMPTY for x in xrange(columns)] for y in xrange(rows)] - for i in xrange(n_apples): - x, y = self.get_random_position() - self.board[y][x] = common.APPLE + self.add_items(common.APPLE, n_apples) + self.add_items(common.ICE_CREAM, n_ice_creams) + self.add_items(common.SHRINK_POTION, n_shrink_potions) def add_bot(self, bot, team=None, colour=None): """ @@ -83,7 +88,7 @@ class Engine(object): if position is None: raise KeyError, "Could not insert snake into the board." - self.bots[letter] = [bot, colour, deque([position]), team] + self.bots[letter] = [bot, colour, deque([position]), team, 0] return letter def remove_bot(self, letter): @@ -99,7 +104,7 @@ class Engine(object): def update_snakes(self): self.game_ticks += 1 - for letter, (bot, colour, path, team) in self.bots.items(): + for letter, (bot, colour, path, team, length_delta) in self.bots.items(): board = deepcopy(self.board) try: x, y = path[-1] @@ -153,13 +158,32 @@ class Engine(object): # Make old head into body. self.board[y][x] = letter.lower() + # Since they added 1 to their head, we take away 1 (then do bonuses) + length_delta -= 1 + if oldcell == common.APPLE: - # Add in an apple to compensate. + length_delta += 1 self.replace_random(common.EMPTY, common.APPLE) + elif oldcell == common.ICE_CREAM: + length_delta += 3 + self.replace_random(common.EMPTY, common.ICE_CREAM) + elif oldcell == common.SHRINK_POTION: + length_delta -= 1 + self.replace_random(common.EMPTY, common.SHRINK_POTION) + + if length_delta > 0: + length_delta -= 1 else: - # Remove last part of snake. - ox, oy = path.popleft() - self.board[oy][ox] = common.EMPTY + while length_delta < 0: + # Remove last part of snake. + length_delta += 1 + if len(path) > 1: + ox, oy = path.popleft() + self.board[oy][ox] = common.EMPTY + + # Need to put length delta back in list + self.bots[letter][4] = length_delta + else: self.remove_bot(letter) diff --git a/snakegame/images/icecream.png b/snakegame/images/icecream.png new file mode 100644 index 0000000..7313223 Binary files /dev/null and b/snakegame/images/icecream.png differ diff --git a/snakegame/images/shrinkpotion.png b/snakegame/images/shrinkpotion.png new file mode 100644 index 0000000..376a994 Binary files /dev/null and b/snakegame/images/shrinkpotion.png differ diff --git a/snakegame/viewers/pygame.py b/snakegame/viewers/pygame.py index 8b8fca4..3f32da1 100644 --- a/snakegame/viewers/pygame.py +++ b/snakegame/viewers/pygame.py @@ -59,7 +59,13 @@ class Viewer(object): xscale = self.board_width / self.columns yscale = self.board_height / self.rows - self.apple = load_image('images/apple.png', xscale, yscale) + self.items = { + common.APPLE : 'images/apple.png', + common.ICE_CREAM : 'images/icecream.png', + common.SHRINK_POTION : 'images/shrinkpotion.png' + } + for item in self.items: + self.items[item] = load_image(self.items[item], xscale, yscale) self.eyes = load_image('images/eyes.png', xscale, yscale) def draw_board(self, board): @@ -80,8 +86,8 @@ class Viewer(object): self.EDGE_WIDTH) # Draw the things on the square. - if cell == common.APPLE: - self.surface.blit(self.apple, r.topleft) + if cell in self.items: + self.surface.blit(self.items[cell], r.topleft) elif common.is_snake(cell): bot = self.engine.bots[cell.lower()] diff --git a/snakegame/viewers/pyglet.py b/snakegame/viewers/pyglet.py index 9b7a023..f1a5f88 100644 --- a/snakegame/viewers/pyglet.py +++ b/snakegame/viewers/pyglet.py @@ -59,11 +59,20 @@ class Viewer(pyglet.window.Window): xscale = float(self.board_width) / self.columns yscale = float(self.board_height) / self.rows - self.apple = pyglet.resource.image('images/apple.png') - self.apple.size = scale_aspect( - (self.apple.width, self.apple.height), - (xscale, yscale) - ) + self.images = { + common.APPLE : 'images/apple.png', + common.ICE_CREAM : 'images/icecream.png', + common.SHRINK_POTION : 'images/shrinkpotion.png' + } + + for item, location in self.images.items(): + image = pyglet.resource.image(location) + image.size = scale_aspect( + (image.width, image.height), + (xscale, yscale) + ) + self.images[item] = image + self.eyes = pyglet.resource.image('images/eyes.png') self.eyes.size = scale_aspect( (self.eyes.width, self.eyes.height), @@ -95,9 +104,10 @@ class Viewer(pyglet.window.Window): ('c4B', self.EDGE_COLOR * 4)) # Draw the things on the square. - if cell == common.APPLE: - w, h = self.apple.size - self.apple.blit(left + (xscale - w) / 2.0, top - h, width=w, height=h) + if cell in self.images: + image = self.images[cell] + w, h = image.size + image.blit(left + (xscale - w) / 2.0, top - h, width=w, height=h) elif common.is_snake(cell): bot = self.engine.bots[cell.lower()] @@ -114,4 +124,3 @@ class Viewer(pyglet.window.Window): def run(self): pyglet.app.run() - -- cgit v1.2.3