Exemple #1
0
def main():
    # canvas = box(pos=vector(0, 0), size=(10,10), color=color.white)
    square = MagicBox(pos=vector(0, 0), size=(2, 2, 2), color=color.blue)
    ball = MagicSphere(pos=vector(2, 2), radius=0.5, color=color.green)
    
    deltat = 0.005
    
    jr = JoystickReader()
    # scene.autoscale = False
    
    while True:
        try:
            values = jr.interpret_values()
        except ValueError:
            continue
        except BadInputError as e:
            print(e)
            print('Remove and reconnect the joystick')
            time.sleep(3)
            main()
        
        rate(50)
        
        if values['button_pressed'] is True:
            square.color = color.red
        elif values['button_pressed'] is False:
            square.color = color.blue
        
        square.velocity = actor_velocity(values)
    
        if touch(square, ball):
            ball.color = color.orange
            impact = square.mass/ball.mass
            ball.velocity = vector(square.velocity.x/impact, square.velocity.y/impact)
            t = 0
            while t < 3:
                new_pos = ball.pos + ball.velocity*deltat
                if new_pos < vector(5, 5):
                    ball.pos = new_pos
                t += deltat
        else:
            ball.color = color.green
            square.pos = move(square.pos, square.velocity*deltat)
Exemple #2
0
def main():
    # initialise the serial port
    print('Initialising the serial port...')
    x_zero = 127
    y_zero = 127
    if args.calibrate:
        x_zero, y_zero = calibrate()
    if args.port:
        jr = JoystickReader(port=args.port, x_zero=x_zero, y_zero=y_zero)
    else:
        try:
            jr = JoystickReader()
        except BadPortError:
            connected = False
            while not connected:
                port = input('port: ')
                try:
                    jr = JoystickReader(port=port, x_zero=x_zero, y_zero=y_zero)
                except BadPortError as e:
                    print(e)
                    continue
                else:
                    connected = True
    
    
    pp = pprint.PrettyPrinter()
    
    print('Waiting for input.')
    
    last_values = {'x_steps': None, 'y_steps': None}
    
    while True:
        try:
            values = jr.interpret_values()
        # sometimes there is a random ValueError, whose origin I'm still not sure of
        except ValueError:
            continue
        # and sometimes the input data is also bad, and I also don't know why
        except BadInputError as e:
            print(e)
            print('Relaunch me because something went wrong.')
            break
        else:
            if last_values['x_steps'] != values['x_steps'] or last_values['y_steps'] != values['y_steps']:
                last_values = values
                
                if args.r:
                    pp.pprint(values)
                else:
                    button_pressed = values['button_pressed']
                
                    y_direction = values['y_direction']
                    y_steps = values['y_steps']
            
                    x_direction = values['x_direction']
                    x_steps = values['x_steps']
                
                
                    if button_pressed:
                        print('You pressed the button.')
                    if y_steps > 0:
                        print('Y is {} steps {}'.format(values['y_steps'],
                            values['y_direction']))
                    if x_steps > 0:
                        print('X is {} steps {}'.format(values['x_steps'],
                            values['x_direction']))
                    if not button_pressed and y_steps is 0 and x_steps is 0:    
                        print('All at rest')
            else:
                continue