changeset 182:0a955d2536f0

Painted Jezebel! (Assuming you name your snake Jezebel.)
author Jeremy Thurgood <firxen@gmail.com>
date Wed, 14 Sep 2011 16:27:11 +0200
parents 061d711ba570
children 045f6b4301eb
files data/levels/dev.txt mamba/level.py mamba/mutators.py mamba/snake.py mamba/sprites.py mamba/world.py
diffstat 6 files changed, 59 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/data/levels/dev.txt	Wed Sep 14 15:27:27 2011 +0200
+++ b/data/levels/dev.txt	Wed Sep 14 16:27:11 2011 +0200
@@ -11,9 +11,9 @@
 X......................................X
 X.................m....................X
 X...................M..................X
-X......................................X
+X.........r....R.......................X
 X.................f....................X
-X......................................X
+X...........b.....B....................X
 X...........~~~~~~.....................X
 X..........~~~~~~~~....................X
 X..........~~....~~....................X
--- a/mamba/level.py	Wed Sep 14 15:27:27 2011 +0200
+++ b/mamba/level.py	Wed Sep 14 16:27:11 2011 +0200
@@ -30,6 +30,8 @@
     '>': mktile(sprites.EntrySprite, direction=Snake.RIGHT),
     'E': mktile(sprites.ExitSprite),
     '~': mktile(sprites.PuddleSprite),
+    'r': mktile(sprites.Painter, colour='red'),
+    'b': mktile(sprites.Painter, colour='blue'),
     }
 
 THING_MAP = {
--- a/mamba/mutators.py	Wed Sep 14 15:27:27 2011 +0200
+++ b/mamba/mutators.py	Wed Sep 14 16:27:11 2011 +0200
@@ -19,7 +19,7 @@
         return hash((id(self._func), self._args))
 
     def __eq__(self, other):
-        return (self._func is other.func) and self._args == other._args
+        return (self._func is other._func) and self._args == other._args
 
     def __repr__(self):
         return "<%s args=%r>" % (self.__class__.__name__, self._args)
--- a/mamba/snake.py	Wed Sep 14 15:27:27 2011 +0200
+++ b/mamba/snake.py	Wed Sep 14 16:27:11 2011 +0200
@@ -44,12 +44,13 @@
             shifted, ds = self.head.shift_head(ds)
             if not shifted:
                 break
-            world.interact(self.head.tile_pos)
+            world.interact(self.head)
             self.head.set_orientation(self.orientation)
             for segment in self.segments[1:]:
                 old_tile_state = segment.get_tile_state()
                 segment.shift_tile(tile_state)
                 tile_state = old_tile_state
+                world.interact(segment)
 
         for segment in self.segments:
             segment.shift_pixels(ds)
@@ -76,6 +77,8 @@
 
     _detail_mutators = ()
 
+    is_head = False
+
     def __init__(self, image_name, tile_pos):
         super(Segment, self).__init__()
         self.set_base_image(image_name)
@@ -138,6 +141,8 @@
     EYE = mutators.Overlay("tiles/common/snake/snake-head-eye-r.png")
     TONGUE = mutators.Overlay("tiles/common/snake/snake-head-tongue-r.png")
 
+    is_head = True
+
     def __init__(self, tile_pos):
         self._detail_mutators = (self.EYE,)
         super(Head, self).__init__(self.CLOSED, tile_pos)
--- a/mamba/sprites.py	Wed Sep 14 15:27:27 2011 +0200
+++ b/mamba/sprites.py	Wed Sep 14 16:27:11 2011 +0200
@@ -5,6 +5,19 @@
 from mamba import mutators
 
 
+COLOURS = {
+    'red': mutators.RED,
+    'blue': mutators.BLUE,
+    'yellow': mutators.YELLOW,
+    }
+
+
+def colour_with_fittings(image_name, tileset, colour):
+    fittings_mutator = mutators.Overlay(
+            "tiles/%s/%s-fittings.png" % (tileset, image_name))
+    return image_name, (COLOURS[colour], fittings_mutator)
+
+
 def tile_sizify(pos):
     ts_x, ts_y = TILE_SIZE
     p_x, p_y = pos
@@ -72,14 +85,9 @@
         }
 
 
-class InvariantSpriteImageVariants(SpriteImageVariants):
-    def __call__(self, top, bottom, left, right):
-        return (self.base_image_name, ())
-
-
 class BaseSprite(Sprite):
     tileset = 'common'
-    variants_class = InvariantSpriteImageVariants
+    variants_class = None
     variants = None
     name = None
     alive = True
@@ -109,7 +117,8 @@
         return self.variants(top, bottom, left, right)
 
     def use_variant(self, *args):
-        self.image = self.load_image(*self.get_variant(*args))
+        if self.variants_class is not None:
+            self.image = self.load_image(*self.get_variant(*args))
 
 
 class TileSprite(BaseSprite):
@@ -118,11 +127,11 @@
         self.tile_char = tile_char
         self.solid = solid
 
-    def get_solid(self, snake):
+    def get_solid(self, snake, segment):
         return self.solid
 
-    def interact(self, snake):
-        if self.get_solid(snake):
+    def interact(self, snake, segment):
+        if self.get_solid(snake, segment):
             snake.crash()
 
 
@@ -140,12 +149,12 @@
 
     def __init__(self, colour, **kw):
         self.colour = colour
-        image_name = 'door_%s' % (colour,)
-        kw.setdefault('image_name', image_name)
         super(DoorSprite, self).__init__(**kw)
+        self.image = self.load_image(
+            *colour_with_fittings("door", self.tileset, colour))
 
-    def get_solid(self, snake_mutations):
-        return self.colour in self.snake_mutations
+    def get_solid(self, snake, segment):
+        return COLOURS[self.colour] != segment._colour_overlay
 
 
 class EntrySprite(SingleImageTileSprite):
@@ -171,7 +180,7 @@
         super(PuddleSprite, self).__init__(**kw)
         self.image = self.load_image(*self.get_variant(*variant))
 
-    def get_solid(self, snake):
+    def get_solid(self, snake, segment):
         if snake.can_swim():
             print "Splishy!"
             return False
@@ -180,31 +189,45 @@
 
 
 class EdibleTile(SingleImageTileSprite):
-    def eat(self):
+    def interact(self, snake, segment):
+        if not segment.is_head:
+            return
+        self.eat(snake)
         self.alive = False
         self.kill()
 
+    def eat(self, snake):
+        print "I'm delicious!"
+
 
 class BigMouse(EdibleTile):
     image_name = "rat-big"
 
-    def interact(self, snake):
+    def eat(self, snake):
         print "Embiggen!"
-        self.eat()
 
 
 class SmallMouse(EdibleTile):
     image_name = "rat-small"
 
-    def interact(self, snake):
+    def eat(self, snake):
         print "Ensmallen!"
-        self.eat()
 
 
 class Frog(EdibleTile):
     image_name = "frog"
 
-    def interact(self, snake):
+    def eat(self, snake):
         print "Swimmery!"
         snake.mutate('amphibious')
-        self.eat()
+
+
+class Painter(TileSprite):
+    def __init__(self, colour, **kw):
+        super(Painter, self).__init__(**kw)
+        self.colour = colour
+        self.image = self.load_image(
+            *colour_with_fittings("spray", self.tileset, colour))
+
+    def interact(self, snake, segment):
+        segment.set_colour(COLOURS[self.colour])
--- a/mamba/world.py	Wed Sep 14 15:27:27 2011 +0200
+++ b/mamba/world.py	Wed Sep 14 16:27:11 2011 +0200
@@ -28,8 +28,7 @@
 
         self.snake.update(dt, self)
 
-    def interact(self, tile_pos):
-        tile = self.level.get_tile(tile_pos)
+    def interact(self, segment):
+        tile = self.level.get_tile(segment.tile_pos)
         if tile is not None and tile.alive:
-            print "Interact:", tile_pos, tile
-            tile.interact(self.snake)
+            tile.interact(self.snake, segment)