3.20 world
include world
import world as ...
The world model is based on the universe teachpack in HtDP. You can find documentation for the teachpack here:
The Pyret world library provides functions for building animations and interactive programs.
3.20.1 Data Types
This type includes the values that can be passed to big-bang as event handlers (e.g. on-tick and on-key), renderers (e.g. to-draw), and other configuration options (e.g. stop-when).
3.20.2 Functions
- big-bang :: (
- init :: a,
- handlers :: List<WorldConfig<a>>
- )
- -> a
This function starts a world program in the initial state specified by init. Its behaviour is defined via the handler functions handlers. These handler functions allow you to define various program behaviours: rendering, mouse and keyboard events, timed events, when to shut down, etc. A world specification may not contain more than one to-draw or on-tick handlers. This function will return the last world state when the stop condition is satisfied (see stop-when) or when the canvas is closed.
- 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-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.
- 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.