view skaapsteker/cutscene.py @ 247:8c0c132b422f

Button widget for cutscene
author Stefano Rivera <stefano@rivera.za.net>
date Thu, 07 Apr 2011 19:25:48 +0200
parents 4050e77dade6
children de60329cfc9f
line wrap: on
line source

# -*- coding: utf-8 -*-
from __future__ import division

import pygame
from pygame.locals import K_ESCAPE, K_q, KEYDOWN

from . import data
from .engine import ChangeScene, Scene
from .widgets.text import Text, ButtonSet, TextButton, unindent_text

class CutScene(Scene):
    def __init__(self, game_state, text, background, music=None):
        super(CutScene, self).__init__(game_state)
        self.background = data.load_image('backgrounds/' + background)
        self.start_time = pygame.time.get_ticks()
        self.run_time = 60000 # ms

        self._background_music = None
        if music and pygame.mixer.get_init():
            self._background_music = data.filepath(music)

        text_widget = Text(text, pygame.Rect(20, 20, 800-40, 600-40),
                           size=24, shadow='gray', wrap=True)
        self.widgets.append(text_widget)

        button_set = ButtonSet()
        button_set.append(TextButton("Continue", (300, 500), size=24, color='yellow'))
        button_set.callbacks.append(self.done)
        self.widgets.append(button_set)

    def done(self, selected=None, data=None):
        # Avoid circular import...
        from .menuscene import MenuScene
        ChangeScene.post(MenuScene(self.game_state))

    def draw(self, surface, engine):
        viewport = surface.get_clip()

        # Scoll background back and forth:
        max_pos = self.background.get_rect().width - viewport.width
        bottom = self.background.get_rect().height - viewport.height
        pos = ((pygame.time.get_ticks() - self.start_time)
               % (self.run_time * 2)) * max_pos // (self.run_time)
        if pos > max_pos:
            pos = (2 * max_pos) - pos
        surface.blit(self.background, viewport.topleft,
                     pygame.Rect((pos, bottom), viewport.size))

        super(CutScene, self).draw(surface, engine)

    def dispatch(self, ev):
        if ev.type is KEYDOWN:
            if ev.key in(K_q, K_ESCAPE):
                self.done()
        super(CutScene, self).dispatch(ev)

    def enter(self):
        if self._background_music:
            pygame.mixer.music.load(self._background_music)
            pygame.mixer.music.play(-1)

    def leave(self):
        if self._background_music:
            pygame.mixer.music.stop()


def opening_cutscene(game_state):
    text = u"""
    Many moons ago, an evil nine-tailed kitsune, a fearsome fox god, ruled the
    land.

    It had many powers — the ability to shape shift, turn invisible, control
    the elements. Its powers came from its majestic tails and the kitsune
    guarded them ferociously. If any fox attained eight tails, it would battle
    the kitsune for domination of the celestial world. To prevent this, the
    kitsune stole the tails of all the foxes it encountered.

    The evil kitsune challenged every fox to a haiku contest. But the
    competition was not fair, and the kitsune cheated every time. When it
    defeated the poor fox, it stole its tail and threw it to the four winds.

    The kitsune stole your tail. Now it’s time to get it back.
    """
    text = unindent_text(text)
    return CutScene(game_state, text, 'background_01_back.png')