Source

outofdesert / outofdesert / main.py

Full commit

import pygame
from pygame.locals import *
from skelutil import data_dir

# 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_alpha()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image


# these are awesome sprite classes.
class Sprite(pygame.sprite.DirtySprite):
    pass
class Group(pygame.sprite.LayeredDirty):
    pass

# 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.indicator = 0.5

        self.indicator = Indicator().start()
        self.indicator.add(self)


        self.floor = Floor().start()
        self.add(self.floor, layer = 2)

        self.duck= Duck().start()
        self.add(self.duck, layer = 20)
        self.duck.fly()

        self.hero = Hero().start()
        self.add(self.hero, layer = 10)

        self.baddy = Baddy().start()
        self.add(self.baddy, layer = 15)


        return self

    def handle_events(self, events):
        for e in events:
            if e.type == QUIT or e.type == KEYDOWN and e.key == K_ESCAPE:
                going = False
            if e.type == KEYDOWN:
                if e.key == K_d:
                    self.hero.duck()
            if e.type == KEYUP:
                if e.key == K_d:
                    self.hero.stand()


class Indicator(Sprite):
    def start(self):
        # when it gets to 1.0 the game is complete, 0.0 fail.
        self.indicator = 0.5
        self.rect = Rect(WIDTH -100, 0, 100, 20)
        self.image = pygame.Surface((100, 20))
        self.count = 0
        
        return self

    def update(self, *args):
        self.count += 1
        self.indicator += 0.001
        #if self.indicator > 0.6:
        #    self.indicator = 0.1

        howmuch = int(100 * self.indicator)
        #print ("howmuch %s %s" % (howmuch, self.indicator))
        self.image.fill(Color("white"))
        self.image.fill(Color("red"), (0, 0, howmuch, 20))
        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):
        self.rect = Rect(0, HEIGHT - 40, WIDTH, 20)
        self.image = pygame.Surface(self.rect.size)
        self.image.fill((255,255,255))

        self.count = 0
        return self

    def update(self, *args):
        self.count += 1

        # draw the floor.
        c = pygame.Color("black")
        start = (0,5)
        end = (self.image.get_width(), 5)

        r = pygame.draw.line(self.image, c, start, end)

        self.dirty = 1



def main():
    """ your app starts here
    """
    SCREEN_SIZE = WIDTH, HEIGHT
    pygame.display.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)
    pygame.display.set_caption("Out of the Desert.   K|W|D 2|5|8")
    #dirty = [pygame.display.get_surface().get_size()]
    dirty = []
    going = True
    g = Game().start()
    clock = pygame.time.Clock()
    screen.fill((255,255,255))

    while going:
        events = pygame.event.get()
        for e in events:
            if e.type == QUIT or e.type == KEYDOWN and e.key == K_ESCAPE:
                going = False

        g.handle_events(events)

        g.update()
        dirty = g.draw(screen)
        pygame.display.update(dirty)
        clock.tick(20)