It is my pleasure to announce the first release of elm_playground
, an OCaml package that allows you to easily create pictures, animations, and even video games in a portable way using an API that really simplifies how to view the computer and its devices (the screen, keyboard, and mouse). The library offers a native backend to run the games from a terminal and a web backend to run the games in your browser.
This is a port of the excellent Elm playground package
GitHub - evancz/elm-playground: Create pictures, animations, and games with Elm! to OCaml.
You can install it via OPAM via opam install elm_playground
.
Here are a few examples of code using the library.
First a “picture” app:
(* from https://elm-lang.org/examples/picture *)
open Playground
let app =
picture [
rectangle brown 40. 200.
|> move_down 80.;
circle green 100.
|> move_up 100.;
]
let main = Playground_platform.run_app app
Then an “animation” app:
(* from https://elm-lang.org/examples/animation *)
open Playground
let view time = [
octagon darkGray 36.
|> move_left 100.
|> rotate (spin 3. time);
octagon darkGray 36.
|> move_right 100.
|> rotate (spin 3. time);
rectangle red 300. 80.
|> move_up (wave 50. 54. 2. time)
|> rotate (zigzag (-. 2.) 2. 8. time);
]
let app =
animation view
let main = Playground_platform.run_app app
And finally a “game” app:
(* from https://elm-lang.org/examples/mouse *)
open Playground
let view _computer (x, y) = [
square blue 40.
|> move x y
]
let update computer (x, y) =
(x +. to_x computer.keyboard, y +. to_y computer.keyboard)
let app =
game view update (0., 0.)
let main = Playground_platform.run_app app
Note that you can write more complex games. For example here is a screenshot of a toy tetris app:
For more information, follow the README at GitHub - aryx/ocaml-elm-playground: Port of the Elm playground package to OCaml to make pictures, animations, and even video games easily.
And merry christmas!