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
21WINDOW_WIDTH = 1280
22WINDOW_HEIGHT = 720
23WINDOW_TITLE = "Sound Speed Demo"
24BUTTON_SIZE = 30
25
26
27SPEED_VARIATION = [0.1, 0.5, 1.0, 2.0, 4.0]
28MARGIN = WINDOW_WIDTH / 4
29BUTTON_X_POSITIONS = [
30 MARGIN,
31 MARGIN + (WINDOW_WIDTH - MARGIN * 2) / 3 * 1,
32 MARGIN + (WINDOW_WIDTH - MARGIN * 2) / 3 * 2,
33 MARGIN + (WINDOW_WIDTH - MARGIN * 2) / 3 * 3,
34 WINDOW_WIDTH - MARGIN,
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 def __init__(self, sound_file, speed, volume, center_x, center_y):
50 super().__init__(BUTTON_SIZE, BUTTON_SIZE, color=arcade.color.WHITE)
51 self.sound = arcade.Sound(sound_file)
52 self.speed = speed
53 self.volume = volume
54 self.center_x = center_x
55 self.center_y = center_y
56
57 def play(self):
58 self.sound.play(speed=self.speed, volume=self.volume)
59
60
61class GameView(arcade.View):
62 def __init__(self):
63 super().__init__()
64
65 self.background_color = arcade.color.AMAZON
66 self.button_sprites = arcade.SpriteList()
67
68 # Position the grid of buttons
69 # The zip function takes pieces from iterables and returns them
70 # as tuples. For more information, you can see the python doc:
71 # https://docs.python.org/3/library/functions.html#zip
72 for vol, y_offset in zip(VOLUME_VARIATION, Y_OFFSETS):
73 for speed, x_pos in zip(SPEED_VARIATION, BUTTON_X_POSITIONS):
74 self.button_sprites.append(
75 SoundButton(
76 ":resources:sounds/gameover3.wav",
77 speed,
78 vol,
79 x_pos,
80 WINDOW_HEIGHT / 2 + y_offset,
81 )
82 )
83
84 def on_draw(self):
85 self.clear()
86 self.button_sprites.draw()
87
88 def on_update(self, delta_time):
89 self.button_sprites.update()
90
91 def on_mouse_press(self, x, y, button, key_modifiers):
92 hit_sprites = arcade.get_sprites_at_point((x, y), self.button_sprites)
93 for sprite in hit_sprites:
94 button_sprite = typing.cast(SoundButton, sprite)
95 if button == arcade.MOUSE_BUTTON_LEFT:
96 button_sprite.play()
97
98
99def main():
100 """ Main function """
101 # Create a window class. This is what actually shows up on screen
102 window = arcade.Window(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE)
103
104 # Create the GameView
105 game = GameView()
106
107 # Show GameView on screen
108 window.show_view(game)
109
110 # Start the arcade game loop
111 arcade.run()
112
113
114if __name__ == "__main__":
115 main()