1 """Presents video/audio to the user in a manner similar to Slides"""
2 import pygame, os, pyzzle
3 from pygame.rect import Rect
4 from pygame.surface import Surface
5 from pygame.sprite import Group
6 from Panel import Panel
7 import media
8
10 """Presents video/audio to the user in a manner similar to Slides.
11
12 Like the Slide, movies can blit an image to the screen,
13 perform click and highlight behavior, nest within Panels,
14 and even nest Sprites such as Hotspots, Text, and Slides.
15
16 It is worth mentioning that Pygame has limited support for
17 movies, and these limitations transfer to Pyzzle.
18 Movie files must use the MPEG-1 video codec. If the movie files
19 contain audio, they must use the MPEG-2 audio codec (not MP2),
20 and no other sounds can play alongside them. As of 0.9,
21 I have not been able to get sound to play from movie files.
22 However, you can specify a seperate audio file to play alongside
23 the movie using the soundfile attribute. This has the added
24 benefit of being able to play alongside other sounds.
25 """
26 - def __init__(self, id, moviefile, soundfile=None, stage=None,
27 rectRel=None, layer=0, loop=False, onStop=lambda:None):
28 """Creates a Movie
29 @param id: A unique identifier for the Movie.
30 @param moviefile: The name of the movie file to be played
31 @param soundfile: The name of the sound file to be played in sync
32 with the movie. Use this parameter if you cannot get sound
33 to work from your movie file, or if you would like other sounds
34 to play along with the movie (ambience, music, effects, etc.)
35 @type stage: Stage
36 @param stage: An area of the game in which the movie occurs. Used to determine
37 folder paths.
38 @type rectRel: RelativeRect
39 @param rectRel: The rectangle occupied by the Movie.
40 rect width and height do not resize the movie - this is a limitation
41 within Pygame.
42 @type layer: float
43 @param layer: The layer of the Movie. Larger numbers represent upper layers.
44 Upper layers will draw over lower layers.
45 @type boolean:
46 @param loop: Whether the movie should loop endlessly.
47 @param onStop: The function that plays upon completion of the movie.
48 If loop=True, this function will never fire.
49 """
50 Panel.__init__(self)
51 self.id = id
52 self.file = moviefile
53 self.stage=stage
54 self.rectRel=rectRel
55 self._rect=None
56 self._layer=layer
57 self.loaded=False
58 self.surface=None
59 self.onStop=onStop
60 self.loop=loop
61 self.played=False
62
63 self.soundfile=soundfile
64 if stage:
65 self.file=os.path.join(stage.folder, self.file)
66
87 movie=property(_getMovie)
89 """The coordinates of the movie.
90 rect coordinates are determined by rectRel.
91 If a coordinate in rectRel is None, the coordinate is
92 determined by the slide's image size.
93 """
94 self._getMovie()
95 return self._rect
96 rect=property(_getRect)
97
98
99 - def draw(self, screen):
100 """Renders the movie to the screen,
101 then draws any sprites within the Movie."""
102 movie=self._getMovie()
103 if not movie.get_busy():
104 if self.loop:
105 movie.rewind()
106 elif not self.played:
107 self.played=True
108 self.exit(delay=0)
109 self.onStop()
110 screen.blit(self.surface, self._rect)
111 Panel.draw(self, screen)
112 - def enter(self, oldslide=None, delay=.1):
113 """Called when the user encounters the Movie.
114 @type oldslide: Panel
115 @param oldslide: The Panel that was previously presented to the user,
116 to be replaced by self
117 @param delay: The time it should take for oldslide to transition to self
118 """
119 movie=self._getMovie()
120 if self.soundfile:
121 sound=media.sounds.load(self.soundfile)
122 sound.play(-1, fade_ms=int(delay*1000))
123 elif movie.has_audio():
124 pygame.mixer.quit()
125 movie.set_volume(1.0)
126 movie.play()
127 - def exit(self, newslide=None, delay=.1):
128 """Called when the user exits the Movie.
129 @type newslide: Panel
130 @param newslide: The Panel that was previously presented to the user,
131 to be replaced by self
132 @param delay: The time it should take for oldslide to transition to self,
133 in seconds
134 """
135 movie=self._getMovie()
136 if self.soundfile:
137 sound=media.sounds.load(self.soundfile)
138 sound.fadeout(int(delay*1000))
139 movie.stop()
140