3.21 world
Handler functions written for big-bang are compatible with reactors, so it is easy to start with big-bang and move to reactors if you need their advanced features.
The world/reactor model is based on the universe teachpack in HtDP. You can find documentation for the teachpack here:
3.21.1 Starting a big-bang
- big-bang :: (
- init :: a,
- handlers :: List<WorldConfig<a>>
- )
- -> a
big-bang is called with an init state and a list of handler functions. It immediately creates an interactive window based on init and optionally begins drawing graphics and text as defined by the to-draw function, running handler functions scheduled by on-tick and on-tick-n as well as those triggered by on-mouse and on-key.
big-bang(init, |
[list: |
on-tick(‹expr›), |
on-tick-n(‹expr›), |
on-mouse(‹expr›), |
on-key(‹expr›), |
to-draw(‹expr›), |
stop-when(‹expr›) |
] |
) |
All of the handlers for big-bang (in the list argument) are
optional. They can also appear in any order —
include world big-bang('inert', [list: ]) fun increment(x): x + 1 end big-bang(10, [list: on-tick(increment)]) big-bang(10, [list: on-tick-n(increment, 3)])
These are not allowed:
big-bang(10, 11)
big-bang([list: on-tick(increment)])
big-bang(10, [list: not-a-handler(increment)])
big-bang(10, on-tick(increment))
3.21.2 Functions
- on-tick :: (
- handler :: (a -> a)
- )
- -> WorldConfig<a>
Consumes a function and returns a handler that, when passed to big-bang, will be called each program tick with the current world state.
- on-tick-n :: (
- handler :: (a -> a),
- n :: Number
- )
- -> WorldConfig<a>
Consumes a function and returns a handler that, when passed to big-bang, will be called every n program ticks with the current world state.
- to-draw :: (
- drawer :: (a -> Scene)
- )
- -> WorldConfig<a>
Consumes a function and returns a handler that, when passed to big-bang, will inform the world program what to draw.
- on-key :: (
- onKey :: (a, String -> a)
- )
- -> WorldConfig<a>
Consumes a function and returns a handler that, when passed to big-bang, will be called every time a key is pressed. The function is called with the current world state and a String representing the pressed key. For most keys, this is just the corresponding single character.
The special keys are:
Backspace key: "backspace"
Tab key: "tab"
Enter key: "enter"
Shift key: "shift"
Control key: "control"
Pause key: "pause"
Escape key: "escape"
Prior key: "prior"
Next key: "next"
End key: "end"
Home key: "home"
Left arrow: "left"
Up arrow: "up"
Right arrow: "right"
Down arrow: "down"
Print key: "print"
Insert key: "insert"
Delete key: "delete"
Backspace key: "backspace"
Num lock key: "numlock"
Scroll key: "scroll"
- on-mouse :: (
- mouse-handler :: (a, Number, Number, String -> a)
- )
- -> WorldConfig<a>
Consumes a function and returns a handler that, when passed to big-bang, will be called on every sampled mouse movement. The function will receive the world state, the current x and y positions of the mouse, and a String representing a mouse event. Possible mouse events are:
"button-down" signals that the computer user has pushed a mouse button down;
"button-up" signals that the computer user has let go of a mouse button;
"drag" signals that the computer user is dragging the mouse. A dragging event occurs when the mouse moves while a mouse button is pressed.
"move" signals that the computer user has moved the mouse;
"enter" signals that the computer user has moved the mouse into the canvas area; and
"leave" signals that the computer user has moved the mouse out of the canvas area.
- stop-when :: (
- stopper :: (a -> Boolean)
- )
- -> WorldConfig<a>
- is-world-config :: (
- v :: Any
- )
- -> WorldConfig<a>
Tests if the input is of type WorldConfig.
- is-key-equal :: (
- key1 :: String,
- key2 :: String
- )
- -> WorldConfig<a>
Tests if two key events are equals to each other.