HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Snake Game OCaml

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
ocamlgamesnake

Problem

I wrote the following two players snake game in OCaml. It is working fine (I did not pay much efforts to the graphical appearance though). As a beginner in OCaml, I would be happy to have some review.

```
#load "unix.cma";;
#load "graphics.cma";;

let minisleep (sec: float) =
ignore (Unix.select [] [] [] sec);;

Graphics.open_graph " ";;
Graphics.set_window_title "Test !";;
Graphics.plot 50 50;;

type relief = Top | Bot | Flat;;

type box_config =
{ w:int; h:int; bw:int; mutable r:relief;
b1_col : Graphics.color;
b2_col : Graphics.color;
b_col : Graphics.color} ;;

let draw_rect x0 y0 w h =
let (a,b) = Graphics.current_point()
and x1 = x0+w
and y1 = y0+h
in
Graphics.moveto x0 y0;
Graphics.lineto x0 y1; Graphics.lineto x1 y1;
Graphics.lineto x1 y0; Graphics.lineto x0 y0;
Graphics.moveto a b ;;

let draw_background n_tiles tile_size =
for i=1 to n_tiles do
for j=1 to n_tiles do
draw_rect (itile_size) (jtile_size) tile_size tile_size
done
done ;;

let draw_box_outline bcf col x1 y1=
Graphics.set_color col;
draw_rect x1 y1 bcf.w bcf.h ;;

let draw_box bcf x1 y1 =
let x2 = x1+bcf.w and y2 = y1+bcf.h in
let ix1 = x1+bcf.bw and ix2 = x2-bcf.bw
and iy1 = y1+bcf.bw and iy2 = y2-bcf.bw in
let border1 g =
Graphics.set_color g;
Graphics.fill_poly
[| (x1,y1);(ix1,iy1);(ix2,iy1);(ix2,iy2);(x2,y2);(x2,y1) |]
in
let border2 g =
Graphics.set_color g;
Graphics.fill_poly
[| (x1,y1);(ix1,iy1);(ix1,iy2);(ix2,iy2);(x2,y2);(x1,y2) |]
in
Graphics.set_color bcf.b_col;
( match bcf.r with
Top ->
Graphics.fill_rect ix1 iy1 (ix2-ix1) (iy2-iy1);
border1 bcf.b1_col;
border2 bcf.b2_col
| Bot ->
Graphics.fill_rect ix1 iy1 (ix2-ix1) (iy2-iy1);
border1 bcf.b2_col;
border2 bcf.b1_col
| Flat ->
Graphics.fill_rect x1 y1 bcf.w bcf.h );

Solution

I saw the same problem Abdallah found: Unix.Unix_error (Unix.EINTR, "select", "")

This can be fixed by using Unix.sleepf (OCaml version >= 4.03.0):

let minisleep (sec: float) = Unix.sleepf sec

Context

StackExchange Code Review Q#121563, answer score: 3

Revisions (0)

No revisions yet.