Commits

Vincent Fiack committed e1d9805

now with sound!

Comments (0)

Files changed (4)

data/sfx/failure.wav

Binary file added.

data/sfx/step.wav

Binary file added.

data/sfx/success.wav

Binary file added.
 exception Break
 exception Escape
 
+(* drawings *)
+
 let tilesize = 32
 
 let draw_tile screen x y color =
   Sdlvideo.fill_rect ~rect: insiderect screen color';
   Sdlvideo.update_rect ~rect: fullrect screen
 
+let draw_dot screen color maze (x, y) =
+  let shift = tilesize / 2 in
+  let half = shift / 2 in
+  if Maze.get maze (x, y) <> '#' then
+    let color' = Sdlvideo.map_RGB screen color in
+    let rect = Sdlvideo.rect (x * tilesize + half) (y * tilesize + half) shift shift in
+    Sdlvideo.fill_rect ~rect: rect screen color';
+    Sdlvideo.update_rect ~rect: rect screen
+
 let load_maze screen maze =
   let width = Maze.width maze and height = Maze.height maze in
   let gray = (150, 150, 150) in
       draw_tile screen x y color
     done
   done
+  
+(* sounds *)
 
-let draw_dot screen color maze (x, y) =
-  let shift = tilesize / 2 in
-  let half = shift / 2 in
-  if Maze.get maze (x, y) <> '#' then
-    let color' = Sdlvideo.map_RGB screen color in
-    let rect = Sdlvideo.rect (x * tilesize + half) (y * tilesize + half) shift shift in
-    Sdlvideo.fill_rect ~rect: rect screen color';
-    Sdlvideo.update_rect ~rect: rect screen
+let sfx_step () =   
+  let sfx = Sdlmixer.loadWAV "data/sfx/step.wav" in
+  Sdlmixer.play_sound sfx
 
+let sfx_error () = 
+  let sfx = Sdlmixer.loadWAV "data/sfx/failure.wav" in
+  Sdlmixer.play_sound sfx
+  
+let sfx_success () =   
+  let sfx = Sdlmixer.loadWAV "data/sfx/success.wav" in
+  Sdlmixer.play_sound sfx
+  
 let walk screen maze path =
   List.iter (draw_dot screen Sdlvideo.blue maze) path
 
+(* interactions *)
+
 let solve screen maze goal start =
   let open Sdlevent in
   let open Sdlkey in
     | KEYDOWN { keysym = KEY_DOWN } -> fst current, (snd current) +1
     | _ -> current
   in
-  if not (Maze.check maze next) || Maze.get maze next = '#' then begin
+  if next = current then interact screen maze goal start current
+  else if not (Maze.check maze next) || Maze.get maze next = '#' then begin
+    sfx_error ();
     interact screen maze goal start current
   end
   else begin
     let color = (0xff, 0xc6, 0x00) in
     draw_dot screen color maze current;
-    if next = goal then raise Break
-    else interact screen maze goal start next
+    if next = goal then (sfx_success(); raise Break)
+    else (sfx_step(); interact screen maze goal start next)
   end
 
+(* main *)
+
 let mainloop screen maze_width maze_height =
   try
     while true do
 let _ =
   let width, height = 15, 11 in
   Random.self_init ();
-  Sdl.init [`VIDEO];
+  Sdl.init [`VIDEO; `AUDIO];
   at_exit Sdl.quit;
+  Sdlmixer.open_audio ();
+  at_exit Sdlmixer.close_audio;
+  
   Sdlkey.enable_key_repeat ();
+  Sdlwm.set_caption "maze minigame" "";
   let screen = Sdlvideo.set_video_mode (width * tilesize) (height * tilesize) [] in
   mainloop screen width height