Graphics (raylib)
The built-in raylib module draws in a window inside the playground’s Output
panel. Its function names and arguments follow Python raylib (pyray), so programs read like raylib
programs, with tuples for points and colours. Open the Bouncing ball example in the playground
to try it.
import raylib as rl
rl.init_window(320, 200, "Hello")rl.begin_drawing()rl.clear_background(rl.RAYWHITE)rl.draw_circle(160, 100, 40, rl.BLUE)rl.draw_text("Hello, Nilakan", 12, 12, 20, rl.DARKGRAY)rl.end_drawing()print(rl.get_screen_width())Animation
Section titled “Animation”An animation is a commit loop that draws one frame per round. The
init: block and primed names track values such as position from one
round to the next. window_should_close() ends the loop when you close the window or press Escape.
A loop must live inside a function, and the function must return a value on every path. That’s why
the example ends with return 0.
import raylib as rl
def slide(): rl.init_window(320, 200, "Slide") rl.set_target_fps(60) init: x = 0 frames = 0 while not rl.window_should_close(): x' = x + 2 frames' = frames + 1 rl.begin_drawing() rl.clear_background(rl.BLACK) rl.draw_rectangle(x, 80, 40, 40, rl.GOLD) rl.end_drawing() rl.close_window() print(frames) return 0
slide()A round’s drawing follows the same transaction rules as print:
drawing calls take effect when the round commits, so the frame a round draws appears only once the
round’s state is committed. A round that is rolled back draws nothing. Frames are paced to
set_target_fps (60 by default). An animation can run as long as you like. The playground’s loop
limit stops a program that runs too many rounds without showing a frame.
Drawing
Section titled “Drawing”Drawing calls go between begin_drawing() and end_drawing(). Coordinates are pixels from the
window’s top-left corner, with y growing downward. A point is an (x, y) tuple, and a colour is an
(r, g, b) or (r, g, b, a) tuple of whole numbers from 0 to 255, or a colour constant such as
raylib.RED.
| Function | Draws |
|---|---|
clear_background(color) |
Fills the whole frame. |
draw_pixel(pos_x, pos_y, color) |
One pixel. |
draw_line(start_pos_x, start_pos_y, end_pos_x, end_pos_y, color) |
A line between two points. |
draw_line_v(start_pos, end_pos, color) |
A line between two (x, y) points. |
draw_circle(center_x, center_y, radius, color) |
A filled circle. |
draw_circle_v(center, radius, color) |
A filled circle around an (x, y) point. |
draw_circle_lines(center_x, center_y, radius, color) |
A circle’s outline. |
draw_rectangle(pos_x, pos_y, width, height, color) |
A filled rectangle from its top-left corner. |
draw_rectangle_lines(pos_x, pos_y, width, height, color) |
A rectangle’s outline. |
draw_triangle(v1, v2, v3, color) |
A filled triangle through three points. |
draw_triangle_lines(v1, v2, v3, color) |
A triangle’s outline. |
draw_text(text, pos_x, pos_y, font_size, color) |
Text with its top-left corner at the point. |
Drawing calls, and functions that call them, are statements, like print: they cannot be used as
values. A frame holds at most 10,000 drawing calls.
Window and time
Section titled “Window and time”| Function | Does |
|---|---|
init_window(width, height, title) |
Opens the window. Width and height are 1 to 4096 pixels. |
close_window() |
Closes it. |
window_should_close() |
True once the window is closed or Escape is pressed. |
set_target_fps(fps) |
Shows at most this many frames per second. |
get_screen_width(), get_screen_height() |
The window’s size. |
get_time() |
Seconds since the window opened. |
get_frame_time() |
Seconds the previous frame took. |
Call close_window() before opening another window in the same run. Each new window starts with
fresh keyboard and mouse state and no pending close request.
Keyboard and mouse
Section titled “Keyboard and mouse”Input is read once per frame. Every query in a round sees the same snapshot, taken when the previous frame was shown, so a loop body reads it like the rest of its frozen state.
| Function | Returns |
|---|---|
is_key_down(key), is_key_up(key) |
Whether a key is held. |
is_key_pressed(key), is_key_released(key) |
Whether a key went down or up since the last frame. |
get_mouse_x(), get_mouse_y(), get_mouse_position() |
The pointer’s position in the window. |
is_mouse_button_down(button), is_mouse_button_up(button) |
Whether a button is held. |
is_mouse_button_pressed(button), is_mouse_button_released(button) |
Whether a button changed since the last frame. |
Keys are constants such as KEY_SPACE, KEY_ENTER, KEY_ESCAPE, KEY_A to KEY_Z, KEY_0 to
KEY_9, and the arrows KEY_LEFT, KEY_RIGHT, KEY_UP, and KEY_DOWN. Mouse buttons are
MOUSE_BUTTON_LEFT, MOUSE_BUTTON_RIGHT, and MOUSE_BUTTON_MIDDLE. Right-clicking the drawing
canvas goes to the program without opening the browser’s context menu.
Keyboard focus
Section titled “Keyboard focus”Click the window to type into it. Tab moves focus away from the window. Escape asks the program to close the window.
Colours
Section titled “Colours”LIGHTGRAY, GRAY, DARKGRAY, YELLOW, GOLD, ORANGE, PINK, RED, MAROON, GREEN,
LIME, DARKGREEN, SKYBLUE, BLUE, DARKBLUE, PURPLE, VIOLET, DARKPURPLE, BEIGE,
BROWN, DARKBROWN, WHITE, BLACK, BLANK, MAGENTA, and RAYWHITE have raylib’s values.
Constants can be read anywhere, including inside functions.
Limits
Section titled “Limits”- Windows open only in the playground. The command line reports an error
when a program calls
init_window. - This is a small part of raylib: there are no textures, sounds, 3D, or cameras.
import raylibuses the built-in module only when there is noraylib.nlkfile next to the importing file.