LittleCoder

Ideas 2: Making Noises

We're assuming that you've got LittleCoder up and running on your computer. If that's not the case and you're having any kind of trouble, please email me post haste! My email address is hhausman ({at}) gmail.com. I can get you all set up.

LittleCoder is all about experimentation. Just try things, see what happens. Stuff will probably explode, but thats okay! These ideas are here in case you run out. Each one is a complete LittleCoder program. All you have to do is replace the code inside the 'main.rb' file with the code here, save the main.rb file (important) and restart LittleCoder.

All these ideas use some funny sounds I made. You're going to need these sounds if you want to try these ideas out:

Here are all the sounds you need


Grab that, and unzip it next to LittleCoder.exe. It's critical that the folder is right next to the LittleCoder.exe, so that the paths in the code here will point to the sound files.

Yeah, so, on to the ideas:
Scene.new_sound( "sounds/beep.ogg" ).play
That's it! Thats a one line LittleCoder program that makes a noise. Fun? Yes!

Lets try something else:
$beep_sound = Scene.new_sound( "sounds/beep.ogg" )
$beep_sound.play
That's basically the same program, except now we've given the sound a name. The name starts with a dollar sign ($) because we're telling Ruby that this name is very valuable and should never be lost. Ruby respects that, and keeps the name around always, so you can get it anywhere. Sweet!

Next:
$beep_sound = Scene.new_sound( "sounds/beep.ogg" )

def keyboard( in_key, in_down_flag )
$beep_sound.play
end
This one is a bit more complex. We're defining a function there, called keyboard. If you define a function called keyboard, LittleCoder will call that function when someone presses (or releases) a key on the keyboard. Try running this program and pressing some keys. ha! Thats fun.

More:
$beep_sound = Scene.new_sound( "sounds/beep.ogg" )
$a_sound = Scene.new_sound( "sounds/a.ogg" )

def keyboard( in_key, in_down_flag )
if( in_key == 'a' )
$a_sound.play
else
$beep_sound.play
end
end
When LittleCoder calls your keyboard function, gives you which key was hit in the in_key variable. Here, we're creating two sounds, and playing them conditionally depending on which key the user has pressed.

Can you figure out how to hear the 'a' noise?

Try to load some of the other sounds, and play them.

All right. This ones a bit freaky, but fun to play with:
$sounds = {}
('a'..'z').each do |letter|
$sounds[letter] = Scene.new_sound( "sounds/#{letter}.ogg" )
end

def keyboard( in_key, in_down_flag )
return unless in_down_flag

if( $sounds[ in_key ] )
$sounds[ in_key ].play
end
end
This idea uses some slightly more advanced Ruby code, but I bet you can figure out how it works.

Okay. That's it for now. Hopefully these ideas have been interesting/informative. If you've got any questions or comments, I'm all ears. (: