-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.ml
214 lines (203 loc) · 5.5 KB
/
game.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
open Level
open Render
open Basephysics
open Gamemechanics
open Gametypes
open Graphics
open Gui
(* Display a win message *)
let displayWin () =
let rec wait () =
drawCenteredText 250 400 "Congratulations, you win!";
synchronize ();
ignore (wait_next_event [Button_down]);
wait ()
in
clear_graph();
ignore (
try
set_font "-*-fixed-medium-r-semicondensed--25-*-*-*-*-*-iso8859-1"
with Graphics.Graphic_failure(_) -> ()
);
wait ()
(* Display a message before next level
* Must have a player to count the stars
*)
let displayNext score =
let rec drawStars n m =
if (n < m) then begin
let width = m * 50 in
let space = (m-1) * 30 in
let x = 250 - (width+space)/2 + 80*n in
draw_image star_sprite x 400;
drawStars (n+1) m
end
else
()
in
(* Wait a click *)
let rec wait_click () =
(* Draw *)
clear_graph();
drawCenteredText 250 550 "You did it, try the next level!";
drawCenteredText 250 500 "Your score";
drawStars 0 score;
drawButton 175 300 150 50 "Next";
drawButton 175 200 150 50 "Retry";
drawButton 175 100 150 50 "Rage quit";
synchronize ();
(* Wait for a click *)
let event = wait_next_event [Button_down] in
if event.button then begin
if ((event.mouse_x >= 175) && (event.mouse_x <= 325) && (event.mouse_y >= 300) && (event.mouse_y <= 350)) then begin
ignore (wait_next_event [Button_up]);
0 (* Return 0 to continue *)
end
else if ((event.mouse_x >= 175) && (event.mouse_x <= 325) && (event.mouse_y >= 200) && (event.mouse_y <= 250)) then begin
ignore (wait_next_event [Button_up]);
1 (* Return 1 to retry *)
end
else if ((event.mouse_x >= 175) && (event.mouse_x <= 325) && (event.mouse_y >= 100) && (event.mouse_y <= 150)) then begin
ignore (wait_next_event [Button_up]);
exit 0
end
else
wait_click ()
end
else
wait_click ()
in
ignore (
try
set_font "-*-fixed-medium-r-semicondensed--25-*-*-*-*-*-iso8859-1"
with Graphics.Graphic_failure(_) -> ()
);
wait_click ()
(* Display a retry message *)
let displayRetry () =
(* Wait a click *)
let rec wait_click () =
(* Draw *)
clear_graph();
drawCenteredText 250 550 "Sorry, you loose....";
drawButton 175 400 150 50 "Retry";
drawButton 175 300 150 50 "Rage quit";
synchronize ();
(* Wait for a click *)
let event = wait_next_event [Button_down] in
if event.button then begin
if ((event.mouse_x >= 175) && (event.mouse_x <= 325) && (event.mouse_y >= 400) && (event.mouse_y <= 450)) then
ignore (wait_next_event [Button_up])
else if ((event.mouse_x >= 175) && (event.mouse_x <= 325) && (event.mouse_y >= 300) && (event.mouse_y <= 350)) then begin
ignore (wait_next_event [Button_up]);
exit 0
end
else
wait_click ()
end
else
wait_click ()
in
ignore (
try
set_font "-*-fixed-medium-r-semicondensed--25-*-*-*-*-*-iso8859-1"
with Graphics.Graphic_failure(_) -> ()
);
wait_click ()
(* Merge all the values of an array *)
let mergeArray arr =
Array.fold_left (fun acc str -> acc ^ "\"" ^ str ^ "\" ") "" arr
(* Allow user to select a style *)
let styleSelect () =
(* Function called on a click *)
let buttonAction p =
ignore (wait_next_event [Button_up]);
close_graph ();
ignore (Sys.command ((mergeArray Sys.argv) ^ " -s " ^ p));
exit 0
in
(* Wait a click *)
let rec wait_click () =
(* Draw *)
clear_graph();
drawCenteredText 250 550 "Choose a theme....";
drawButton 175 400 150 50 "Basic";
drawButton 175 300 150 50 "Mario";
drawButton 175 200 150 50 "Cut The Rope";
synchronize ();
(* Wait for a click *)
let event = wait_next_event [Button_down] in
if event.button then begin
if ((event.mouse_x >= 175) && (event.mouse_x <= 325) && (event.mouse_y >= 400) && (event.mouse_y <= 450)) then
buttonAction "basic"
else if ((event.mouse_x >= 175) && (event.mouse_x <= 325) && (event.mouse_y >= 300) && (event.mouse_y <= 350)) then
buttonAction "mario"
else if ((event.mouse_x >= 175) && (event.mouse_x <= 325) && (event.mouse_y >= 200) && (event.mouse_y <= 250)) then
buttonAction "ctr"
else
wait_click ()
end
else
wait_click ()
in
ignore (
try
set_font "-*-fixed-medium-r-semicondensed--25-*-*-*-*-*-iso8859-1"
with Graphics.Graphic_failure(_) -> ()
);
wait_click ()
(* Main function, will increment the level *)
let rec main levelInfos =
try
game_loop (snd levelInfos)
with
| EndGame(Win(score)) -> (
let rec wait_up () =
if (button_down ()) then
wait_up ()
else
()
in
wait_up ();
let newID = (fst levelInfos) + 1 in(* Show the score for this level *)
if ((displayNext score) = 0) then begin
try
(* Load the next level *)
let newLevel = loadLevel ("levels/" ^ (string_of_int newID) ^ ".lvl") in
(* Launch the new level *)
main (newID, newLevel)
with LevelLoadError ->
(* If the incrementing loading fail, that means we are a the last level, so we win *)
displayWin ()
end
else
(* Retry *)
main levelInfos
)
| EndGame(Die) -> (
let rec wait_up () =
if (button_down ()) then
wait_up ()
else
()
in
wait_up ();
(* If the player die, we display the retry menu *)
displayRetry ();
main levelInfos
)
let () =
(* Detect if a style wasn't given *)
if (not (Array.mem "-s" Sys.argv)) then
styleSelect ();
(* Load a file if there is one given as a parameter *)
let firstLevel =
if ((Array.length Sys.argv) >= 2 && Sys.argv.(1) <> "-s") then
(-1, loadLevel Sys.argv.(1))
else
(1, loadLevel "levels/1.lvl")
in
(* Run the game *)
try
main firstLevel
with Graphics.Graphic_failure(_) -> exit 0