Next: Documentation
Up: Bibliothèques
Previous: Fonctions graphiques
#open "graphics";; open_graph "";; let c = 5;; (* {\em Le rayon de la balle} *) let draw_balle x y = set_color foreground; fill_circle x y c;; let clear_balle x y = set_color background; fill_circle x y c;; let get_mouse () = while not (button_down ()) do () done; mouse_pos();; let wait () = for i = 0 to 1000 do () done;; let v_x = 2 (* {\em Vitesse de déplacement de la balle} *) and v_y = 3;; let x_min = 2 * c + v_x;; (* {\em Cadre autorisé} *) let x_max = size_x () - x_min;; let y_min = 2 * c + v_y;; let y_max = size_y () - y_min;; let rec pong_aux x y dx dy = draw_balle x y; let new_x = x + dx and new_y = y + dy in let new_dx = if new_x <= x_min || new_x >= x_max then (- dx) else dx and new_dy = if new_y <= y_min || new_y >= y_max then (- dy) else dy in wait (); clear_balle x y; pong_aux new_x new_y new_dx new_dy;; let pong () = clear_graph(); pong_aux 20 20 v_x v_y;; pong ();;
Les adeptes du style impératif écriraient plutôt la procédure pong ainsi:
let dx = ref v_x and dy = ref v_y;; let pong () = clear_graph(); let x = ref 20 and y = ref 20 in while true do let cur_x = !x and cur_y = !y in draw_balle cur_x cur_y; x := cur_x + !dx; y := cur_y + !dy; if !x <= x_min || !x >= x_max then dx := - !dx; if !y <= y_min || !y >= y_max then dy := - !dy; wait (); clear_balle cur_x cur_y done;;