Play SoundΒΆ

Automatically generated code (generated by the behavior dialog):

# Play sound when left mouse button is pressed down.
rectangle1.bind(LEFTDOWN, system.evtHandler, PLAYSOUND,
           **{'sound': 'laser_gun', 'volume': 0.40, 'repeat': 2})

Manually created code (equivalent to the automatically generated code shown above):

def fireGun(event, source):
    system.playSound('laser', 'gun', 0.40, 2)

rectangle1.bind(LEFTDOWN, fireGun)

It is also possible to adjust volume or the sound itself depending on the collision speed or normal vector.

def fireGun(event, source):
    vol = event.velocity / 50
    system.playSound('laser', 'gun', vol, 2)

rectangle1.bind(COLLISION, fireGun, system.bottomWall)

Previous topic

Assign Sprite

Next topic

Running and postprocessing

This Page