Mercurial > skaapsteker
annotate skaapsteker/cutscene.py @ 243:4050e77dade6
Add support for background music tracks
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Thu, 07 Apr 2011 16:42:54 +0200 |
parents | 691d2795ae3c |
children | 8c0c132b422f |
rev | line source |
---|---|
134 | 1 # -*- coding: utf-8 -*- |
2 from __future__ import division | |
3 | |
4 import pygame | |
5 from pygame.locals import K_ESCAPE, K_q, KEYDOWN | |
6 | |
7 from . import data | |
8 from .engine import ChangeScene, Scene | |
9 from .widgets.text import Text, unindent_text | |
10 | |
11 class CutScene(Scene): | |
243
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
12 def __init__(self, game_state, text, background, music=None): |
134 | 13 super(CutScene, self).__init__(game_state) |
14 self.text = text | |
15 self.text_widget = Text(text, pygame.Rect(20, 20, 800-40, 600-40), | |
183 | 16 size=24, shadow='gray', wrap=True) |
134 | 17 self.background = data.load_image('backgrounds/' + background) |
18 self.start_time = pygame.time.get_ticks() | |
19 self.run_time = 60000 # ms | |
243
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
20 self._background_music = None |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
21 if music and pygame.mixer.get_init(): |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
22 self._background_music = data.filepath(music) |
134 | 23 |
24 def draw(self, surface, engine): | |
25 viewport = surface.get_clip() | |
26 | |
27 # Scoll background back and forth: | |
28 max_pos = self.background.get_rect().width - viewport.width | |
29 bottom = self.background.get_rect().height - viewport.height | |
30 pos = ((pygame.time.get_ticks() - self.start_time) | |
31 % (self.run_time * 2)) * max_pos // (self.run_time) | |
32 if pos > max_pos: | |
33 pos = (2 * max_pos) - pos | |
34 surface.blit(self.background, viewport.topleft, | |
35 pygame.Rect((pos, bottom), viewport.size)) | |
36 | |
37 self.text_widget.draw(surface) | |
38 super(CutScene, self).draw(surface, engine) | |
39 | |
40 def dispatch(self, ev): | |
41 if ev.type is KEYDOWN: | |
42 if ev.key in(K_q, K_ESCAPE): | |
43 # Avoid circular import... | |
44 from .menuscene import MenuScene | |
165
8d45715c587d
Rename ._game_state to game_state for consistency with engine and since everyone and their dog has access to it. :)
Simon Cross <hodgestar@gmail.com>
parents:
164
diff
changeset
|
45 ChangeScene.post(MenuScene(self.game_state)) |
134 | 46 super(CutScene, self).dispatch(ev) |
47 | |
243
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
48 def enter(self): |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
49 if self._background_music: |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
50 pygame.mixer.music.load(self._background_music) |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
51 pygame.mixer.music.play(-1) |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
52 |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
53 def leave(self): |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
54 if self._background_music: |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
55 pygame.mixer.music.stop() |
4050e77dade6
Add support for background music tracks
Neil Muller <drnlmuller@gmail.com>
parents:
183
diff
changeset
|
56 |
134 | 57 |
58 def opening_cutscene(game_state): | |
59 text = u""" | |
60 Many moons ago, an evil nine-tailed kitsune, a fearsome fox god, ruled the | |
61 land. | |
62 | |
63 It had many powers — the ability to shape shift, turn invisible, control | |
64 the elements. Its powers came from its majestic tails and the kitsune | |
65 guarded them ferociously. If any fox attained eight tails, it would battle | |
66 the kitsune for domination of the celestial world. To prevent this, the | |
67 kitsune stole the tails of all the foxes it encountered. | |
68 | |
69 The evil kitsune challenged every fox to a haiku contest. But the | |
70 competition was not fair, and the kitsune cheated every time. When it | |
71 defeated the poor fox, it stole its tail and threw it to the four winds. | |
72 | |
73 The kitsune stole your tail. Now it’s time to get it back. | |
74 """ | |
75 text = unindent_text(text) | |
76 return CutScene(game_state, text, 'background_01_back.png') |