quicksilver 0.3.2

A simple game framework for 2D games in pure Rust
// Play a sound when a button is clicked
extern crate quicksilver;

use quicksilver::{
    Result,
    geom::{Rectangle, Shape, Vector},
    graphics::{Background::Col, Color},
    input::{ButtonState, MouseButton},
    lifecycle::{Asset, Settings, State, Window, run},
    sound::Sound
};

struct SoundPlayer {
    asset: Asset<Sound>,
}

const BUTTON_AREA: Rectangle = Rectangle {
    pos:  Vector {x: 350.0, y: 250.0},
    size: Vector {x: 100.0, y: 100.0}
};

impl State for SoundPlayer {
    fn new() -> Result<SoundPlayer> {
        let asset = Asset::new(Sound::load("boop.ogg"));
        Ok(SoundPlayer { asset })
    }

    fn update(&mut self, window: &mut Window) -> Result<()> {
        self.asset.execute(|sound| {
            if window.mouse()[MouseButton::Left] == ButtonState::Pressed
                && BUTTON_AREA.contains(window.mouse().pos()) {
                sound.play()?;
            }
            Ok(())
        })
    }

    fn draw(&mut self, window: &mut Window) -> Result<()> {
        window.clear(Color::WHITE)?;
        // If the sound is loaded, draw the button
        self.asset.execute(|_| {
            window.draw(&BUTTON_AREA, Col(Color::BLUE));
            Ok(())
        })
    }
}

fn main() {
    run::<SoundPlayer>("Sound example", Vector::new(800, 600), Settings::default());
}