(********************************************************************) (* OCaml code from the book ``Learn Programming with OCaml'' *) (* See https://usr.lmf.cnrs.fr/lpo/ *) (* *) (* Sylvain Conchon and Jean-Christophe Filliâtre *) (* Copyright 2025 Université Paris-Saclay and CNRS *) (* *) (* Openly licensed via CC BY SA 4.0 *) (* See https://creativecommons.org/licenses/by-sa/4.0/deed.en *) (********************************************************************) (* Program 11 on page 90 Breakout without Bricks (2/2) *) let bounce (x, y) (vx, vy) xp = let vx = if x <= Draw.left || x >= Draw.right then -. vx else vx in let vy = if y <= float Draw.thick && x >= xp && x <= xp +. float Draw.paddle || y >= Draw.up then -. vy else vy in (vx, vy) let new_position (x, y) (vx, vy) = x +. vx, y +. vy let rec play (x, y) (vx, vy) = if y <= Draw.down then begin Printf.eprintf "Game over!\n"; exit 0 end; let xp = Draw.game x y in let vx, vy = bounce (x, y) (vx, vy) (float xp) in let x', y' = new_position (x, y) (vx, vy) in play (x', y') (vx, vy) let () = Draw.init(); let speed = 0.1 in let vx = speed *. Random.float 1. in let vy = speed *. Random.float 1. in play (Draw.right /. 2., float Draw.thick) (vx, vy)
This document was generated using caml2html