Skip to content

Commit df68c68

Browse files
committed
fix ball movement and use 60fps
1 parent 7a5b4a3 commit df68c68

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

backend/server.ml

+13-14
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,23 @@ let update_state (state : G.state) =
1010
let b = state.ball in
1111
let new_x = b.x +. b.dx in
1212
let new_y = b.y +. b.dy in
13-
let new_x =
14-
if new_x < 0. then 0.
15-
else if new_x > float state.width then float state.width
16-
else new_x
13+
let x, dx =
14+
if new_x -. b.radius < 0. then (b.radius, -.b.dx)
15+
else if new_x +. b.radius > float state.width then
16+
(float state.width -. b.radius, -.b.dx)
17+
else (new_x, b.dx)
1718
in
18-
let new_y =
19-
if new_y < 0. then 0.
20-
else if new_y > float state.height then float state.height
21-
else new_y
19+
let y, dy =
20+
if new_y -. b.radius < 0. then (b.radius, -.b.dy)
21+
else if new_y +. b.radius > float state.height then
22+
(float state.height -. b.radius, -.b.dy)
23+
else (new_y, b.dy)
2224
in
23-
{
24-
state with
25-
ball = { x = new_x; y = new_y; radius = b.radius; dx = b.dx; dy = b.dy };
26-
}
25+
{ state with ball = { x; y; radius = b.radius; dx; dy } }
2726

2827
let rec game_loop (state : G.state) =
29-
let fps = 2. in
30-
(* 2 frames per second *)
28+
let fps = 60. in
29+
(* frames per second *)
3130
Lwt_unix.sleep (1. /. fps) >>= fun () ->
3231
let new_state = update_state state in
3332
let sexp = G.sexp_of_server_message (Update new_state) in

frontend/client.ml

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,11 @@ let animate ctx canvas =
4949
(* Draw ball *)
5050
ctx##beginPath;
5151
ctx##arc !ball.x !ball.y !ball.radius 0. (2. *. Float.pi) Js._false;
52-
ctx##.fillStyle := Js.string "green";
52+
ctx##.fillStyle := Js.string "white";
5353
ctx##fill;
54+
ctx##.lineWidth := Js.float 4.;
55+
ctx##.strokeStyle := Js.string "black";
56+
ctx##stroke;
5457
ctx##closePath;
5558

5659
(* Request next animation frame *)

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
canvas {
6060
background-color: var(--basquiat-bg);
6161
border: 3px solid var(--basquiat-black);
62-
padding: 10px;
62+
padding: 0px;
6363
box-shadow:
6464
-4px -4px 0 var(--basquiat-red),
6565
4px 4px 0 var(--basquiat-green);
@@ -111,4 +111,4 @@
111111
<body>
112112
</body>
113113

114-
</html>
114+
</html>

0 commit comments

Comments
 (0)