Builtins and Libraries
3.1 Global Utilities
3.2 Numbers
3.3 Strings
3.4 Booleans
3.5 Raw  Array
3.6 Tables
3.7 lists
3.8 sets
3.9 arrays
3.10 string-dict
3.11 option
3.12 pick
3.13 either
3.14 srcloc
3.15 pprint
3.16 s-exp
3.17 s-exp-structs
3.18 image-structs
3.19 image
3.20 world
3.21 reactors
3.22 plot
3.20 world
On this page:
3.20.1 Data Types
World  Config
3.20.2 Functions
big-bang
to-draw
on-tick
on-tick-n
on-key
on-mouse
stop-when
is-world-config
is-key-equal

3.20 world

Usage:

include world

import world as ...

The world model is based on the universe teachpack in HtDP. You can find documentation for the teachpack here:

http://docs.racket-lang.org/teachpack/2htdpuniverse.html

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>

Consumes a function and returns a handler that, when passed to big-bang, will be called to determine if the world should stop running. If the function returns true, then no other handlers will be called. The big-bang function will return this last world state.

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.