+++ /dev/null
-'''\r
-Created on 23 juil. 2009\r
-\r
-@author: Samuel Benveniste\r
-'''\r
-from math import floor, ceil\r
-import pygame\r
-import pygame.midi\r
-import sys\r
-import colorsys\r
-import constants\r
-from gradients import gradients\r
-from logging.PickleableEvent import PickleableEvent\r
-from instruments.Instrument import Instrument\r
-from cursor.WarpingCursor import *\r
-from controllers.Wiimote import Wiimote\r
-from logging.EventLog import EventLog\r
-\r
-\r
-class StaticFamiliarizer:\r
- '''\r
- The screen on which the game is played\r
- \r
- wiimotes: \r
- The wiimotes used in this session\r
- window:\r
- The main display window\r
- screen:\r
- The main display surface\r
- clock:\r
- The clock used to animate the screen\r
- savedScreen:\r
- The background that is painted every time\r
- playerScreen:\r
- The buffer for painting everything before bliting\r
- width:\r
- The width of the window in pixels\r
- height:\r
- The height of the window in pixels\r
- extendScale :\r
- True if the scale is G to C instead of C to C\r
- cascade:\r
- True if crossing from note to note with a button pressed triggers a new note\r
- scaleSize:\r
- The size of the scale used\r
- cursorPositions:\r
- The positions of the cursors on the screen, in pixels\r
- '''\r
- \r
- \r
- \r
- def __init__(self, wiimotes, window, screen, clock, joys, portOffset,activeWiimotes,replay = False, level = 0, defaultInstrumentChannel = 16, defaultNote = 60, eventLog = None):\r
- '''\r
- Constructor\r
- '''\r
- self.firstClickTime = None\r
- self.firstClickInTime = None\r
- self.duration = None\r
- self.clicks = 0\r
- self.clicksIn = 0\r
- \r
- pygame.font.init()\r
- self.font = pygame.font.Font(None,60)\r
- self.congratulations = ["Bien !","Tres Bien !","Bravo !","Excellent !","Felicitations !"]\r
- self.renderedCongratulations = [self.font.render(congratulation,False,(0,0,0)) for congratulation in self.congratulations]\r
- self.congratulationCount = None\r
- self.isCongratulating = False\r
- self.congratulationTimer = 0\r
- self.congratulationLength = 2000\r
- self.congratulationPos = None\r
- \r
- self.blinkLength = 200\r
- self.minimalVelocity = 64\r
- self.shortScaleSize = 8\r
- self.longScaleSize = 11\r
- self.borderSize = 5\r
- self.savedHighlightedNote = 0\r
- self.scaleFactor = 1\r
- self.wiimotes = wiimotes\r
- self.window = window\r
- self.screen = screen\r
- self.clock = clock\r
- self.width = int(floor(screen.get_width()*self.scaleFactor))\r
- self.height = int(floor(screen.get_height()*self.scaleFactor))\r
- self.blitOrigin = ((self.screen.get_width()-self.width)/2,(self.screen.get_height()-self.height)/2) \r
- self.joys = joys\r
- self.portOffset = portOffset\r
- self.savedScreen = pygame.Surface(self.screen.get_size())\r
- self.savedScreen.fill((255,255,255))\r
- self.playerScreen = pygame.Surface(self.savedScreen.get_size())\r
- self.playerScreen.blit(self.savedScreen, (0, 0))\r
- self.cursorPositions = []\r
- self.level = level\r
- self.nextLevel = None\r
- self.activeWiimotes = activeWiimotes\r
- \r
- for i in range(len(self.wiimotes)):\r
- #Set the screen for the cursors (it can't be set before)\r
- self.wiimotes[i].cursor.screen = self.playerScreen\r
- self.cursorPositions.append(self.wiimotes[i].cursor.centerPosition)\r
- \r
- if eventLog == None:\r
- self.eventLog = EventLog()\r
- self.replay = False\r
- else:\r
- self.eventLog = eventLog\r
- self.replay = replay\r
- \r
- self.defaultInstrumentChannel = defaultInstrumentChannel\r
- self.defaultNote = defaultNote\r
- \r
- self.done = False\r
- self.backToInstrumentChoice = False\r
- self.easyMode = False\r
-\r
- self.noteRects = []\r
- self.boundingRect = None\r
- self.notes = []\r
- self.buttonDown = []\r
- self.velocityLock = []\r
- \r
- self.drawBackground()\r
- self.initializeWiimotes()\r
- events = pygame.event.get()\r
- \r
- #The main loop\r
- while not self.done :\r
- \r
- self.playerScreen.blit(self.savedScreen, (0, 0))\r
- \r
- # Limit frame speed to 50 FPS\r
- #\r
- timePassed = self.clock.tick(10000)\r
- \r
- if self.replay:\r
- self.eventLog.update(timePassed)\r
- pickledEventsToPost = self.eventLog.getPickledEvents() \r
- for pickledEvent in pickledEventsToPost:\r
- pygame.event.post(pickledEvent.event)\r
- \r
- events = pygame.event.get()\r
- \r
- if not self.replay:\r
- pickledEvents = [PickleableEvent(event.type,event.dict) for event in events]\r
- if pickledEvents != [] :\r
- self.eventLog.appendEventGroup(pickledEvents)\r
- \r
- for event in events:\r
- self.input(event)\r
- \r
- if self.isCongratulating :\r
- self.congratulationTimer += timePassed\r
- if self.congratulationTimer < self.congratulationLength :\r
- self.blitCongratulation()\r
- else :\r
- self.isCongratulating = False\r
- \r
- for i in range(len(self.wiimotes)):\r
- if self.activeWiimotes[i]:\r
- self.wiimotes[i].cursor.update(timePassed, self.cursorPositions[i])\r
- if self.buttonDown[i] :\r
- self.wiimotes[i].cursor.flash()\r
- self.wiimotes[i].cursor.blit(self.playerScreen)\r
- \r
- self.screen.blit(self.playerScreen, (0,0))\r
- \r
- pygame.display.flip()\r
- \r
- for i in range(len(self.wiimotes)):\r
- if self.activeWiimotes[i]:\r
- if self.notes[i] != None :\r
- self.wiimotes[i].stopNote(self.notes[i])\r
- if self.replay :\r
- self.duration = self.eventLog.getCurrentTime() \r
- \r
- def drawBackground(self):\r
- self.savedScreen.fill((255,255,255))\r
- if self.level == 0 :\r
- A = [4] \r
- else :\r
- A = [1,7]\r
- \r
- self.noteRects = [pygame.Rect(i * self.width / 11+self.blitOrigin[0], self.blitOrigin[1], (self.width / 11 + 1)*3, self.height+1) for i in A]\r
- \r
- #create bounding rect\r
- self.boundingRect = self.noteRects[0].unionall(self.noteRects)\r
- \r
- #fill the rectangles with a color gradient\r
- #We start with blue\r
- startingHue = 0.66666666666666663\r
- \r
- for rectNumber in range(len(self.noteRects)) :\r
- colorRatio = float(A[rectNumber]) / (11 - 1)\r
- #hue will go from 0.6666... (blue) to 0 (red) as colorRation goes up\r
- hue = startingHue * (1 - colorRatio)\r
- #The color of the bottom of the rectangle in hls coordinates\r
- bottomColorHls = (hue, 0.6, 1)\r
- #The color of the top of the rectangle in hls coordinates\r
- topColorHls = (hue, 0.9, 1)\r
- \r
- #convert to rgb ranging from 0 to 255\r
- bottomColorRgb = [floor(255 * i) for i in colorsys.hls_to_rgb(*bottomColorHls)]\r
- topColorRgb = [floor(255 * i) for i in colorsys.hls_to_rgb(*topColorHls)]\r
- #add transparency\r
- bottomColorRgb.append(255)\r
- topColorRgb.append(255)\r
- #convert to tuple\r
- bottomColorRgb = tuple(bottomColorRgb)\r
- topColorRgb = tuple(topColorRgb) \r
- \r
- self.savedScreen.blit(gradients.vertical(self.noteRects[rectNumber].size, topColorRgb, bottomColorRgb), self.noteRects[rectNumber])\r
- \r
- pygame.draw.rect(self.savedScreen, pygame.Color(0, 0, 0, 255), self.noteRects[rectNumber], 2)\r
- \r
- def initializeWiimotes(self):\r
- for loop in self.wiimotes:\r
- if loop.port == None :\r
- loop.port = pygame.midi.Output(loop.portNumber)\r
- self.notes.append(0)\r
- self.buttonDown.append(False)\r
- self.velocityLock.append(False)\r
- \r
- def updateCursorPositionFromJoy(self, joyEvent):\r
- joyName = pygame.joystick.Joystick(joyEvent.joy).get_name()\r
- correctedJoyId = constants.joyNames.index(joyName)\r
- if correctedJoyId < len(self.cursorPositions):\r
- if joyEvent.axis == 0 :\r
- self.cursorPositions[correctedJoyId] = (int((joyEvent.value + 1) / 2 * self.screen.get_width()), self.cursorPositions[correctedJoyId][1])\r
- if joyEvent.axis == 1 :\r
- self.cursorPositions[correctedJoyId] = (self.cursorPositions[correctedJoyId][0], int((joyEvent.value + 1) / 2 * self.screen.get_height()))\r
- \r
- def heightToVelocity(self, pos, controllerNumber):\r
- velocity = int(floor((1 - (float(pos[1])-self.blitOrigin[1]) / self.height) * (127-self.minimalVelocity))+self.minimalVelocity)\r
- return(velocity)\r
- \r
- def widthToNote(self, pos):\r
- nn = 0\r
- try :\r
- while self.noteRects[nn].collidepoint(pos) == False:\r
- nn = nn + 1\r
- return(nn)\r
- except(IndexError):\r
- return(None)\r
- \r
- def congratulate(self,targetRect,posy):\r
- if self.congratulationCount != None :\r
- if self.congratulationCount < len(self.congratulations)-1:\r
- self.congratulationCount += 1\r
- else :\r
- self.congratulationCount = 0\r
- self.congratulationTimer = 0\r
- self.congratulationPos = (targetRect.left+(targetRect.width-self.renderedCongratulations[self.congratulationCount].get_width())/2,posy)\r
- self.isCongratulating = True\r
- \r
- def resetCongratulation(self):\r
- self.congratulationCount = None\r
- self.congratulationPos = None\r
- self.isCongratulating = False\r
- \r
- def blitCongratulation(self):\r
- self.playerScreen.blit(self.renderedCongratulations[self.congratulationCount],self.congratulationPos) \r
- \r
- def input(self, event): \r
- \r
- print event\r
- \r
- if event.type == pygame.QUIT:\r
- for loop in self.wiimotes:\r
- del loop.port\r
- pygame.midi.quit()\r
- sys.exit(0) \r
- \r
- if event.type == pygame.KEYDOWN:\r
- if event.key == pygame.K_q:\r
- self.nextLevel = None\r
- self.done = True\r
- \r
- if event.key == pygame.K_w:\r
- self.nextLevel = 0\r
- self.done = True\r
- \r
- if event.key == pygame.K_e:\r
- self.nextLevel = 1\r
- self.done = True\r
- \r
- if event.key == pygame.K_r:\r
- self.nextLevel = 2\r
- self.done = True\r
- \r
- if event.key == pygame.K_t:\r
- self.nextLevel = 3\r
- self.done = True \r
- \r
- if event.type == pygame.JOYAXISMOTION:\r
- \r
- \r
- joyName = pygame.joystick.Joystick(event.joy).get_name()\r
- correctedJoyId = constants.joyNames.index(joyName)\r
- if self.activeWiimotes[correctedJoyId]:\r
- self.updateCursorPositionFromJoy(event)\r
- wiimote = self.wiimotes[correctedJoyId]\r
- pos = self.cursorPositions[correctedJoyId]\r
- \r
- if self.buttonDown[correctedJoyId]:\r
- wiimote.cursor.flash()\r
- if self.notes[correctedJoyId] != None:\r
- velocity = self.heightToVelocity(pos, correctedJoyId)\r
- CCHexCode = wiimote.getCCHexCode()\r
- wiimote.port.write_short(CCHexCode, 07, velocity)\r
- \r
- if event.type == pygame.JOYBUTTONDOWN :\r
- \r
- joyName = pygame.joystick.Joystick(event.joy).get_name()\r
- correctedJoyId = constants.joyNames.index(joyName)\r
- if self.activeWiimotes[correctedJoyId]:\r
- wiimote = self.wiimotes[correctedJoyId]\r
- pos = self.cursorPositions[correctedJoyId]\r
- wiimote.cursor.flash()\r
- if self.replay :\r
- self.clicks += 1\r
- if self.firstClickTime == None :\r
- self.firstClickTime = self.eventLog.getCurrentTime()\r
- \r
- if not self.buttonDown[correctedJoyId]:\r
- self.notes[correctedJoyId] = self.widthToNote(pos)\r
- \r
- velocity = self.heightToVelocity(pos, correctedJoyId)\r
- \r
- if self.notes[correctedJoyId] != None : \r
- wiimote.playNote(self.notes[correctedJoyId],velocity)\r
- self.congratulate(self.noteRects[self.notes[correctedJoyId]],pos[1])\r
- if self.replay :\r
- self.clicksIn += 1\r
- if self.firstClickInTime == None :\r
- self.firstClickInTime = self.eventLog.getCurrentTime()\r
- else :\r
- self.resetCongratulation()\r
- \r
- self.buttonDown[correctedJoyId] = True\r
- \r
- if event.type == pygame.JOYBUTTONUP:\r
- joyName = pygame.joystick.Joystick(event.joy).get_name()\r
- correctedJoyId = constants.joyNames.index(joyName)\r
- if self.activeWiimotes[correctedJoyId]:\r
- wiimote = self.wiimotes[correctedJoyId]\r
- wiimote.stopNote(self.notes[correctedJoyId])\r
- self.buttonDown[correctedJoyId] = False\r
- self.velocityLock[correctedJoyId] = False\r
- \r
- if event.type == pygame.MOUSEMOTION:\r
- \r
- self.updateCursorPositionFromMouse(event)\r
- \r
- correctedJoyId = 0\r
- while not self.activeWiimotes[correctedJoyId] :\r
- correctedJoyId += 1\r
- wiimote = self.wiimotes[correctedJoyId]\r
- pos = self.cursorPositions[correctedJoyId]\r
-\r
- if self.buttonDown[correctedJoyId]:\r
- wiimote.cursor.flash()\r
- if self.notes[correctedJoyId] != None:\r
- velocity = self.heightToVelocity(pos, correctedJoyId)\r
- CCHexCode = wiimote.getCCHexCode()\r
- wiimote.port.write_short(CCHexCode, 07, velocity) \r
- \r
- if event.type == pygame.MOUSEBUTTONDOWN:\r
- \r
- if event.button == 1:\r
- correctedJoyId = 0\r
- while not self.activeWiimotes[correctedJoyId] :\r
- correctedJoyId += 1\r
- wiimote = self.wiimotes[correctedJoyId]\r
- pos = self.cursorPositions[correctedJoyId]\r
- wiimote.cursor.flash()\r
- if self.replay :\r
- self.clicks += 1\r
- if self.firstClickTime == None :\r
- self.firstClickTime = self.eventLog.getCurrentTime()\r
- \r
- if not self.buttonDown[correctedJoyId]:\r
- self.notes[correctedJoyId] = self.widthToNote(pos)\r
- \r
- velocity = self.heightToVelocity(pos, correctedJoyId)\r
- \r
- if self.notes[correctedJoyId] != None : \r
- wiimote.playNote(self.notes[correctedJoyId],velocity)\r
- self.congratulate(self.noteRects[self.notes[correctedJoyId]],pos[1])\r
- if self.replay :\r
- self.clicksIn += 1\r
- if self.firstClickInTime == None :\r
- self.firstClickInTime = self.eventLog.getCurrentTime()\r
- else :\r
- self.resetCongratulation()\r
- \r
- self.buttonDown[correctedJoyId] = True\r
- \r
- if event.button == 2:\r
- \r
- self.done = True\r
- \r
- if event.type == pygame.MOUSEBUTTONUP:\r
- \r
- correctedJoyId = 0\r
- while not self.activeWiimotes[correctedJoyId] :\r
- correctedJoyId += 1\r
- wiimote = self.wiimotes[correctedJoyId]\r
- wiimote.stopNote(self.notes[correctedJoyId])\r
- self.buttonDown[correctedJoyId] = False\r
- self.velocityLock[correctedJoyId] = False\r
- \r
- def hasChanged(self):\r
- return(True)\r
- \r
- def updateCursorPositionFromMouse(self, mouseEvent):\r
- correctedJoyId = 0\r
- while not self.activeWiimotes[correctedJoyId] :\r
- correctedJoyId += 1\r
- self.cursorPositions[correctedJoyId] = mouseEvent.pos\r
- \r
-if __name__ == "__main__" :\r
- pygame.init()\r
- modeResolution = (1024,768)\r
- window = pygame.display.set_mode(modeResolution,pygame.FULLSCREEN)\r
- pygame.font.init()\r
- \r
- pygame.midi.init()\r
- instruments = [Instrument(constants.scaleDict["majorScale"], i + 1, "".join(["../instruments/instrumentImages/", constants.instrumentImagePathList[i], ".jpg"]), constants.octaves[i]) for i in range(9)]\r
- \r
- joys = [[id,pygame.joystick.Joystick(id).get_name()] for id in range(pygame.joystick.get_count())]\r
- for joy in joys:\r
- if joy[1] in constants.joyNames:\r
- pygame.joystick.Joystick(joy[0]).init() \r
- \r
- ports = [pygame.midi.get_device_info(id)[1] for id in range(pygame.midi.get_count())]\r
- portOffset = ports.index(constants.portNames[0])\r
- print(portOffset)\r
- \r
- events = pygame.event.get()\r
- \r
- screen = pygame.display.get_surface()\r
- clock = pygame.time.Clock() \r
- cursorImages = [createImageListFromPath('../cursor/cursorImages/black', 11),createImageListFromPath('../cursor/cursorImages/red', 11)]\r
- durations = [75 for i in range(len(cursorImages[0]))]\r
- \r
- wiimoteCount = 1\r
- cursors = [WarpingCursor(None, cursorImages[i], durations, (300 * i, 300 * i),flashImage = '../cursor/cursorImages/black/flash.png' ) for i in range(wiimoteCount)]\r
- wiimotes = [Wiimote(i, i + portOffset, None, instruments[i], cursors[i]) for i in range(wiimoteCount)]\r
- \r
- fam = StaticFamiliarizer(instruments, wiimotes, window, screen, clock, joys, portOffset)\r
- \r
- for loop in fam.wiimotes:\r
- del loop.port\r
- \r
- pygame.midi.quit()\r
- \r
- pygame.quit() \r