Otter Example: Sound and Music
Another Otter example coming fresh off the ovens! This one marks the 14th example, and covers playing sound and music using the framework. The full example is right here, and here's a glimpse at the codes:
So easy!
Now there's almost nothing holding you back from making a video game using Otter if you so choose. There's still a handful of examples to go, but this one I believe marks the last in the series that is necessary to make a complete thing. Input, graphics, collision, and sound should cover a pretty large portion of making a game, right?
using Otter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SoundStuff {
class Program {
static void Main(string[] args) {
// Create a Game.
var game = new Game("Sound and Music");
// Start the game with a new SoundScene.
game.Start(new SoundScene());
}
}
class SoundScene : Scene {
// Load the sounds into individual Sound objects.
public Sound SoundOne = new Sound("sound1.wav");
public Sound SoundTwo = new Sound("sound2.wav");
public Sound SoundThree = new Sound("sound3.wav");
// Load the music into a Music object.
public Music Music = new Music("music.ogg");
public override void Update() {
base.Update();
// Play sounds for the 1, 2, and 3 keys being pressed.
if (Input.KeyPressed(Key.Num1)) {
SoundOne.Play();
}
if (Input.KeyPressed(Key.Num2)) {
SoundTwo.Play();
}
if (Input.KeyPressed(Key.Num3)) {
SoundThree.Play();
}
// Pause or play the music on the spacebar being pressed.
if (Input.KeyPressed(Key.Space)) {
if (Music.IsPlaying) {
Music.Pause();
}
else {
Music.Play();
}
}
// Adjust the volume of the sounds with up and down keys.
if (Input.KeyDown(Key.Up)) {
Sound.GlobalVolume += 0.02f;
}
if (Input.KeyDown(Key.Down)) {
Sound.GlobalVolume -= 0.02f;
}
// Adjust the volume of the music with left and right keys.
if (Input.KeyDown(Key.Right)) {
Music.GlobalVolume += 0.02f;
}
if (Input.KeyDown(Key.Left)) {
Music.GlobalVolume -= 0.02f;
}
}
}
}
Now there's almost nothing holding you back from making a video game using Otter if you so choose. There's still a handful of examples to go, but this one I believe marks the last in the series that is necessary to make a complete thing. Input, graphics, collision, and sound should cover a pretty large portion of making a game, right?
No Comments