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

Source Code for Script script-demo_scratchMyBot_py

 1  from joy import * 
 2   
3 -class ScrMyBot(JoyApp):
4 - def __init__(self):
5 JoyApp.__init__(self,scr={},robot=dict(count=1)) 6 self.gain = 100 7 self.limit = 5000
8
9 - def onStart( self ):
10 self.aNode = self.robot.values()[0]
11
12 - def onEvent( self, evt ):
13 if evt.type==SCRATCHUPDATE and evt.scr==0: 14 if evt.var=='robotPos1': 15 pos = evt.value * self.gain 16 if pos>self.limit: pos = self.limit 17 elif pos<-self.limit: pos = -self.limit 18 self.aNode.set_pos( pos ) 19 elif evt.type != TIMEREVENT: 20 JoyApp.onEvent(self,evt)
21 22 if __name__=="__main__": 23 print ''' 24 DEMO: connecting Scratch sprite to robot 25 ---------------------------------------- 26 27 When this program runs, the x position of the cat sprite on 28 the screen controls a servo in the robot. 29 30 ''' 31 smb = ScrMyBot() 32 smb.run() 33