import pygame, sys, os from pygame.locals import * from eduGames import * from Paint import * class ColorPaint(Game): (INITIAL, WAITING, BAD_CHOICE, GOOD_CHOICE, WAITING_PAINT, PAINTING, END) = (0,1,2,3,4,5, 6) def __init__(self, screen, uiMgr, sndMgr, screenMgr, path): Game.__init__(self, screen, uiMgr, sndMgr, screenMgr, path) self.finished = False self.puzzleElements = None self.paintBoxes = None self.currentElementIndex = 0 self.state = ColorPaint.INITIAL self.puzzleElementControls = None self.selectedColor = "" self.soundIcon = None self.soundsDir = "" #set later by the Games class self.xMargin = 0 self.yMargin = 0 self.masterResourcesDir = None self.brush = None self.fakeMouse = None self.isHelp = False self.onNavigationPortion = False self.xStartBrushZone = 0 self.yStartBrushZone = 0 self.xEndBrushZone = 1030 + self.xMargin self.yEndBrushZone = 0 def getCurrentPuzzleElement(self): return self.puzzleElements[self.currentElementIndex] def getCurrentPuzzleElementControl(self): return self.puzzleElementControls[self.currentElementIndex] def pause(self): self.getUiMgr().removeControl(self.brush) pygame.mouse.set_visible(True) def unPause(self): self.getUiMgr().addControl(self.brush) pygame.mouse.set_visible(False) def initializeGameData(self): self.brush = Brush(self, "") self.alert = os.path.join(self.soundsDir, "chord.ogg") self.ding = os.path.join(self.soundsDir, "ding.ogg") (self.puzzleElements, self.paintBoxes, backgroundPath, backgroundX, backgroundY) = self.readInfo() self.showElements() self.showPaintBoxes() fakeMouse = [c for c in self.getUiMgr().controls if c.type == "fakeMouse"] self.fakeMouse = None if not len(fakeMouse) == 0: self.fakeMouse = fakeMouse[0] self.brush.trackControl(self.fakeMouse) self.fakeMouse.setLayer(20) self.getUiMgr().addControl(self.brush) if not self.isHelp: pygame.mouse.set_visible(False) def showElements(self): x = self.settings["xFirstElement"] + self.xMargin y = self.settings["yFirstElement"] + self.yMargin distanceBetweenElements = -1 if self.settings.has_key("distanceBetweenElements"): distanceBetweenElements = self.settings["distanceBetweenElements"] self.puzzleElementControls = [] counter = 1 for element in self.puzzleElements: if counter > self.settings["elementsPerRow"] and counter % self.settings["elementsPerRow"] == 1: xLastRow = self.settings["xFirstElementLastRow"] + self.xMargin x = xLastRow y = y + self.settings["spaceBetweenRows"] ic = GrowsAndShrinksAnimatedControl(self, x, y, os.path.join(self.resourcesDir, element.image), "", element.getNumberOfStates()) ic.setLayer(6) ic.type = "drawing" if distanceBetweenElements > -1: x += distanceBetweenElements else: x += ic.getWidth() self.getUiMgr().addControl(ic) self.puzzleElementControls.append(ic) counter += 1 def showPaintBoxes(self): x = self.settings["paintBoxesX"] + self.xMargin y = self.settings["paintBoxesY"] + self.yMargin watercolor = ImageControl(self, x, y, os.path.join(self.masterResourcesDir, "_acuarelas.png")) watercolor.setLayer(5) 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.getUiMgr().addControl(watercolor) self.getUiMgr().addControl(self.soundIcon) x = self.settings["paintBoxesX"] + self.xMargin + 75 y = self.settings["paintBoxesY"] + self.yMargin + 25 for element in self.paintBoxes: ic = Control(self, x, y, 50, 50) ic.color = element ic.setLayer(4) ic.type = "paint" x += 7 + ic.getWidth() self.getUiMgr().addControl(ic) self.yEndBrushZone = watercolor.getY() + watercolor.getHeight() def readInfo(self): fileName = os.path.join(self.path, self.settings["infoFile"]) file = open(fileName, "r") fileText = file.read() reader = ScreensReader() file.close() return reader.read(fileText) def on_mouse_hover(self, clickedControl): if clickedControl == self.soundIcon: self.soundIcon.setImageDivisionIndex(1) self.onNavigationPortion = True else: if self.soundIcon.getImageDivisionIndex() == 1: #might improve performance. self.soundIcon.setImageDivisionIndex(0) mousePos = pygame.mouse.get_pos() mousePosX = mousePos[0] mousePosY = mousePos[1] if mousePosX < self.xStartBrushZone or mousePosY < self.yStartBrushZone or mousePosX > self.xEndBrushZone or mousePosY > self.yEndBrushZone: self.onNavigationPortion = True else: self.onNavigationPortion = False try: if self.onNavigationPortion: if self.brush.isVisible(): self.brush.makeInvisible() else: if not self.brush.isVisible(): self.brush.makeVisible() except: pass def on_mouse_button_down(self, clickedControl): if self.state == Paint.WAITING: if clickedControl.type == "paint": self.selectedColor = clickedControl.color elif clickedControl == self.soundIcon: self.state = Paint.INITIAL elif clickedControl == self.puzzleElementControls[self.currentElementIndex]: soundPath = os.path.join(self.soundsDir, self.getCurrentPuzzleElement().currentAudio()) self.getSoundMgr().addSoundForPlayback(soundPath) if self.state == Paint.WAITING_PAINT: if clickedControl.type == "drawing" and clickedControl is self.getCurrentPuzzleElementControl(): self.state = Paint.PAINTING def paintCurrentElement(self): self.getCurrentPuzzleElementControl().setImageDivisionIndex(self.getCurrentPuzzleElement().getCurrentIndex()+1) def playSound(self, sound): self.getSoundMgr().addSoundForPlayback(sound) def updateState(self): mouseVisible = self.isHelp or self.isPaused or self.onNavigationPortion pygame.mouse.set_visible(mouseVisible) if self.state == Paint.INITIAL: soundPath = os.path.join(self.soundsDir, self.getCurrentPuzzleElement().currentAudio()) self.getSoundMgr().addSoundForPlayback(soundPath) self.state = Paint.WAITING currentControl = self.getCurrentPuzzleElementControl() currentControl.playAnimation() return if self.state == Paint.WAITING: #Actually two states: with and without a selected color. if self.selectedColor != "": if self.selectedColor == self.getCurrentPuzzleElement().currentColor(): self.state = Paint.GOOD_CHOICE self.getUiMgr().removeControl(self.brush) self.brush = Brush(self, self.selectedColor, self.fakeMouse) self.getUiMgr().addControl(self.brush) else: self.state = Paint.BAD_CHOICE self.selectedColor = "" return if self.state == Paint.BAD_CHOICE: self.playSound(self.alert) self.state = Paint.WAITING return if self.state == Paint.GOOD_CHOICE: self.playSound(self.ding) self.state = Paint.WAITING_PAINT return if self.state == Paint.PAINTING: self.paintCurrentElement() self.getCurrentPuzzleElement().moveOn() self.getUiMgr().removeControl(self.brush) self.brush = Brush(self) self.brush.trackControl(self.fakeMouse) self.getUiMgr().addControl(self.brush) self.state = Paint.INITIAL if self.getCurrentPuzzleElement().solved(): self.getCurrentPuzzleElementControl().stopAnimation() self.currentElementIndex += 1 if self.currentElementIndex >= len(self.puzzleElements): self.state = Paint.END return if self.state == Paint.END: self.finished = True if not self.fakeMouse: pygame.mouse.set_visible(True) if not self.isHelp: self.saveAsDone() return def endGame(self): self.finished = True if not self.fakeMouse: pygame.mouse.set_visible(True)