comparison skaapsteker/cutscene.py @ 375:8631e38afc24

Usage cutscene (with minor CutScene refactoring)
author Stefano Rivera <stefano@rivera.za.net>
date Sat, 09 Apr 2011 15:05:36 +0200
parents a43f571e8f5b
children fada4195349d
comparison
equal deleted inserted replaced
374:9530b8dbda5f 375:8631e38afc24
5 from pygame.locals import K_ESCAPE, K_q, KEYDOWN, SRCALPHA 5 from pygame.locals import K_ESCAPE, K_q, KEYDOWN, SRCALPHA
6 6
7 from . import constants 7 from . import constants
8 from . import data 8 from . import data
9 from .engine import ChangeScene, Scene 9 from .engine import ChangeScene, Scene
10 from .levelscene import LevelScene
10 from .widgets.text import Text, ButtonSet, TextButton, unindent_text 11 from .widgets.text import Text, ButtonSet, TextButton, unindent_text
11 12
12 class CutScene(Scene): 13 class CutScene(Scene):
13 def __init__(self, game_state, soundsystem, text, background, music=None): 14 def __init__(self, game_state, soundsystem):
14 super(CutScene, self).__init__(game_state, soundsystem) 15 super(CutScene, self).__init__(game_state, soundsystem)
15 self.background = data.load_image('backgrounds/' + background) 16 self._background_img = data.load_image('backgrounds/' + self.background)
16 fill = pygame.Surface(self.background.get_size(), flags=SRCALPHA) 17 fill = pygame.Surface(self._background_img.get_size(), flags=SRCALPHA)
17 fill.fill((255, 255, 255, 128)) 18 fill.fill((255, 255, 255, 128))
18 self.background.convert_alpha(fill) 19 self._background_img.convert_alpha(fill)
19 self.background.blit(fill, (0, 0)) 20 self._background_img.blit(fill, (0, 0))
20 self.background.convert_alpha() 21 self._background_img.convert_alpha()
21 self.start_time = pygame.time.get_ticks() 22 self._start_time = pygame.time.get_ticks()
22 self.run_time = 60000 # ms 23 self._run_time = 60000 # ms
23 24
24 self._background_music = None 25 self._background_music = self.music
25 self._background_music = music
26 26
27 text_widget = Text(text, pygame.Rect(20, 20, 27 text_widget = Text(unindent_text(self.text),
28 constants.SCREEN[0] - 40, 28 pygame.Rect(20, 20,
29 constants.SCREEN[1] - 40), 29 constants.SCREEN[0] - 40,
30 constants.SCREEN[1] - 40),
30 size=24, shadow='gray', wrap=True) 31 size=24, shadow='gray', wrap=True)
31 self.widgets.append(text_widget) 32 self.widgets.append(text_widget)
32 33
33 button_set = ButtonSet() 34 button_set = ButtonSet()
34 # TODO: Dynamic position 35 # TODO: Dynamic position
44 45
45 def draw(self, surface, engine): 46 def draw(self, surface, engine):
46 viewport = surface.get_clip() 47 viewport = surface.get_clip()
47 48
48 # Scoll background back and forth: 49 # Scoll background back and forth:
49 max_pos = self.background.get_rect().width - viewport.width 50 max_pos = self._background_img.get_rect().width - viewport.width
50 bottom = self.background.get_rect().height - viewport.height 51 bottom = self._background_img.get_rect().height - viewport.height
51 pos = ((pygame.time.get_ticks() - self.start_time) 52 pos = ((pygame.time.get_ticks() - self._start_time)
52 % (self.run_time * 2)) * max_pos // (self.run_time) 53 % (self._run_time * 2)) * max_pos // (self._run_time)
53 if pos > max_pos: 54 if pos > max_pos:
54 pos = (2 * max_pos) - pos 55 pos = (2 * max_pos) - pos
55 surface.blit(self.background, viewport.topleft, 56 surface.blit(self._background_img, viewport.topleft,
56 pygame.Rect((pos, bottom), viewport.size)) 57 pygame.Rect((pos, bottom), viewport.size))
57 58
58 super(CutScene, self).draw(surface, engine) 59 super(CutScene, self).draw(surface, engine)
59 60
60 def dispatch(self, ev): 61 def dispatch(self, ev):
66 def enter(self): 67 def enter(self):
67 if self._background_music: 68 if self._background_music:
68 self._soundsystem.play_background_music(self._background_music) 69 self._soundsystem.play_background_music(self._background_music)
69 70
70 71
71 def opening_cutscene(game_state, soundsystem): 72 class OpeningCutScene(CutScene):
72 text = u""" 73 text = u"""
73 Many moons ago, an evil nine-tailed kitsune, a fearsome fox god, ruled the 74 Many moons ago, an evil nine-tailed kitsune, a fearsome fox god, ruled the
74 land. 75 land.
75 76
76 It had many powers — the ability to shape shift, turn invisible, control 77 It had many powers — the ability to shape shift, turn invisible, control
83 competition was not fair, and the kitsune cheated every time. When it 84 competition was not fair, and the kitsune cheated every time. When it
84 defeated the poor fox, it stole its tail and threw it to the four winds. 85 defeated the poor fox, it stole its tail and threw it to the four winds.
85 86
86 The kitsune stole your tail. Now it’s time to get it back. 87 The kitsune stole your tail. Now it’s time to get it back.
87 """ 88 """
88 text = unindent_text(text) 89 background = 'background_01_back.png'
89 return CutScene(game_state, soundsystem, text, 'background_01_back.png') 90 music = None
91
92 def done(self, selected=None, data=None):
93 ChangeScene.post(LevelScene(self.game_state, self._soundsystem,
94 'temple.starting'))
95
96
97 class UsageCutScene(CutScene):
98 text = u"""
99 Arrow keys control your movement.
100
101 Double-tap ← or → to sprint, ↑ to fly (when you can).
102
103 Z and X perform attacks.
104 """
105 background = 'background_01_back.png'
106 music = None