Commits

Anonymous committed 89fff8d

game chaining

  • Participants
  • Parent commits a7c7b69

Comments (0)

Files changed (3)

data/memory/sfx/success.wav

Binary file modified.

data/mills/win.wav

Binary file removed.
     | Some sound -> Sdlmixer.play_channel ~channel:0 sound
 end
 
-
 module Image = struct
   open Sdlvideo
   open Sdlloader
   let blit dst dst_rect img = match img with
     | None -> failwith "image not initialized"
     | Some image ->
-        Sdlvideo.blit_surface ~dst_rect: dst_rect ~src: image ~dst: dst () 
+        Sdlvideo.blit_surface ~dst_rect: dst_rect ~src: image ~dst: dst ()
   
   let draw_img screen x y img =
     let open Sdlvideo in
     let fullrect = rect x y imgsize imgsize in
     blit screen fullrect img
-    
+  
   let draw_background screen =
-    draw_img screen 0 0 !background    
+    draw_img screen 0 0 !background
 end
 
 let layout = function
         end
     | _ -> select_card cards
   
-  let rec play screen cards =
-    let show_backgrounds () =
+  let play screen cards =
+    let show_backgrounds cards =
       Image.draw_background screen;
       List.iter (fun c -> Image.draw_img screen c.x c.y !Image.back) cards;
       Sdlvideo.flip screen
       Image.draw_img screen c.x c.y Image.images.(c.img);
       Sdlvideo.flip screen
     in
-    match cards with
-    | [] -> 
-      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 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;
-        if success then Sfx.play Sfx.bleep; 
-        play screen remaining
+    let rec loop flips cards =
+      show_backgrounds cards;
+      match cards with
+      | [] ->
+          Sdltimer.delay 500;
+          Sfx.play Sfx.success;
+          Sdltimer.delay 1000;
+          flips
+      | _ ->
+          let first = select_card cards in
+          show_card first;
+          let second = select_card cards in
+          show_card second;
+          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;
+          if success then Sfx.play Sfx.bleep;
+          loop (succ flips) remaining
+    in loop 0 cards
 end
 
 let init_sdl width height =
   Sdlwm.set_caption "memory minigame" "";
   Sdlvideo.set_video_mode width height [`HWSURFACE;`DOUBLEBUF]
 
+let rec chain_games screen width height count =
+  let level_up = if count = 24 then 24 else count + 2
+  and level_down = if count = 4 then 4 else count - 2 in
+  let cards = get_cards width height count in
+  let flips = Game.play screen cards in
+  let ratio = float_of_int flips /. float_of_int count in
+  if ratio < 1.2 then chain_games screen width height level_up
+  else if ratio > 2. then chain_games screen width height level_down
+  else chain_games screen width height count
+
 let _ =
-  Random.self_init();  
+  Random.self_init();
   let width, height = 1024, 768 in
-  let screen = init_sdl width height in 
+  let screen = init_sdl width height in
   Image.load ();
   Sfx.load ();
-  let count = 10 in
-  let cards = get_cards width height count in
+  let count = 12 in
   try
-    Game.play screen cards
-  with Exit -> ()
+    chain_games screen width height count
+  with Exit -> ()