示例#1
0
class TitleScreen(context.Context):
    borderImage = Image("lpc-border0.png", colorkey=True)

    def init(self):
        self.background = (109, 109, 109)
        self.border = draw.GraphicBox(self.borderImage)
        self.counter = 0
        self.game = None

        self.new_game()


    def enter(self):
        if self.game:
            self.menu = Menu(20, -5, 'vertical', 100,
                [('New Game', self.new_game),
                ('Continue', self.continue_game),
                #('Save', self.save_game),
                #('Reload', self.load_game),
                #('Save and Quit', self.savequit_game),
                ('Quit', self.quit_game)],
                font="visitor1.ttf", font_size=20)
        else:
            self.menu = Menu(20, -5, 'vertical', 100,
                [('New Game', self.new_game),
                #('Continue', self.load_game),
                ('Introduction', self.show_intro),
                ('Quit', self.quit_game)],
                font="visitor1.ttf", font_size=20)

        self.menu.rect = pygame.Rect(12,12,20,100)
        self.redraw = True


    def handle_event(self, event):
        self.menu.handle_event(event)


    def draw(self, surface):
        if self.redraw:
            self.redraw = False
            #if self.game:
            self.border.draw(surface, surface.get_rect())

        self.menu.draw(surface)


    def new_game(self):
        res.fadeoutMusic(1000)
        self.game = world.build()
        level = self.game.getChildByGUID(5001)
        self.driver.append(LevelState(self.driver, level))


    def save_game(self):
        path = os.path.join("resources", "saves", "save")
        [ i.unload() for i in self.game.getChildren() ]
        self.game.save(path)
        self.continue_game()


    def load_game(self):
        try:
            path = os.path.join("resources", "saves", "save")
            self.game = loadObject(path)
        except IOError:
            return self.new_game()

        level = self.game.getChildByGUID(5001)
        self.driver.start(LevelState(level))


    def continue_game(self):
        res.fadeoutMusic(1000)
        level = self.game.getChildByGUID(5001)
        self.driver.start(LevelState(level))


    def show_intro(self):
        self.driver.start_restart(InstructionScreen())


    def savequit_game(self):
        if self.game:
            path = os.path.join("resources", "saves", "save")
            [ i.unload() for i in self.game.getChildren() ]
            self.game.save(path)
        self.quit_game()


    def quit_game(self):
        self.driver.done()