import pygame, sys, os from pygame.locals import * from eduGames import * from pygame.font import * class MultipleChoice(Game): (INITIAL, WAITING, GOOD_CHOICE, BAD_CHOICE, END) = (0,1,2,3,4) def __init__(self, screen, uiMgr, sndMgr, screenMgr, path): Game.__init__(self, screen, uiMgr, sndMgr, screenMgr, path) self.currentQuestionIndex = 0 self.questions = None self.finished = False self.hoveredOnControl = None self.state = MultipleChoice.INITIAL self.soundIcon = None self.soundsDir = "" #set later by Games class self.xMargin = 0 self.yMargin = 0 self.isHelp = False self.masterResourcesDir = None self.successSound = "" def readInfo(self): self.alert = os.path.join(self.soundsDir,"chord.ogg") fileName = os.path.join(self.path, self.settings["infoFile"]) puzzleFile = open(fileName, "r") fileText = puzzleFile.read() puzzleFileReader = Reader(self.resourcesDir, self.soundsDir) puzzleFile.close() return puzzleFileReader.read(fileText) def initializeGameData(self): self.questions = self.readInfo() self.soundIcon = ImageControl(self, self.settings["soundIconX"] + self.xMargin, self.settings["soundIconY"] + self.yMargin, os.path.join(self.masterResourcesDir, "sound.png"), "", 2) self.soundIcon.setLayer(5) self.soundIcon.type = "soundIcon" self.successSound = os.path.join(self.soundsDir, "bing.ogg") def showCurrentQuestion(self): fontPath = os.path.join(self.resourcesDir, "Helvetica LT Condensed Black.ttf") font = Font(fontPath, 50) self.getUiMgr().deleteGameControls(self) self.getUiMgr().addControl(self.soundIcon) x = self.settings["leftMargin"] + self.xMargin y = self.settings["topMargin"] + self.yMargin question = self.questions[self.currentQuestionIndex] questionControl = ImageControl(self, x, y, question.image, question.audio, 1) questionControl.setLayer(4) self.getUiMgr().addControl(questionControl) x = self.settings["firstOptionX"] + self.xMargin for option in question.options: optionControl = AnimatedImageControl(self, x, y, option.image, 3) optionControl.optionName = option.name optionControl.type = "option" optionControl.framesBetweenUpdates = 3 if self.settings.has_key("animationSpeed"): self.settings["animationSpeed"] optionControl.pingpong = True optionControl.setLayer(4) txt = option.name xText = optionControl.getX() + optionControl.getWidth() + 30 yText = optionControl.getY() + optionControl.getHeight()/2 - 20 text = Label(self, xText, yText, 20, 10, font, txt, 4) text.color = (0,0,0) text.background = None self.getUiMgr().addControl(text) self.getUiMgr().addControl(optionControl) y = y + optionControl.getHeight() + self.settings["spaceBetweenOptions"] def on_mouse_hover(self, clickedControl): if not self.hoveredOnControl is None: self.hoveredOnControl.pause() self.hoveredOnControl = None if clickedControl.type == "option": if not self.hoveredOnControl is clickedControl: self.hoveredOnControl = clickedControl self.hoveredOnControl.playAnimation(-1) if clickedControl == self.soundIcon: self.soundIcon.setImageDivisionIndex(1) else: self.soundIcon.setImageDivisionIndex(0) def on_mouse_button_down(self, clickedControl): if self.state == MultipleChoice.WAITING: if clickedControl.type == "option": if clickedControl.optionName == self.questions[self.currentQuestionIndex].correctOption: self.state = MultipleChoice.GOOD_CHOICE else: self.state = MultipleChoice.BAD_CHOICE elif clickedControl is self.soundIcon: self.getSoundMgr().addSoundForPlayback(self.questions[self.currentQuestionIndex].audio) def updateState(self): if not self.state == MultipleChoice.END: question = self.questions[self.currentQuestionIndex] if self.state == MultipleChoice.INITIAL: self.showCurrentQuestion() self.getSoundMgr().addSoundForPlayback(question.audio, True) self.state = MultipleChoice.WAITING if self.state == MultipleChoice.GOOD_CHOICE: self.currentQuestionIndex += 1 self.getSoundMgr().addSoundForPlayback(self.successSound, False, True) if self.currentQuestionIndex == len(self.questions): self.state = MultipleChoice.END else: self.state = MultipleChoice.INITIAL if self.state == MultipleChoice.BAD_CHOICE: self.getSoundMgr().addSoundForPlayback(self.alert, True) self.state = MultipleChoice.WAITING if self.state == MultipleChoice.END: self.finished = True if not self.isHelp: self.saveAsDone() class Reader: def __init__(self, resourcesDir, soundsDir): self.resourcesDir = resourcesDir self.soundsDir = soundsDir def read(self, puzzlesDefinition): (NONE, QUESTION, OPTIONS) = (0,1,2) lines = puzzlesDefinition.splitlines() state = NONE currentQuestion = None questions = [] for line in lines: line = line.strip() if line == "": continue if line.startswith("#"): continue if line == "question:": state = QUESTION continue if line == "options:": state = OPTIONS continue if state == QUESTION: questionData = line.split() currentQuestion = Question(questionData[0], questionData[1], questionData[2], self.resourcesDir, self.soundsDir) questions.append(currentQuestion) continue #Next line must be "options" if state == OPTIONS: optionData = line.split() option = Option(optionData[0], optionData[1], self.resourcesDir) currentQuestion.options.append(option) return questions class Option: def __init__(self, name, image, resourcesDir): self.name = name self.image = os.path.join(resourcesDir, image) class Question: def __init__(self, image, audio, correctOption, resourcesDir, soundsDir): self.image = os.path.join(resourcesDir, image) self.audio = os.path.join(soundsDir, audio) self.correctOption = correctOption self.options = []