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
 17from __future__ import annotations
 18
 19import typing
 20
 21import arcade
 22
 23
 24SCREEN_WIDTH = 800
 25SCREEN_HEIGHT = 600
 26SCREEN_TITLE = "Sound Speed Demo"
 27BUTTON_SIZE = 30
 28
 29
 30SPEED_VARIATION = [0.1, 0.5, 1.0, 2.0, 4.0]
 31BUTTON_X_POSITIONS = [
 32    BUTTON_SIZE,
 33    SCREEN_WIDTH / 4,
 34    SCREEN_WIDTH / 2,
 35    SCREEN_WIDTH / 4 * 3,
 36    SCREEN_WIDTH - BUTTON_SIZE,
 37]
 38
 39
 40VOLUME_VARIATION = [0.1, 0.5, 1]
 41Y_OFFSETS = [50, 0, -50]
 42
 43
 44class SoundButton(arcade.SpriteSolidColor):
 45    """
 46    A sprite that stores settings about how to play a sound.
 47
 48    You can tell it to play a sound faster or slower, as well as adjust
 49    the volume of the sound.
 50    """
 51
 52    def __init__(self, sound_file, speed, volume, center_x, center_y):
 53        super().__init__(BUTTON_SIZE, BUTTON_SIZE, color=arcade.color.WHITE)
 54        self.sound = arcade.Sound(sound_file)
 55        self.speed = speed
 56        self.volume = volume
 57        self.center_x = center_x
 58        self.center_y = center_y
 59
 60    def play(self):
 61        self.sound.play(speed=self.speed, volume=self.volume)
 62
 63
 64class MyGame(arcade.Window):
 65    def __init__(self, width, height, title):
 66        super().__init__(width, height, title)
 67
 68        self.background_color = arcade.color.AMAZON
 69        self.button_sprites = None
 70
 71    def setup(self):
 72        self.button_sprites = arcade.SpriteList()
 73
 74        # Position the grid of buttons
 75        # The zip function takes pieces from iterables and returns them
 76        # as tuples. For more information, you can see the python doc:
 77        # https://docs.python.org/3/library/functions.html#zip
 78        for vol, y_offset in zip(VOLUME_VARIATION, Y_OFFSETS):
 79            for speed, x_pos in zip(SPEED_VARIATION, BUTTON_X_POSITIONS):
 80                self.button_sprites.append(
 81                    SoundButton(
 82                        ":resources:sounds/gameover3.wav",
 83                        speed,
 84                        vol,
 85                        x_pos,
 86                        SCREEN_HEIGHT / 2 + y_offset,
 87                    )
 88                )
 89
 90    def on_draw(self):
 91        self.clear()
 92        self.button_sprites.draw()
 93
 94    def on_update(self, delta_time):
 95        self.button_sprites.update()
 96
 97    def on_mouse_press(self, x, y, button, key_modifiers):
 98        hit_sprites = arcade.get_sprites_at_point((x, y), self.button_sprites)
 99        for sprite in hit_sprites:
100            button_sprite = typing.cast(SoundButton, sprite)
101            if button == arcade.MOUSE_BUTTON_LEFT:
102                button_sprite.play()
103
104
105def main():
106    game = MyGame(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_TITLE)
107    game.setup()
108    arcade.run()
109
110
111if __name__ == "__main__":
112    main()