Sound Speed Demo#

Screen shot of using the sound speed demo
sound_speed_demo.py#
  1"""
  2Sound Speed Demo
  3
  4If Python and Arcade are installed, this example can be run from the
  5command line with:
  6python -m arcade.examples.sound_speed_demo
  7
  8Left click a button to play a sound.
  9
 10Each button plays the same sound sample in a slightly different way.
 11
 12The middle buttons play at normal speed. The ones to the left play
 13slower, and the ones to the right play faster. The buttons higher on
 14the screen are quieter, while the ones further down are louder.
 15"""
 16
 17import typing
 18
 19import arcade
 20
 21
 22SCREEN_WIDTH = 800
 23SCREEN_HEIGHT = 600
 24SCREEN_TITLE = "Sound Speed Demo"
 25BUTTON_SIZE = 30
 26
 27
 28SPEED_VARIATION = [0.1, 0.5, 1.0, 2.0, 4.0]
 29BUTTON_X_POSITIONS = [
 30    BUTTON_SIZE,
 31    SCREEN_WIDTH / 4,
 32    SCREEN_WIDTH / 2,
 33    SCREEN_WIDTH / 4 * 3,
 34    SCREEN_WIDTH - BUTTON_SIZE,
 35]
 36
 37
 38VOLUME_VARIATION = [0.1, 0.5, 1]
 39Y_OFFSETS = [50, 0, -50]
 40
 41
 42class SoundButton(arcade.SpriteSolidColor):
 43    """
 44    A sprite that stores settings about how to play a sound.
 45
 46    You can tell it to play a sound faster or slower, as well as adjust
 47    the volume of the sound.
 48    """
 49
 50    def __init__(self, sound_file, speed, volume, center_x, center_y):
 51        super().__init__(BUTTON_SIZE, BUTTON_SIZE, color=arcade.color.WHITE)
 52        self.sound = arcade.Sound(sound_file)
 53        self.speed = speed
 54        self.volume = volume
 55        self.center_x = center_x
 56        self.center_y = center_y
 57
 58    def play(self):
 59        self.sound.play(speed=self.speed, volume=self.volume)
 60
 61
 62class MyGame(arcade.Window):
 63    def __init__(self, width, height, title):
 64        super().__init__(width, height, title)
 65
 66        self.background_color = arcade.color.AMAZON
 67        self.button_sprites = None
 68
 69    def setup(self):
 70        self.button_sprites = arcade.SpriteList()
 71
 72        # Position the grid of buttons
 73        # The zip function takes pieces from iterables and returns them
 74        # as tuples. For more information, you can see the python doc:
 75        # https://docs.python.org/3/library/functions.html#zip
 76        for vol, y_offset in zip(VOLUME_VARIATION, Y_OFFSETS):
 77            for speed, x_pos in zip(SPEED_VARIATION, BUTTON_X_POSITIONS):
 78                self.button_sprites.append(
 79                    SoundButton(
 80                        ":resources:sounds/gameover3.wav",
 81                        speed,
 82                        vol,
 83                        x_pos,
 84                        SCREEN_HEIGHT / 2 + y_offset,
 85                    )
 86                )
 87
 88    def on_draw(self):
 89        self.clear()
 90        self.button_sprites.draw()
 91
 92    def on_update(self, delta_time):
 93        self.button_sprites.update()
 94
 95    def on_mouse_press(self, x, y, button, key_modifiers):
 96        hit_sprites = arcade.get_sprites_at_point((x, y), self.button_sprites)
 97        for sprite in hit_sprites:
 98            button_sprite = typing.cast(SoundButton, sprite)
 99            if button == arcade.MOUSE_BUTTON_LEFT:
100                button_sprite.play()
101
102
103def main():
104    game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
105    game.setup()
106    arcade.run()
107
108
109if __name__ == "__main__":
110    main()