# HG changeset patch # User Stefano Rivera # Date 1301954767 -7200 # Node ID 4713a2a3b0be12548dcb8f1187ce11a372358e0d # Parent aca8b7456c72173ffc2d4949468992ca552bb8f4 Initial cutscene screen diff -r aca8b7456c72 -r 4713a2a3b0be skaapsteker/cutscene.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/skaapsteker/cutscene.py Tue Apr 05 00:06:07 2011 +0200 @@ -0,0 +1,65 @@ +# -*- 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, unindent_text + +class CutScene(Scene): + def __init__(self, game_state, text, background): + super(CutScene, self).__init__(game_state) + self._game_state = game_state + self.text = text + self.text_widget = Text(text, pygame.Rect(20, 20, 800-40, 600-40), + size=24) + self.background = data.load_image('backgrounds/' + background) + self.start_time = pygame.time.get_ticks() + self.run_time = 60000 # ms + + 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)) + + self.text_widget.draw(surface) + super(CutScene, self).draw(surface, engine) + + def dispatch(self, ev): + if ev.type is KEYDOWN: + if ev.key in(K_q, K_ESCAPE): + # Avoid circular import... + from .menuscene import MenuScene + ChangeScene.post(MenuScene(self._game_state)) + super(CutScene, self).dispatch(ev) + + +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') diff -r aca8b7456c72 -r 4713a2a3b0be skaapsteker/menuscene.py --- a/skaapsteker/menuscene.py Tue Apr 05 00:04:30 2011 +0200 +++ b/skaapsteker/menuscene.py Tue Apr 05 00:06:07 2011 +0200 @@ -1,6 +1,7 @@ import pygame from pygame.locals import K_ESCAPE, K_q, KEYDOWN, QUIT +from .cutscene import opening_cutscene from .engine import ChangeScene, Scene from .levelscene import LevelScene from .widgets.text import Text, TextChoice @@ -16,6 +17,7 @@ ('Level 3', 'level3'), ('Level 4', 'level4'), ('Level 5', 'level5'), + ('Starting Cutscene', 'cutscene'), ('Quit', 'quit'), ] if cur_game is not None: @@ -28,11 +30,13 @@ "Callback from menu TextChoice" if data.startswith('level'): ChangeScene.post(LevelScene(self._game_state, data)) - elif data == 'quit': - pygame.event.post(pygame.event.Event(QUIT)) elif data == 'resume': self.cur_game.thaw() ChangeScene.post(self.cur_game) + elif data == 'cutscene': + ChangeScene.post(opening_cutscene(self._game_state)) + elif data == 'quit': + pygame.event.post(pygame.event.Event(QUIT)) def draw(self, surface, engine): surface.fill(pygame.Color('black')) diff -r aca8b7456c72 -r 4713a2a3b0be skaapsteker/widgets/text.py --- a/skaapsteker/widgets/text.py Tue Apr 05 00:04:30 2011 +0200 +++ b/skaapsteker/widgets/text.py Tue Apr 05 00:06:07 2011 +0200 @@ -20,6 +20,21 @@ fonts[(name, size)] = pygame.font.Font(fontfn, size) return fonts[(name, size)] +# Probably belongs in a utils module +def unindent_text(text): + """Reformat docstring-style text blocks.""" + text = text.split('\n') + while text[0].strip() == '': + text.pop(0) + while text[-1].strip() == '': + text.pop(-1) + indent = len(text[0]) - len(text[0].lstrip()) + indent_chars = text[0][:indent] + for index, line in enumerate(text): + if line.startswith(indent_chars): + text[index] = line[indent:] + return '\n'.join(text) + class Text(Widget): def __init__(self, text, pos, font='sans', size=16, color='black'):