1
+ from cocos .audio .pygame import mixer
2
+ from cocos .audio .pygame .mixer import Sound
3
+ from cocos .director import director
4
+ from cocos .menu import Menu , CENTER , MenuItem , shake , shake_back
5
+ from cocos .scene import Scene
6
+ from pyglet .app import exit
7
+
8
+ # Here we will make a simple menu that allows you to play/pause a song, and to increase or decrease its volume
9
+ # As you'll see, Cocos makes writing menus EXTREMELY easy. Like I probably should have put this in beginner easy
10
+
11
+
12
+ # To begin, we have to make a wrapper for the Sound class (remember this?)
13
+ class Audio (Sound ):
14
+ def __init__ (self , audio_file ):
15
+ super (Audio , self ).__init__ (audio_file )
16
+
17
+
18
+ # Now here's the new part
19
+ # We make a class that extends Cocos's Menu class
20
+ class AudioMenu (Menu ):
21
+ def __init__ (self ):
22
+ # When we initialize the super class, we can to pass in a title for our awesome menu, that will be displayed above it
23
+ super (AudioMenu , self ).__init__ ("AUDIO CONTROL" )
24
+ # if you want to add your own, personalized text, feel free to pass nothing in
25
+
26
+ # Then we simply set the vertical and horizontal alignment (in this case center for both)
27
+ self .menu_valign = CENTER
28
+ self .menu_halign = CENTER
29
+ # The cocos.menu file comes with many more different positions you can mix and match to get what you like
30
+
31
+ # Next we need to make our menu items
32
+ # This is the important part!
33
+ # What we do is create a list filled with MenuItem objects
34
+ menu_items = [
35
+ # Each MenuItem has text to display, and a callback method
36
+ # The callback method we pass in gets called when a user clicks on the MenuItem
37
+ (MenuItem ("Play/Pause" , self .play_music )),
38
+ (MenuItem ("Increase Volume" , self .volume_up )),
39
+ (MenuItem ("Decrease Volume" , self .volume_down )),
40
+ (MenuItem ("Exit" , self .on_quit ))
41
+ # Pretty easy I'd say
42
+ ]
43
+
44
+ # Now let's set the song we want them to hear (good god I love me some Latin Industries)
45
+ self .song = Audio ("assets/sound/LatinIndustries.wav" )
46
+
47
+ # Then we simply create the menu using the create_menu method and passing in our MenuItem list
48
+ self .create_menu (menu_items )
49
+
50
+ # And I set the is_playing status to False, which we will access for our Play/Pause functionality
51
+ self .is_playing = False
52
+
53
+ # So, if you were paying attention, you'd notice that this is the first callback method we passed into the list
54
+ def play_music (self ):
55
+ # If it is playing, I stop the song and set the boolean to false
56
+ if self .is_playing :
57
+ self .song .stop ()
58
+ self .is_playing = False
59
+ # If not, I need to play the song (indefinitely I might add), and then set the boolean value to true
60
+ else :
61
+ self .song .play (- 1 )
62
+ self .is_playing = True
63
+
64
+ # Now for the volume up functionality
65
+ def volume_up (self ):
66
+ # First I grab the current volume of the song
67
+ volume = self .song .get_volume ()
68
+ # Then I set the volume to that volume, but I add just a smidgen to it (remember that volume is from 0.0 - 1.0)
69
+ self .song .set_volume (volume + .1 )
70
+
71
+ # And the exact opposite for volume down
72
+ def volume_down (self ):
73
+ volume = self .song .get_volume ()
74
+ self .song .set_volume (volume - .1 )
75
+
76
+ # IMPORTANT!
77
+ # All your menus must have this on_quit function or they will not exit even if you press escape
78
+ # (That means some fun Control+Alt+Tabing for you Windows users)
79
+ def on_quit (self ):
80
+ # It seems odd that I need a function that literally just calls the exit function but...
81
+ exit ()
82
+ # You've gotta do what you've gotta do
83
+
84
+
85
+ # Then we initialize the mixer (I bet you forgot about this!)
86
+ mixer .init ()
87
+ # And initialize the director like usual
88
+ director .init (resizable = True )
89
+ # Finally, we run the scene
90
+ director .run (Scene (AudioMenu ()))
91
+
92
+ # ... Yep this definitely should have been in beginner
0 commit comments