outofdesert / outofdesert / main.py

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

WIDTH, HEIGHT = 640,480


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



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 []


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.floor.add(self)

        self.duck= Duck().start()
        self.duck.add(self)
        self.duck.fly()

        self.hero = Hero().start()
        self.hero.add(self)


        return self


class Hero(Sprite):

    def start(self):
        self.images = {}
        self.images['stand'] = load_image(data_dir("hero.png"))
        self.images['duck'] = load_image(data_dir("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 fly(self):
        self.dx = -1
    def stop(self):
        self.dx = 0
    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.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



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




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
    pygame.display.init()
    screen = pygame.display.set_mode(SCREEN_SIZE)
    #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.update()
        dirty = g.draw(screen)
        pygame.display.update(dirty)
        clock.tick(20)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.