This repository has been archived by the owner on Aug 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
twoPlayersGame.ml
108 lines (97 loc) · 3.73 KB
/
twoPlayersGame.ml
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
open OcsfmlWindow
open OcsfmlGraphics
open Camera2d
open MorpionDef
open Morpion
open Common
let morpion_width = 500.
let morpion_pos = 150.0,50.0
let main_two_players app =
let morpion = creer_grille () in
let camera2D = camera2D app (float app#get_width) (float app#get_height) in
let player = ref Croix in
let grid = ref false in
let gagnant = ref None in
(*let renderer = new render_texture 500 500 in *)
let cursor =
let cercle_spr = new circle_shape ~radius:30. ~fill_color:Color.red ~outline_color:Color.black ~outline_thickness:2. () in
let croix_spr = new rectangle_shape ~size:(40.,40.) ~fill_color:Color.blue ~outline_color:Color.black ~outline_thickness:2. () in
let grid_texture = new texture (`File "grid.png") in
let grid_spr = new sprite ~texture:grid_texture () in
cercle_spr # scale 0.75 0.75 ; croix_spr # scale 0.75 0.75 ; grid_spr # scale 0.75 0.75 ;
let eval () =
let x, y = app#convert_coords (Mouse.get_relative_position app) in
let pos = x +. 10.0, y +. 10.0 in
if !grid
then (grid_spr#set_position_v pos ; app#draw grid_spr)
else
match !player with
| Croix -> (croix_spr#set_position_v pos ; app#draw croix_spr)
| Cercle -> (cercle_spr#set_position_v pos ; app#draw cercle_spr)
in eval
in
let gagne =
let msg = new text ~style:[Bold] ~character_size:96 ~color:Color.red () in
let eval () =
match !gagnant with
| None -> ()
| Some Croix -> (msg#set_string "Croix a gagné\nEsc pour quitter" ; place_middle msg 300. ; app#draw msg)
| Some Cercle -> (msg#set_string "Cercle a gagné\nEsc pour quitter" ; place_middle msg 300. ; app#draw msg)
in
eval
in
let process_event b evt =
let open Event in
match evt with
| Closed -> app#close ; false
| KeyPressed { code = KeyCode.Escape ; _ } -> false
| KeyPressed { code = KeyCode.Left ; _ } -> camera2D#activate_horizontal (-1) ; b
| KeyPressed { code = KeyCode.Right ; _ } -> camera2D#activate_horizontal 1 ; b
| KeyPressed { code = KeyCode.Up ; _ } -> camera2D#activate_vertical (-1) ; b
| KeyPressed { code = KeyCode.Down ; _ } -> camera2D#activate_vertical 1 ; b
| KeyReleased { code = KeyCode.Left ; _ } -> camera2D#activate_horizontal 0 ; b
| KeyReleased { code = KeyCode.Right ; _ } -> camera2D#activate_horizontal 0 ; b
| KeyReleased { code = KeyCode.Up ; _ } -> camera2D#activate_vertical 0 ; b
| KeyReleased { code = KeyCode.Down ; _ } -> camera2D#activate_vertical 0 ; b
| KeyReleased { code = KeyCode.Space ; _ } -> grid := not !grid ; b
| MouseWheelMoved ( k , { x ; y } ) ->
camera2D#scroll k x y ; b
| MouseButtonPressed (_, { x ; y }) ->
if !gagnant = None
then
let pos = app#convert_coords (x,y) in
let path = get_position_coup_vide morpion_pos morpion_width morpion pos in
begin match path with
| None -> b
| Some l ->
if !grid
then modify_at_path (l,morpion) (fun _ -> creer_grille ())
else (
modify_at_path (l,morpion) (fun _ -> creer_joueur !player) ;
if process_victoire !player morpion l
then gagnant := Some !player
else player := (match !player with Croix -> Cercle | Cercle -> Croix)
);
b
end
else b
| _ -> b
in
let display () =
app#clear ~color:Color.white () ;
draw app 0 morpion_pos morpion_width morpion ;
camera2D#disable ;
cursor () ;
gagne () ;
camera2D#enable ;
app#display
in
let rec event_loop () =
let b = while_opt (fun () -> app#poll_event) process_event true in
camera2D#update ;
display () ;
if app#is_open && b then event_loop ()
in
event_loop () ;
camera2D#disable ;
Some (EntryPoint)