Commits

vfiack  committed a7c7b69

sfx for memory

  • Participants
  • Parent commits 6a61760

Comments (0)

Files changed (4)

File data/memory/sfx/bleep.wav

Binary file added.

File data/memory/sfx/success.wav

Binary file added.

File data/memory/sfx/turn.wav

Binary file added.

File memory/memory.ml

+module Sfx = struct
+  open Sdlmixer
+  
+  let turn = ref None
+  let bleep = ref None
+  let success = ref None
+  
+  let load () =
+    turn := Some (loadWAV "data/memory/sfx/turn.wav");
+    bleep := Some (loadWAV "data/memory/sfx/bleep.wav");
+    success := Some (loadWAV "data/memory/sfx/success.wav")
+  
+  let play sfxref = match !sfxref with
+    | None -> failwith "sound not initialized"
+    | Some sound -> Sdlmixer.play_channel ~channel:0 sound
+end
+
+
 module Image = struct
   open Sdlvideo
   open Sdlloader
     | MOUSEBUTTONDOWN event ->
         let x, y = event.mbe_x, event.mbe_y in
         begin match find_card x y cards with
-          | Some (card) -> card
+          | Some (card) -> (Sfx.play Sfx.turn; card)
           | None -> select_card cards
         end
     | _ -> select_card cards
       Sdlvideo.flip screen
     in
     match cards with
-    | [] -> () (* victory *)
+    | [] -> 
+      Sdltimer.delay 500;
+      Sfx.play Sfx.success;
+      Sdltimer.delay 1500;
     | _ ->
         show_backgrounds ();
         let first = select_card cards in
         show_card first;
         let second = select_card cards in
         show_card second;
-        let remaining = 
-          if first.img <> second.img then cards
+        let success = first.img = second.img in
+        let remaining =  
+          if not success then cards
           else List.filter (fun c -> c.img <> first.img) cards
         in
-        Sdltimer.delay 1500; 
+        Sdltimer.delay 1500;
+        if success then Sfx.play Sfx.bleep; 
         play screen remaining
 end
 
-let _ =
-  Random.self_init();
+let init_sdl width height =
   Sdl.init [`VIDEO; `AUDIO];
   at_exit Sdl.quit;
   Sdlmixer.open_audio ();
   at_exit Sdlmixer.close_audio;
   
   Sdlwm.set_caption "memory minigame" "";
-  
+  Sdlvideo.set_video_mode width height [`HWSURFACE;`DOUBLEBUF]
+
+let _ =
+  Random.self_init();  
   let width, height = 1024, 768 in
-  let screen = Sdlvideo.set_video_mode ~w: width ~h: height [`HWSURFACE;`DOUBLEBUF] in
-  
+  let screen = init_sdl width height in 
   Image.load ();
+  Sfx.load ();
   let count = 10 in
   let cards = get_cards width height count in
   try