Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix playmus.py example (broken in 2.0.1) #2738

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/reST/ref/music.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ can crash the program, ``e.g``. Debian Linux. Consider using ``OGG`` instead.
| :sg:`rewind() -> None`

Resets playback of the current music to the beginning.

.. note:: :func:`rewind` supports a limited number of file types (``MOD``, ``OGG``,
``MP3`` and Native MIDI files). For unsupported file types use :func:`play`
which will restart the music if it's already playing.

.. ## pygame.mixer.music.rewind ##

Expand Down
40 changes: 26 additions & 14 deletions examples/playmus.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def __new__(cls, *args, **kwds):

def __init__(self, title):
pg.display.set_caption(title)
self.screen.fill(pg.Color("white"))
self.text_color = (254, 231, 21, 255)
self.background_color = (16, 24, 32, 255)
self.screen.fill(self.background_color)
pg.display.flip()

pygame.freetype.init()
Expand All @@ -57,10 +59,11 @@ def __init__(self, title):
self.line_height = self.ascender - self.descender

self.write_lines(
"'q', ESCAPE or close this window to quit\n"
"SPACE to play/pause\n"
"'r' to rewind\n"
"'f' to faid out over 5 seconds\n",
"\nPress 'q' or 'ESCAPE' or close this window to quit\n"
"Press 'SPACE' to play / pause\n"
"Press 'r' to rewind to the beginning (restart)\n"
"Press 'f' to fade music out over 5 seconds\n\n"
"Window will quit automatically when music ends\n",
0,
)

Expand All @@ -84,10 +87,11 @@ def write_lines(self, text, line=0):
for i, text_line in enumerate(text.split("\n"), line):
y = i * line_height + self.ascender
# Clear the line first.
self.screen.fill(pg.Color("white"), (0, i * line_height, w, line_height))

self.screen.fill(
self.background_color, (0, i * line_height, w, line_height)
)
# Write new text.
self.font.render_to(self.screen, (15, y), text_line, pg.Color("blue"))
self.font.render_to(self.screen, (15, y), text_line, self.text_color)
pg.display.flip()


Expand All @@ -112,7 +116,7 @@ def main(file_path):
pg.mixer.music.play()
win.write_lines("Playing ...\n", -1)

while pg.mixer.music.get_busy():
while pg.mixer.music.get_busy() or paused:
e = pg.event.wait()
if e.type == pg.KEYDOWN:
key = e.key
Expand All @@ -126,19 +130,27 @@ def main(file_path):
paused = True
win.write_lines("Paused ...\n", -1)
elif key == pg.K_r:
pg.mixer.music.rewind()
if file_path[-3:].lower() in ("ogg", "mp3", "mod"):
status = "Rewound."
pg.mixer.music.rewind()
else:
status = "Restarted."
pg.mixer.music.play()
if paused:
win.write_lines("Rewound.", -1)
pg.mixer.music.pause()
win.write_lines(status, -1)
elif key == pg.K_f:
win.write_lines("Fading out ...\n", -1)
pg.mixer.music.fadeout(5000)
# when finished get_busy() will return 0.
# when finished get_busy() will return False.
elif key in [pg.K_q, pg.K_ESCAPE]:
paused = False
pg.mixer.music.stop()
# get_busy() will now return 0.
# get_busy() will now return False.
elif e.type == pg.QUIT:
paused = False
pg.mixer.music.stop()
# get_busy() will now return 0.
# get_busy() will now return False.
pg.time.set_timer(pg.USEREVENT, 0)
finally:
pg.mixer.quit()
Expand Down