Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/MultipleChoice.py
blob: b27078bf1c55f8e23cdf075de81e1308488a787a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
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 = []