Skip to content

Commit f39e336

Browse files
committed
Figured out Cocos2D Python audio
1 parent d9569a2 commit f39e336

File tree

7 files changed

+50
-6
lines changed

7 files changed

+50
-6
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,15 @@ On Mac, installation is made easy thanks to Homebrew. If you don't have it insta
3636
On other operating systems such as Windows or Linux distributions, head to http://www.scipy.org/install.html for more information on how to install for your particular operating system.
3737

3838
###AVbin
39-
AVbin is a package that provides audio and video support for Cocos2D Python.
39+
AVbin is a package that provides some audio and the video support for Cocos2D Python.
4040

4141
Head to https://avbin.github.io/AVbin/Download.html to download the correct installer for your operating system.
4242

43+
###SDL
44+
SDL is the framework that PyGame (another Python game library) relies on for audio support. Cocos uses it for it's audio as well.
45+
46+
Head over to https://www.libsdl.org/download-1.2.php to download the base SDL library and then to https://www.libsdl.org/projects/SDL_mixer/release-1.2.html to download the Mixer library from SDL (which was what we will be using for audio support in our games)
47+
4348
----------------------------------------------------------------------------
4449
Now that you're all set up you should be ready to install Cocos2D
4550

@@ -64,7 +69,7 @@ The code is grouped in folders by their difficulty level. Here is how it's mostl
6469
| examples
6570

6671

67-
From here you should be read to start reading through the samples and trying out the concepts yourself. I group the the code by difficulty level, so I recommend starting in the `basics` folder and working your way up.
72+
From here you should be ready to start reading through the samples and trying out the concepts yourself. I group the the code by difficulty level, so I recommend starting in the `basics` folder and working your way up.
6873

6974
It's important to note that `basics` does not mean Python basics! You should at least have a basic grasp on writing object oriented code before trying to jump into Cocos2D Python!
7075

basics/actions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
# I chose these import statements just to make my close look cleaner
2+
# I chose these import statements just to make my code look cleaner
33
# For this to work you would just need to import cocos and then add the subdirectory after
44
# Ex: self.sprite = Sprite('directory') would be self.sprite = cocos.sprite.Sprite('directory')
55
import cocos
33.8 MB
Binary file not shown.

basics/assets/sound/sound.wav

-1.66 MB
Binary file not shown.

basics/audio.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Again very similar inputs to what we had before
2+
from cocos import scene
3+
from cocos.layer import Layer
4+
from cocos.director import director
5+
6+
# Except for these imports
7+
# It might seem odd that it's importing from pygame, but really it's importing SDL libraries
8+
from cocos.audio.pygame.mixer import Sound
9+
from cocos.audio.pygame import mixer
10+
11+
12+
# Our first multi-class program! Nice
13+
# First we need to create a wrapper around the SDL Sound class
14+
# We do this by creating our own object that simply initializes the parent with a file name that we pass in
15+
# You'll see this in action in a second
16+
class Audio(Sound):
17+
def __init__(self, audio_file):
18+
# As stated above, we initialize the super class with the audio file we passed in
19+
super(Audio, self).__init__(audio_file)
20+
21+
22+
# Here we create your standard layer
23+
class AudioLayer(Layer):
24+
def __init__(self):
25+
super(AudioLayer, self).__init__()
26+
# Now, in the layer we create an Audio object from the class we described above
27+
song = Audio("assets/sound/LatinIndustries.wav")
28+
# And lastly we make the song play when the layer is initialized
29+
song.play(-1) # Setting the parameter to -1 makes the song play indefinitely
30+
31+
32+
# Now we have more things to initialize than just the director
33+
director.init()
34+
# The audio mixer also needs us to tell it to get ready!
35+
mixer.init()
36+
37+
# And lastly we run the scene like usual
38+
director.run(scene.Scene(AudioLayer()))

basics/basics.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ Here is the order which the tutorial follows:
88
3. Basic Actions (actions.py)
99
4. Keyboard Input (keyboard.py)
1010
5. Mouse Input (mouse.py)
11-
6. Applying User Input (input.py)
11+
6. Applying User Input (input.py)
12+
7. Audio Playback (audio.py)

basics/input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def on_mouse_press(self, x, y, buttons, modifiers):
5151

5252
# Once again we overload a default event handler
5353
def on_key_press(self, key, modifiers):
54-
# First I create a move actions because we programmers are lazy and hate having to retype code!
54+
# First I create a move action because we programmers are lazy and hate having to retype code!
5555
move_left = MoveBy((-50, 0), .5)
5656

5757
# Here's where that Pyglet symbol_string() function comes in handy
@@ -65,7 +65,7 @@ def on_key_press(self, key, modifiers):
6565
# I only wrote code for moving left, but I can use the Reverse() function instead of rewriting code
6666
# Reverse() simply tells Cocos to do the reverse action of whatever you pass into it.
6767
self.sprite.do(Reverse(move_left))
68-
68+
6969

7070
# And once again the same init code
7171
director.init()

0 commit comments

Comments
 (0)