Commits

illume committed bb24fba

moved the sprites around to different layers

Comments (0)

Files changed (2)

 DONE
 ====
 
+* generic load_image
 * hero
 * duck flying across
 * floor moving

outofdesert/main.py

 from pygame.locals import *
 from skelutil import data_dir
 
-WIDTH, HEIGHT = 640,480
+# in case we need to port to dos later, we keep the resolution low.
+WIDTH, HEIGHT = 320, 200
 
 
+#NOTE: source code ordered by where it appears on the screen by height.  
+#       ie, floor is at the bottom.
+
 def load_image(name, colorkey=None):
+    '''load_image('duck.png') will load the duck portable network graphics file 
+          from the place where the images are.
+    '''
     fullname = data_dir(name)
     try:
         image = pygame.image.load(fullname)
     except pygame.error:
         print ('Cannot load image:', fullname)
         raise SystemExit(str(geterror()))
-    #image = image.convert()
+    image = image.convert()
+    #image = image.convert_alpha()
     if colorkey is not None:
         if colorkey is -1:
             colorkey = image.get_at((0,0))
     return image
 
 
+# these are awesome sprite classes.
+class Sprite(pygame.sprite.DirtySprite):
+    pass
+class Group(pygame.sprite.LayeredDirty):
+    pass
 
-class Sprite(pygame.sprite.DirtySprite):
-    def start(self):
-        self.image = None
-        self.rect = None
-
-    def update(self, *args):
-        pass
-    #def draw(self):
-    #    return []
-
-class Group(pygame.sprite.LayeredDirty):
-    def start(self):
-        pass
-
-    #def update(self, *args):
-    #    pass
-
-    #def draw(self):
-    #    return []
-
-
+# Our Game is a group containing everything.  So there.
 class Game(Group):
     def start(self):
         # when it gets to 1.0 the game is complete, 0.0 fail.
 
 
         self.floor = Floor().start()
-        self.floor.add(self)
+        self.add(self.floor, layer = 2)
 
         self.duck= Duck().start()
-        self.duck.add(self)
+        self.add(self.duck, layer = 20)
         self.duck.fly()
 
         self.hero = Hero().start()
-        self.hero.add(self)
+        self.add(self.hero, layer = 10)
+
+        self.baddy = Baddy().start()
+        self.add(self.baddy, layer = 15)
 
 
         return self
                     self.hero.stand()
 
 
-
-class Hero(Sprite):
-
-    def start(self):
-        self.images = {}
-        self.images['stand'] = load_image("hero.png")
-        self.images['duck'] = load_image("hero-duck.png")
-        self.image = self.images['stand']
-        self.rect = Rect(int(WIDTH / 2), HEIGHT - (40 + self.image.get_height()), 64, 128)
-        self.dy = 0
-        self.count = 0
-        self.dirty = 1
-        return self
-
-    def duck(self):
-        self.image = self.images['duck']
-        self.dirty = 1
-        
-    def stand(self):
-        self.image = self.images['stand']
-        self.dirty = 1
-
-    def stop(self):
-        self.dx = 0
-        self.dirty = 1
-    def update(self, *args):
-        self.count += 1
-        #self.rect.x += self.dx
-        #self.dirty = 1
-
-
-
-
-
-class Baddy(Sprite):
-    def start(self):
-        pass
-
 class Indicator(Sprite):
     def start(self):
         # when it gets to 1.0 the game is complete, 0.0 fail.
         self.dirty = 1
 
 
+# Duck typing.
+class Duck(Sprite):
+
+    def start(self):
+        self.rect = Rect(WIDTH - 200, HEIGHT - 180, 32, 32)
+        self.rect = Rect(WIDTH +2, HEIGHT - 180, 32, 32)
+        self.image = load_image(data_dir("duck.png"))
+        self.dy = 0
+        self.count = 0
+        return self
+
+    def fly(self):
+        self.dx = -1
+    def stop(self):
+        self.dx = 0
+    def update(self, *args):
+        self.count += 1
+        #why is a duck?
+        self.rect.x += self.dx
+        self.dirty = 1
+
+
+
+
+
+class Hero(Sprite):
+
+    def start(self):
+        self.images = {}
+        self.images['stand'] = load_image("hero.png")
+        self.images['duck'] = load_image("hero-duck.png")
+        self.image = self.images['stand']
+        self.rect = Rect(int(WIDTH / 2), 
+                        HEIGHT - (40 + self.image.get_height()), 
+                        64, 128)
+        self.dy = 0
+        self.dx = 0
+        self.count = 0
+        self.dirty = 1
+        return self
+
+    def duck(self):
+        self.image = self.images['duck']
+        self.dirty = 1
+        
+    def stand(self):
+        self.image = self.images['stand']
+        self.dirty = 1
+
+    def stop(self):
+        self.dx = 0
+        self.dirty = 1
+    def update(self, *args):
+        self.count += 1
+        #self.rect.x += self.dx
+        #self.dirty = 1
+
+
+
+
+
+class Baddy(Sprite):
+
+    def start(self):
+        self.rect = Rect(WIDTH - 10, HEIGHT - (40 + 128), 64, 128)
+        self.image = pygame.Surface(self.rect.size)
+        self.image.fill((255,255,255))
+        #self.image.fill(pygame.Color("pink"))
+        self.color = pygame.Color("black")
+
+        self.dx = -1
+        self.dy = 0
+        self.count = 0
+        return self
+
+    def update(self, *args):
+        self.count += 1
+        self.rect.x += self.dx
+        triangles = [
+            ((13, 57), (28, 12), (50,57)), # head
+            ((11, 112), (30, 58), (49, 109)), # body
+            ((13, 127), (20, 112), (26, 127)), # left foot
+            ((32, 127), (35, 111), (45, 127)), # right foot
+        ]
+        for t in triangles:
+            pygame.draw.polygon(self.image, self.color, t)
+
+
+        self.dirty = 1
+
+
+
+
+
+
 
 class Floor(Sprite):
     def start(self):
 
 
 
-
-class Duck(Sprite):
-
-    def start(self):
-        self.rect = Rect(WIDTH + 32, HEIGHT - 200, 32, 32)
-        self.rect = Rect(WIDTH + 2, HEIGHT - 200, 32, 32)
-        #self.image = load_image(data_dir("duck.png"), (255,255,255))
-        self.image = load_image(data_dir("duck.png"))
-        self.dy = 0
-        self.count = 0
-        return self
-
-    def fly(self):
-        self.dx = -1
-    def stop(self):
-        self.dx = 0
-    def update(self, *args):
-        self.count += 1
-        #where is duck?
-        self.rect.x += self.dx
-        self.dirty = 1
-
-
-
 def main():
     """ your app starts here
     """
-    SCREEN_SIZE = 640,480
+    SCREEN_SIZE = WIDTH, HEIGHT
     pygame.display.init()
     screen = pygame.display.set_mode(SCREEN_SIZE)
-    pygame.display.set_caption("Out of the Desert")
+    pygame.display.set_caption("Out of the Desert.   K|W|D 2|5|8")
     #dirty = [pygame.display.get_surface().get_size()]
     dirty = []
     going = True