class Key(Image): def __init__(self, fileName, x, y, polygon, game): """ Initialize a Key @param self -- the key @param fileName -- a string that contains the image name @param x -- a horizontal position of key @param y -- a vertical position of key @param polygon -- a list of (x,y) pairs @param game -- the game """ self.image = pygame.image.load("image/key/" + fileName + ".png") self.image.set_alpha(None) # disable alpha. self.image.convert() self.image.set_colorkey((255, 0, 255)) # magenta self.imageHover = pygame.image.load("image/keyHover/" + fileName + ".png") self.imageHover.set_alpha(None) # disable alpha. self.imageHover.convert() self.imageHover.set_colorkey((255, 0, 255)) # magenta self.imageDown = pygame.image.load("image/keyDown/" + fileName + ".png") self.imageDown.set_alpha(None) # disable alpha. self.imageDown.convert() self.imageDown.set_colorkey((255, 0, 255)) # magenta self.id = int(fileName) self.fileName = fileName self.sound = Sound() self.sound.setFileName("sound/notes/T" + str(int(fileName) + 3)) self.x = x self.y = y self.polygon = polygon self.isHover = False self.isDown = False self.isVisible = True self.mouseInside = False self.game = game def mouseHover(self): """ When the mouse is hover on the Key @param self -- the key """ self.isHover = True if os.path.exists("sound/notes/T" + str(self.id + 3) + ".ogg"): self.sound.play() def mouseDown(self): """ When the mouse is down the Key @param self -- the key """ self.isDown = True if os.path.exists("sound/notes/T" + str(self.id + 3) + ".ogg"): self.sound.play() self.game.attempt(self.id)
class Challenge: def __init__(self, fileName, keys, stick, stones, helps, ball, bar, rightBall, level, game): """ Initialize a challenge @param self -- the challenge @param fileName -- a string that contains the sound name @param keys -- the key vector @param stick -- the stick @param stones -- the stones @param helps -- the helps @param ball -- the left ball that it is used in the animation @param bar -- the grey bar that it is used in the animation @param rightBall -- the right ball that it is used in the animation @param level -- the challenge level @param game -- the game """ self.fileName = fileName self.music = Sound() self.music.setFileName("sound/music/" + fileName) self.section = Section() self.section.setFileName("sound/section/" + fileName) self.section.setKeys(keys) self.stick = stick self.animation = Animation(stones, keys, helps, ball, rightBall, bar, game) self.ball = ball self.level = level self.attempts = 0 self.game = game def start(self): """ Start a challenge @param self -- the challenge """ if os.path.exists("sound/section/" + self.fileName + ".wav"): self.section.play() self.game.statusBar.setText(_("Discover the next musical note")) self.animation.playIntroduction() self.ball.setSound(self.fileName) def change(self): """ Change a challenge @param self -- the challenge """ if os.path.exists("sound/section/" + self.fileName + ".wav"): self.section.play() self.game.statusBar.setText(_("Discover the next musical note")) self.animation.playIntroduction() self.ball.setSound(self.fileName) def finish(self): """ Finish a challenge @param self -- the challenge """ if os.path.exists("sound/music/" + self.fileName + ".ogg"): self.music.play() self.game.currentChallenge = None self.game.statusBar.setText(_("Congratulations!!! You completed the challenge! Choose other to continue playing.")) self.stick.setVisible(False) self.animation.playClosing() self.game.setPoints(self.level, self.attempts) def attempt(self, id): """ Verify a attempt @param self -- the challenge @param id -- the key index """ if self.section.isCurrentKey(id): self.game.setBufferPoints(self.level, self.attempts) self.attempts = 0 if self.section.isLastKey(): self.finish() else: self.animation.lowStone(self.section.currentKey) self.section.currentKey = self.section.currentKey + 1 self.game.statusBar.setText(_("Discover the next musical note")) else: self.attempts = self.attempts + 1 if self.section.moreThan(id): self.game.statusBar.setText(_("Try a lower note")) else: self.game.statusBar.setText(_("Try a higher note")) def getBall(self): """ Get the ball @param self -- the challenge """ return self.ball
class Stone(Image): def __init__(self, fileName, x, y, polygon, game): """ Initialize a Stone @param self -- the stone @param fileName -- a string that contains the image name @param x -- a horizontal position of stone @param y -- a vertical position of stone @param polygon -- a list of (x,y) pairs @param game -- the game """ self.image = pygame.image.load("image/" + fileName + ".png") self.image.set_alpha(None) # disable alpha. self.image.convert() self.image.set_colorkey((255, 0, 255)) # magenta self.fileName = fileName self.x = x self.y = y self.polygon = polygon self.game = game self.isHover = False self.isDown = False self.isVisible = False self.mouseInside = False def setSound(self, id): """ Set a stone sound @param self -- the stone @param id -- a number that represents the ball note """ self.id = id self.sound = Sound() self.sound.setFileName("sound/notes/T" + str(self.id + 3)) def mouseHover(self): """ When the mouse is hover on the stone @param self -- the stone """ self.isHover = True self.game.setEarCursorVisible(True) self.game.setHelp() if self.game.lampIsSelected(): self.game.setLampCursorVisible(False) self.button.mouseHover() self.help.setVisible(True) else: if os.path.exists("sound/notes/T" + str(self.id + 3) + ".ogg"): self.sound.play() def mouseLeave(self): """ When the mouse leaves the stone @param self -- the stone """ self.isHover = False self.game.setEarCursorVisible(False) if self.game.lampIsSelected(): self.game.setLampCursorVisible(True) self.button.mouseLeave() self.help.setVisible(False) def draw(self, screen): """ Draw the stone on the screen @param self -- the stone @param screen -- the screen where the images will be showed """ if self.isVisible: screen.blit(self.image, (self.x, self.y)) def setButton(self, button): """ Set a related button @param self -- the stone @param button -- it relates a button with the stone """ self.button = button def setHelp(self, help): """ Set a related help @param self -- the stone @param help -- it relates a help with the stone """ self.help = help