Script demo_joyToScratch_py
[hide private]
[frames] | no frames]

Source Code for Script script-demo_joyToScratch_py

 1  from joy import * 
 2   
3 -class JoyToScr(JoyApp):
4 - def __init__(self):
5 JoyApp.__init__(self,scr={}) 6 self.speed = 10
7
8 - def onStart( self ):
9 self.scr.sensorUpdate( j0ax1=0, j0ax0=0 ) 10 self.scr.broadcast( 'j0btn0', 'j0btn1' )
11
12 - def onEvent( self, evt ):
13 if evt.type==JOYAXISMOTION and evt.joy==0: 14 if evt.axis==0: 15 self.scr.sensorUpdate( j0ax0=int(evt.value*self.speed) ) 16 elif evt.axis==1: 17 self.scr.sensorUpdate( j0ax1=-int(evt.value*self.speed) ) 18 elif evt.type==JOYBUTTONDOWN and evt.joy==0: 19 if evt.button==0: 20 self.scr.broadcast( 'j0btn0' ) 21 elif evt.button==1: 22 self.scr.broadcast( 'j0btn1' ) 23 elif evt.type != TIMEREVENT: 24 JoyApp.onEvent(self,evt)
25 26 if __name__=="__main__": 27 print ''' 28 DEMO: connecting joystick to Scratch 29 ------------------------------------ 30 31 When this program runs, axes 0 and 1 of joystick 0 are mapped to 32 Scratch sensors j0ax0 and j0ax1; joystick buttons 0 and 1 are 33 mapped to events j0btn0 and j0btn1 34 35 ''' 36 j2s = JoyToScr() 37 j2s.run() 38