diff --git a/music.py b/music.py new file mode 100644 index 0000000..fc949e1 --- /dev/null +++ b/music.py @@ -0,0 +1,35 @@ +from __future__ import print_function + +import sys +from PySide import phonon + +class MusicBox(object): + # default media + media = {} + + # state + _media_obj = None + + @classmethod + def init(clazz): + clazz._output = phonon.Phonon.AudioOutput(phonon.Phonon.MusicCategory) + clazz._media_obj = phonon.Phonon.MediaObject() + phonon.Phonon.createPath(clazz._media_obj, clazz._output) + + @classmethod + def add_music(clazz, name, media_path): + clazz.media[name] = phonon.Phonon.MediaSource(media_path) + + @classmethod + def play_music(clazz, name): + try: + clazz._media_obj.setCurrentSource(clazz.media[name]) + clazz._media_obj.play() + except KeyError: + print("Warning: Song '%s' is missing, not found in media base" % (name,), file=sys.stderr) + + @classmethod + def stop_music(clazz): + # use pause so we don't stop having the device open + clazz._media_obj.pause() + diff --git a/seopardy.py b/seopardy.py index 6c6a3ec..ce26d80 100755 --- a/seopardy.py +++ b/seopardy.py @@ -7,6 +7,7 @@ import sys #from PySide import QtCore, QtGui from PySide import QtGui +from music import MusicBox from question import Questions from gamestate import GameState from game import SeopardyGame @@ -51,6 +52,9 @@ if __name__ == '__main__': else: gamestate = GameState(args.savedir) + # init music box + MusicBox.init() + # create board board = SeopardyGame(questions, gamestate) board.show() diff --git a/windows.py b/windows.py index 5ed38bd..bba5f0e 100644 --- a/windows.py +++ b/windows.py @@ -3,6 +3,7 @@ from PySide import QtGui, QtCore import copy from gamestate import QuestionAnswers +from music import MusicBox from player import Player, ButtonEvent, nobodyColor class QuestionWindow(QtGui.QDialog): @@ -13,16 +14,18 @@ class QuestionWindow(QtGui.QDialog): self.section = section self.qnumber = qnumber self.question = question + self.dj = dj if answers is not None: self.answers = answers else: self.answers = QuestionAnswers(self.section, self.qnumber) - self.dj = dj - if self.dj and not self.answers.is_answered(): - self.answers.set_dj_points(self.dj[1]) - playerNo = self.players.index(self.dj[0])+1 - QtCore.QCoreApplication.postEvent(self, ButtonEvent(playerNo)) + if not self.answers.is_answered(): + MusicBox.play_music("questionSong") + if self.dj: + self.answers.set_dj_points(self.dj[1]) + playerNo = self.players.index(self.dj[0])+1 + QtCore.QCoreApplication.postEvent(self, ButtonEvent(playerNo)) self._setupGui() self.setWindowTitle("Seopardy - %s - %d" % (section, qnumber*100)) @@ -31,6 +34,13 @@ class QuestionWindow(QtGui.QDialog): def get_answers(self): return self.answers + def closeEvent(self, event): + if not self._inWindow: + MusicBox.stop_music() + event.accept() + else: + event.ignore() + def _mkQuestionLabel(self, text): question = QtGui.QLabel(text, alignment=QtCore.Qt.AlignCenter) question.setWordWrap(True) @@ -73,7 +83,8 @@ class QuestionWindow(QtGui.QDialog): res = qawin.exec_() if res == QuestionAnswerWindow.CORRECT: self.answers.add_try(player, correct=True) - self.accept() + self._inWindow = False + self.close() elif res == QuestionAnswerWindow.WRONG: self.answers.add_try(player, correct=False) self._inWindow = False