1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| let couleur_carre = red;; (*carré que l'on va déplacer*)
let couleur_formes = blue;; (*couleur des obstacles*)
let couleur_fond = white;; (*couleur du fond*)
let t = 30;; (*côté du carré*)
let tf = 50;; (*taille des obstacles*)
let M = 500;; (*largeur et hauteur du cadre*)
let rec redsquare = fun (xr,yr,vx,vy) ->
let a = wait_next_event[Button_down; Poll] in
if a.button then ()
else
begin
(*gestion du carré*)
let (xs,ys) = mouse_pos() in
let x = if xs < 1 then 1
else if xs > M-1-t then M-1-t (*on prend M-1 pour ne pas colorer le bord du cadre*)
else xs
and y = if ys < 1 then 1 (*même chose pour y, afin que le carré soit enfermé*)
else if ys > M-1-t then M-1-t
else ys in
begin
set_color couleur_carre; (*dessin du carré*)
fill_rect x y t t;
delay(40);
set_color couleur_fond; (*permet d'effacer le carré quand il se déplace*)
fill_rect x y t t;
end;
(*gestion de l'obstacle*)
set_color couleur_formes;
fill_rect xr yr tf tf;
delay(40);
set_color couleur_fond;
fill_rect xr yr tf tf; (*annule la trace du carré*)
if yr > M-tf or yr < tf
then redsquare(xr+vx, yr-vy, vx, -vy) (*rebond vertical*)
else if xr < tf or xr > M-tf
then redsquare(xr-vx, yr+vy, -vx, vy) (*rebond horizontal*)
else redsquare(xr+vx, yr+vy, vx, vy);
end;; |
Partager