friedbrice / elm-teaching-tools / ElmTeachingTools.Lib.Game

A library with a small scaffolding for creating simple games.

Creating games


type alias Game s e =
Platform.Program () s (GameEvent e)

A simple game, with state type s and event type e.

game : { init : s, update : GameEvent e -> s -> s, view : s -> ElmTeachingTools.Lib.Graphics.Screen e } -> Game s e

Create a game.

This follows the State Machine program architecture. We must provide:

  1. A type s of possible program states,
  2. A type e of possible custom events,
  3. A starting state init : s,
  4. A transition function update : GameEvent e -> s -> s, and
  5. A view function view : s -> Screen e

This framework comes with some built-in events. Specifically, GameEvent can be a ClockTick event, a Keyboard event, or it can be a Custom event with type e.

Game events


type alias Timestamp =
Basics.Int

The number of milliseconds since Midnight UTC Jan 1 1970.

Note: You can tell how much time has elapsed in your game by saving the start time in your game state and then subtracting later on.


type GameEvent e
    = ClockTick Timestamp
    | Keyboard ElmTeachingTools.Lib.Keyboard.KeyEvent
    | Custom e

Events that your game can respond to.

A ClockTick event occurs about 60 times per second and carries the current timestamp.

A Keyboard event occurs whenever a key is pushed down or released (see Lib.Keyboard).

A Custom event can be triggered by the onClick property of a Graphic element (see Lib.Graphics).