Create pictures, animations, and games with OCaml!
This is a port of the excellent Elm playground package to OCaml.
This is the package I wanted when I was learning programming. Start by putting shapes on screen and work up to making games. I hope this package will be fun for a broad range of ages and backgrounds!
A picture is a list of shapes. For example, this picture combines a brown rectangle and a green circle to make a tree
open Playground
let app =
picture [
rectangle brown 40. 200.;
circle green 100.
|> move_up 100.;
]
let main = Playground_platform.run_app appPlay around to get familiar with all the different shapes and transformations in the library.
An animation is a list of shapes that changes over time. For example, here is a spinning triangle:
open Playground
let view time = [
triangle orange 50.
|> rotate (spin 8. time);
]
let app = animation view
let main = Playground_platform.run_app appIt will do a full spin every 8 seconds.
Maybe try making a car with spinning octogons as wheels? Try using Playground.wave to move things back-and-forth? Try using Playground.zigzag to fade things in-and-out?
A game lets you use input from the mouse and keyboard to change your picture. For example, here is a square that moves around based on the arrow keys:
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 appEvery game has three important parts:
memory - Store information. Our example stores (x,y) coordinates.update - Update the memory based on mouse movements, key presses, etc. Our example moves the (x,y) coordinate around based on the arrow keys.view - Turn the memory into a picture. Our example just shows one blue square at the (x,y) coordinate we have been tracking in memory.When you start making fancier games, you will store fancier things in memory. There is a lot of room to develop your programming skills here: Making lists, using records, creating custom types, etc.
I started off trying to make Pong, then worked on games like Breakout and Space Invaders as I learned more and more. It was really fun, and I hope it will be for you as well!
To make characters like Mario walk, see Making animated sprites.
The same in 3D is Playground3d (package elm_playground_3d).
Everything a game or an application needs is written in this repository from scratch, one idea per module, and each module's interface explains its idea. Here they are, grouped as the README's table groups them.
The APIs over the libraries below, where games meet them:
and in 3D Physics3d, Audio3d, Juice3d, Multiplayer3d.
The layers, a camera, sprites, a tilemap, a scene:
and in 3D Camera3d, Character3d, Ragdoll3d, Portal3d.
Other ways to program, each building a Playground app for you:
and Logo3d.
What Elm's core gives, and a few more:
The 2D rasterizer:
and Hershey's fonts and the VGA's.
The 3D rasterizer (package elm_playground_3d_software): Project, Clip, Cull, Triangle, Interpolate, Zbuffer, Painter, Shading, Texture, Render; over the geometry every 3D backend shares:
The ray tracer:
Pixels, and image processing:
Framebuffer Blit Opti Pixelate Matting Rgba_image Lut Histogram Hsl Convolve Gaussian Sobel Median Add_noise Scale Mask Composite Blend Brush Gradient Layers Pixels Images and videos, read and written by our own code:
Png Gif Jpeg Jpeg_encode Dct Xpm Ilbm Svg Movie Yuv Psnr Y4m Fli Avi Mpeg1 Mpeg1_vlc Motion Mpeg1_encode Mpeg_system Body Shape Force Integrate Broadphase Collide Contact Resolve Solver Joint2d Springs Energy Particles Kepler Planets and in 3D Quat, Mat3, Body3d, Force3d, Integrate3d, Broadphase3d, Collide3d, Sweep3d, Contact3d, Resolve3d, Solver3d, Joint3d, Energy3d, Hitbox3d.
Signals and synthesis:
The engine:
A synthesizer's live blocks:
Vco Lfo Drift Voicing Moog_ladder Diode_ladder Svf Modal Fm_algorithm Dx_envelope Polyphony Sampler Sequencer Effects:
Files: WAV, MIDI, ABC, solfège, MOD, and MPEG audio (MP2, MP3):
Wav Midi Abc Doremi Mod Mod_player Paula Mpeg_audio Mpeg_audio_header Layer2 Layer3 Layer3_tables Imdct Polyphase Pathfind Steering Flock Fsm Behavior Utility Sense Bot Minimax Deepening Zobrist Mcts Matrix Neuron Net Backprop Grad Train Qlearn and the game juice:
The protocols as bytes in, bytes out:
Url Http Urlencoded Websocket Irc Wire Transport Mime Mail Mail_thread Mbox Pop3 Smtp Checksum Sim_net Inputs Lockstep Rollback Snapshot Prediction Interpolation Asn1 Pem X509 Tls13 and their sockets, native only (package elm_playground_software): Tcp, Udp, Http_client, Tls_client, Server, Relay.
Cryptography, what TLS 1.3 needs:
Compression:
What a few applications share: a spreadsheet, rich text, paint and draw, compound documents, a browser's engine, editors, CAD, ...
Sheet Sheet_view Rich Style Flow Page Linebreak Outline Bitmap Paint Pattern Seed_fill Indexed Cycling Drawing Figure Figure_shapes Document Component Compound Clipboard Undo Saved File_menu Stroke_text Browser_page Browser_draw Browser_history Browser_forms Browser_tab Browser_script Browser_text Browser_boxes Browser_picture Browser_url Browser_devtools Box_layout Flex_layout Table_layout Html_layout Hit Gap_buffer Emacs_editor Emacs_simple Tui_vi Tui_emacs Tui_turbo Tui_snake Tty_guess Tty_hangman Tty_wumpus Block_layout Block_edit Sketch Relax Cad_geom Cad_drawing Cad_edit Cad_snap Cad_session Dxf Skp_model Skp_infer Skp_view Bsp Modeler Modeler_view Celestial Ephemeris Bright_stars Stereographic Ics Vcard Money Ledger Reconcile Qif Nls_doc Diagram Ortho_route How each genre works, what the games of a genre share: shoot 'em ups, fighting games, platformers, mazes, puzzles, cards, adventures, racing, sports, strategy, rhythm games, and the levels of Doom, Descent and Comanche:
Shots Path Hitbox Frame_data Stickman Tile_move Ladder Slope Crush Grid_move Chase Lightcycles Push Puzzle_undo Sokoban Cards Adventure Road Car Topdown Offroad Free_ball Formation Orders Rhythm Sectors Segments Sixdof Heightmap Isometric and in 3D Skeleton and Track3d.
Languages as text, parsed and run: a spreadsheet's formulas, BASIC, s-expressions, Emacs Lisp, Scheme, Pascal, HyperTalk, Smalltalk-80, Scratch, PostScript, JavaScript, HTML and CSS, and JSON and jsonnet (the code map's configurations):
Formula Basic_parse Basic_run Basic_session Basic_disk Sexpr Sexpr_read Lisp Lisp_read Lisp_eval Lisp_prelude Scheme Scheme_image Scheme_syntax Scheme_prims Scheme_prelude Scheme_eval Scheme_step Pascal_lexer Pascal_compile Pcode Pmachine Pdebug Pascal_disk Hypertalk St_lexer St_parse St_ast St_compile St_bytecode St_chunk St_memory St_class St_kernel St_interp St_primitives St_bitblt St_image St_boot St_debug Scratch_blocks Scratch_text Scratch_run Ps_lexer Ps_graphics Ps_machine Ps_disk Js_lexer Js_parse Js_ast Js_value Js_eval Js_builtins Js_regexp Html_lexer Html_tree Dtd Entities Charset Dom Forms Line_mode Css Css_syntax Css_values Selectors Cascade Computed Looks Ua_sheet Highlight_code Highlight_ml Lexer_ml Token_ml Json Jsonnet Jsonnet_lexer Jsonnet_ast Jsonnet_parse