Windows and graphics

Make windows, drawings and games

With one line, use gui, a comet program can open real windows: text, buttons, typing boxes, shapes, pictures, the keyboard and the mouse. This page teaches it step by step, from a window that says hello to a small game. Every program has a Run in playground link: the window opens right there in your browser, so change the numbers and see what happens.

What a window program is

A normal program writes lines into the console. A window program draws into its own window instead. Three things are new:

  • use gui at the top of the file switches windows on. It is never turned on by itself, so a file without it cannot open a window.
  • window = new.window("title", width, height) makes the window. Every other command is then called on that variable: window.print(...), window.rect(...) and so on.
  • The program must keep the window alive. When main ends, the window closes. So a program either waits with window.wait() (for a picture that just stays) or runs a loop while (window.open()) { ... } (for anything that moves or reacts).

Where does it run? On your own computer the downloaded comet opens a real window (Windows out of the box; Linux needs sudo apt install libx11-dev). In the playground and on compile.okak0.com the window is drawn in the page, and the Download button there still gives you a real .exe.

1. Your first window

This opens a window 400 pixels wide and 200 high, writes two lines and waits until you close it. window.print without a position puts each line under the last one.

use gui

fun main() {
    window = new.window("My first window", 400, 200)
    window.print("Hello!")
    window.print("This is a real window.")
    window.wait()
}
Commands in this program (5)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • window.print(text) / window.print(text, x, y) Writes text inside the window, like printl does in the console. With x and y you choose where: x is how many pixels from the left edge, y how many pixels down from the top edge, and that point is the top-left corner of the text. Without x and y, each print goes on the next line under the last one (or where window.goto said). The text stays there until window.clear().
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.

Try: change the title or the size, add a third window.print. Remove window.wait() and the window closes the moment it opens, because main is over.

2. Where things go: x and y

Everything in a window has a position in pixels, the tiny dots of the screen. x counts from the left edge, y from the top edge. So (0, 0) is the top-left corner, and a bigger y is further down (not up, like in maths at school).

(0, 0) x grows to the right y grows downwards window.circle(240, 110, 5) x = 240 y = 110 width 400, height 220
use gui

fun main() {
    window = new.window("Where things go", 400, 300)
    window.print("(0, 0) is the top-left corner", 0, 0)
    window.print("x = 200, y = 40", 200, 40)
    window.print("x = 20, y = 200", 20, 200)
    window.color("red")
    window.circle(200, 150, 5)
    window.color("white")
    window.print("the middle: (200, 150)", 210, 140)
    window.wait()
}
Commands in this program (7)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • window.print(text) / window.print(text, x, y) Writes text inside the window, like printl does in the console. With x and y you choose where: x is how many pixels from the left edge, y how many pixels down from the top edge, and that point is the top-left corner of the text. Without x and y, each print goes on the next line under the last one (or where window.goto said). The text stays there until window.clear().
  • window.color(name) Picks the colour for everything you draw after it: text, lines, rectangles and circles. Think of it as picking up a different pen; it stays until you pick another. The name is a colour word like "red" or "green" (see the colour table), or your own colour as "#rrggbb".
  • window.circle(x, y, radius) Draws a filled circle (a disc) in the current colour. x and y are the centre of the circle (not its corner, unlike rect), and radius is how far the edge is from the centre, so the circle is radius * 2 pixels wide. Example: window.circle(200, 150, 30) draws a circle 60 wide around the point 200, 150.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.

Text is placed by its top-left corner, rectangles too, but circles by their centre. window.width() and window.height() tell you the size, so window.width() / 2 is always the middle.

3. Text and colours

window.color("name") picks the colour for everything drawn after it: text, lines and shapes. window.background("name") colours the whole window behind it. window.goto(x, y) sets where the next window.print without a position goes, and each print then moves one line down, which makes neat columns.

use gui

fun main() {
    window = new.window("Text and colours", 420, 260)
    window.background("light")
    window.color("dark")
    window.print("A shopping list:", 20, 20)
    window.goto(40, 60)
    window.color("blue")
    window.print("- bread")
    window.print("- milk")
    window.color("#cc3399")
    window.print("- cake (my own colour #cc3399)")
    window.wait()
}
Commands in this program (8)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • window.background(name) Paints the whole window in one colour, behind everything else. Without it the window is dark. Usually you set it once, right after new.window.
  • window.color(name) Picks the colour for everything you draw after it: text, lines, rectangles and circles. Think of it as picking up a different pen; it stays until you pick another. The name is a colour word like "red" or "green" (see the colour table), or your own colour as "#rrggbb".
  • window.print(text) / window.print(text, x, y) Writes text inside the window, like printl does in the console. With x and y you choose where: x is how many pixels from the left edge, y how many pixels down from the top edge, and that point is the top-left corner of the text. Without x and y, each print goes on the next line under the last one (or where window.goto said). The text stays there until window.clear().
  • window.goto(x, y) Moves the writing position: the next window.print or window.input without its own x and y starts at this point (x pixels from the left, y pixels from the top). After each print the position moves one line down, so a few prints after one goto make a neat column.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.

Colours are names like "red" or "light" (see the colour table), or any colour as "#rrggbb": red, green and blue from 00 to ff. "#ff0000" is pure red, "#808080" grey.

4. Drawing shapes

Three commands draw everything: window.line(x1, y1, x2, y2) from one point to another, window.rect(x, y, width, height) a filled rectangle from its top-left corner, and window.circle(x, y, radius) a filled circle around its centre. They use the current colour. Later shapes are drawn over earlier ones, so draw the background first.

use gui

fun main() {
    window = new.window("A house", 400, 300)
    window.background("cyan")
    window.color("green")
    window.rect(0, 240, 400, 60)       // the grass
    window.color("brown")
    window.rect(120, 140, 160, 100)    // the wall
    window.color("red")
    window.line(110, 140, 200, 70)     // the roof: three lines
    window.line(200, 70, 290, 140)
    window.line(110, 140, 290, 140)
    window.color("yellow")
    window.rect(140, 160, 40, 35)      // a window
    window.circle(340, 60, 30)         // the sun
    window.color("dark")
    window.rect(220, 180, 35, 60)      // the door
    window.wait()
}
Commands in this program (9)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • window.background(name) Paints the whole window in one colour, behind everything else. Without it the window is dark. Usually you set it once, right after new.window.
  • window.color(name) Picks the colour for everything you draw after it: text, lines, rectangles and circles. Think of it as picking up a different pen; it stays until you pick another. The name is a colour word like "red" or "green" (see the colour table), or your own colour as "#rrggbb".
  • window.rect(x, y, width, height) Draws a filled rectangle (a box), filled with the current colour (window.color). x and y say where its top-left corner is: x pixels from the left edge, y pixels down from the top. width says how wide the box is and height how tall, in pixels. Example: window.rect(10, 20, 100, 50) draws a box 100 wide and 50 tall whose corner is 10 from the left and 20 from the top. Use it for walls, bars, buttons, a player, the ground.
  • window.line(x1, y1, x2, y2) Draws a straight line from one point to another, in the current colour (window.color). The first point is x1, y1 and the second is x2, y2, each counted in pixels from the window's top-left corner. Example: window.line(0, 0, 100, 100) goes from the corner diagonally down to the right.
  • window.circle(x, y, radius) Draws a filled circle (a disc) in the current colour. x and y are the centre of the circle (not its corner, unlike rect), and radius is how far the edge is from the centre, so the circle is radius * 2 pixels wide. Example: window.circle(200, 150, 30) draws a circle 60 wide around the point 200, 150.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.

Try: move the sun with its first two numbers, make the house wider, add a second window. A loop can draw many shapes: for (i in 0 to 10) { window.circle(20 + i * 40, 280, 10) } makes a row of dots.

5. Asking the user: input and buttons

window.input("prompt", x, y) shows a typing box and waits until the user presses Enter; it gives back the text. window.button("text", x, y) puts a button in the window once, and window.clicked("text") is true one time after each click, so you check it inside a loop. window.alert("…") shows a message with OK; window.ask("…") shows Yes and No and gives back true for Yes.

use gui

fun main() {
    window = new.window("Questions", 420, 260)
    string name = window.input("Your name: ", 20, 20)
    window.print("Hi " + name + "! Press a button.", 20, 70)
    window.button("Say hi", 20, 110)
    window.button("Quit", 130, 110)
    while (window.open()) {
        if (window.clicked("Say hi")) {
            window.alert("Hi, " + name + "!")
        }
        if (window.clicked("Quit")) {
            if (window.ask("Really quit?")) { break }
        }
        window.sleep(0.02)
    }
    window.close()
}
Commands in this program (11)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • window.input(prompt) / window.input(prompt, x, y) Asks the user to type something. It shows the prompt text with a typing box next to it, waits until the user presses Enter, and gives back what they typed, as text. Your program stops on this line until then. x and y choose where the prompt and box appear.
  • window.print(text) / window.print(text, x, y) Writes text inside the window, like printl does in the console. With x and y you choose where: x is how many pixels from the left edge, y how many pixels down from the top edge, and that point is the top-left corner of the text. Without x and y, each print goes on the next line under the last one (or where window.goto said). The text stays there until window.clear().
  • window.button(text, x, y) / window.clicked(text) window.button puts a clickable button in the window, with text written on it, at x pixels from the left and y pixels from the top; you make it once. window.clicked("text") tells you whether that button was clicked: it is true one time after each click and false otherwise, so you ask it again and again inside a loop.
  • while (condition) Repeats a block as long as the condition is true. break leaves the loop at once, continue skips to the next round.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.
  • if / else if / else Runs a block only when a condition is true. else if tries another condition when the first was false, else runs when none was true. The condition goes in ( ), the block in { }.
  • window.alert(text) / window.ask(text) Pop-up messages. window.alert shows a small box with your text and an OK button, and waits until OK is pressed. window.ask shows your question with Yes and No buttons, waits for the answer, and gives back true for Yes and false for No.
  • window.sleep(seconds) Waits a short time while keeping the window alive, and shows what you have drawn so far. In a window loop always use this one, never plain sleep(), because plain sleep freezes the window. window.sleep(0.02) waits 0.02 seconds, which makes 50 pictures (frames) a second: a smaller number makes a game faster.

Why the loop? A button does nothing on its own: the program has to look at it again and again. while (window.open()) keeps going until the window is closed, and window.sleep(0.02) keeps the window responsive between the checks.

6. Animation: the frame loop

Films are many still pictures shown quickly. A moving window program does the same: every round of the loop draws one picture, a frame. Every frame has three steps, always in this order:

  1. Update the numbers: positions, speeds, the score.
  2. Draw the whole picture again: window.clear() wipes the old one, then draw everything at its new place.
  3. Show and wait: window.sleep(0.02) shows the new frame all at once (so nothing flickers) and waits 0.02 seconds, which makes 50 frames a second.
use gui

fun main() {
    window = new.window("Bouncing ball", 400, 300)
    x = 50.0
    y = 50.0
    dx = 4.0      // how far the ball moves each frame
    dy = 3.0
    while (window.open()) {
        // 1. update: move, and turn around at the edges
        x += dx
        y += dy
        if (x < 20 or x > 380) { dx = -dx }
        if (y < 20 or y > 280) { dy = -dy }
        // 2. draw the whole picture again
        window.clear()
        window.color("orange")
        window.circle(x, y, 20)
        // 3. show it and wait a moment: 0.02 s = 50 frames a second
        window.sleep(0.02)
    }
}
Commands in this program (10)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • while (condition) Repeats a block as long as the condition is true. break leaves the loop at once, continue skips to the next round.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.
  • if / else if / else Runs a block only when a condition is true. else if tries another condition when the first was false, else runs when none was true. The condition goes in ( ), the block in { }.
  • window.clear() Wipes the window clean: all text, shapes, pictures and buttons disappear, and only the background colour is left. Moving pictures use it at the start of every frame: clear, draw everything at its new place, then window.sleep shows the new picture all at once, so nothing flickers.
  • window.color(name) Picks the colour for everything you draw after it: text, lines, rectangles and circles. Think of it as picking up a different pen; it stays until you pick another. The name is a colour word like "red" or "green" (see the colour table), or your own colour as "#rrggbb".
  • window.circle(x, y, radius) Draws a filled circle (a disc) in the current colour. x and y are the centre of the circle (not its corner, unlike rect), and radius is how far the edge is from the centre, so the circle is radius * 2 pixels wide. Example: window.circle(200, 150, 30) draws a circle 60 wide around the point 200, 150.
  • window.sleep(seconds) Waits a short time while keeping the window alive, and shows what you have drawn so far. In a window loop always use this one, never plain sleep(), because plain sleep freezes the window. window.sleep(0.02) waits 0.02 seconds, which makes 50 pictures (frames) a second: a smaller number makes a game faster.

Try: change dx and dy for speed, the radius for size, and window.sleep(0.05) for a slower film. Forget window.clear() and the ball leaves a trail, because the old frames are never wiped.

7. The keyboard

There are two ways to read keys, for two different jobs:

  • window.key_down("left") is true while the key is held. Use it for movement: holding the arrow keeps moving.
  • window.key() gives the last key pressed since you last asked, once per press, or "" if none. Use it for single actions: jump, pause, quit, menus. Read it once per frame into a variable, as below, because asking again gives "".
use gui

fun main() {
    window = new.window("Move the square", 400, 300)
    x = 180
    y = 130
    color = "green"
    while (window.open()) {
        // held keys: keep moving while the arrow is down
        if (window.key_down("left")) { x -= 4 }
        if (window.key_down("right")) { x += 4 }
        if (window.key_down("up")) { y -= 4 }
        if (window.key_down("down")) { y += 4 }
        // single presses: one change per press
        key = window.key()
        if (key == "space") { color = color == "green" ? "purple" : "green" }
        if (key == "escape") { break }
        window.clear()
        window.color(color)
        window.rect(x, y, 40, 40)
        window.color("white")
        window.print("arrows move, space changes colour, escape quits", 10, 10)
        window.sleep(0.02)
    }
    window.close()
}
Commands in this program (13)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • while (condition) Repeats a block as long as the condition is true. break leaves the loop at once, continue skips to the next round.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.
  • if / else if / else Runs a block only when a condition is true. else if tries another condition when the first was false, else runs when none was true. The condition goes in ( ), the block in { }.
  • window.key_down(name) Tells you whether one key is being held down right now: true while it is held, false when it is not. Use it for movement, so holding an arrow keeps moving. The name is the key's lowercase name, like "left", "up", "space" or "a".
  • window.key() Tells you which key was pressed: it gives back the key's name, like "a", "space" or "left", or "" (empty text) if nothing was pressed since you last asked. Each press is given only once, which is right for single actions like jump, pause or quit. Key names are lowercase: letters, digits, enter, space, escape, left, right, up, down, backspace, tab, shift, ctrl, alt, delete, home, end, f1 and so on.
  • window.clear() Wipes the window clean: all text, shapes, pictures and buttons disappear, and only the background colour is left. Moving pictures use it at the start of every frame: clear, draw everything at its new place, then window.sleep shows the new picture all at once, so nothing flickers.
  • window.color(name) Picks the colour for everything you draw after it: text, lines, rectangles and circles. Think of it as picking up a different pen; it stays until you pick another. The name is a colour word like "red" or "green" (see the colour table), or your own colour as "#rrggbb".
  • window.rect(x, y, width, height) Draws a filled rectangle (a box), filled with the current colour (window.color). x and y say where its top-left corner is: x pixels from the left edge, y pixels down from the top. width says how wide the box is and height how tall, in pixels. Example: window.rect(10, 20, 100, 50) draws a box 100 wide and 50 tall whose corner is 10 from the left and 20 from the top. Use it for walls, bars, buttons, a player, the ground.
  • window.print(text) / window.print(text, x, y) Writes text inside the window, like printl does in the console. With x and y you choose where: x is how many pixels from the left edge, y how many pixels down from the top edge, and that point is the top-left corner of the text. Without x and y, each print goes on the next line under the last one (or where window.goto said). The text stays there until window.clear().
  • window.sleep(seconds) Waits a short time while keeping the window alive, and shows what you have drawn so far. In a window loop always use this one, never plain sleep(), because plain sleep freezes the window. window.sleep(0.02) waits 0.02 seconds, which makes 50 pictures (frames) a second: a smaller number makes a game faster.

Key names are lowercase words: "a", "7", "space", "enter", "escape", the arrows "left" "right" "up" "down". The full list is below. Click the window first so it gets the keys.

8. The mouse

window.mouse_x() and window.mouse_y() say where the pointer is inside the window; window.mouse_down() is true while the left button is held. On a phone, touching the window counts as the mouse. A click is the moment the button goes from up to down, so the program remembers the last frame's state in was_down:

use gui, random

fun main() {
    window = new.window("Click the circle", 400, 300)
    tx = 200
    ty = 150
    hits = 0
    was_down = false
    while (window.open()) {
        down = window.mouse_down()
        // a click is the moment the button goes down
        if (down and not was_down) {
            dx = window.mouse_x() - tx
            dy = window.mouse_y() - ty
            if (dx * dx + dy * dy < 30 * 30) {   // inside the circle (Pythagoras)
                hits += 1
                tx = random(40 to 360)
                ty = random(40 to 260)
            }
        }
        was_down = down
        window.clear()
        window.color("red")
        window.circle(tx, ty, 30)
        window.color("white")
        window.print("hits: {hits}", 10, 10)
        window.sleep(0.02)
    }
}
Commands in this program (13)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • while (condition) Repeats a block as long as the condition is true. break leaves the loop at once, continue skips to the next round.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.
  • window.mouse_x() / window.mouse_y() / window.mouse_down() Tell you about the mouse: mouse_x is how many pixels the pointer is from the window's left edge, mouse_y how many from the top edge, and mouse_down is true while the left mouse button is held down. On a phone, a finger touching the window counts as the mouse.
  • if / else if / else Runs a block only when a condition is true. else if tries another condition when the first was false, else runs when none was true. The condition goes in ( ), the block in { }.
  • random(a to b) A whole number from a to b, and both ends can come up. This is the one you want for dice, for picking a position, or any "choose a number" job. Every run gives different results.
  • window.clear() Wipes the window clean: all text, shapes, pictures and buttons disappear, and only the background colour is left. Moving pictures use it at the start of every frame: clear, draw everything at its new place, then window.sleep shows the new picture all at once, so nothing flickers.
  • window.color(name) Picks the colour for everything you draw after it: text, lines, rectangles and circles. Think of it as picking up a different pen; it stays until you pick another. The name is a colour word like "red" or "green" (see the colour table), or your own colour as "#rrggbb".
  • window.circle(x, y, radius) Draws a filled circle (a disc) in the current colour. x and y are the centre of the circle (not its corner, unlike rect), and radius is how far the edge is from the centre, so the circle is radius * 2 pixels wide. Example: window.circle(200, 150, 30) draws a circle 60 wide around the point 200, 150.
  • window.print(text) / window.print(text, x, y) Writes text inside the window, like printl does in the console. With x and y you choose where: x is how many pixels from the left edge, y how many pixels down from the top edge, and that point is the top-left corner of the text. Without x and y, each print goes on the next line under the last one (or where window.goto said). The text stays there until window.clear().
  • window.sleep(seconds) Waits a short time while keeping the window alive, and shows what you have drawn so far. In a window loop always use this one, never plain sleep(), because plain sleep freezes the window. window.sleep(0.02) waits 0.02 seconds, which makes 50 pictures (frames) a second: a smaller number makes a game faster.

Without window.clear() every frame adds to the picture, which makes a simple paint program:

use gui

fun main() {
    window = new.window("Paint", 400, 300)
    window.print("hold the mouse button and draw", 10, 10)
    while (window.open()) {
        if (window.mouse_down()) {
            window.color("yellow")
            window.circle(window.mouse_x(), window.mouse_y(), 6)
        }
        window.sleep(0.01)
    }
}
Commands in this program (11)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • window.print(text) / window.print(text, x, y) Writes text inside the window, like printl does in the console. With x and y you choose where: x is how many pixels from the left edge, y how many pixels down from the top edge, and that point is the top-left corner of the text. Without x and y, each print goes on the next line under the last one (or where window.goto said). The text stays there until window.clear().
  • while (condition) Repeats a block as long as the condition is true. break leaves the loop at once, continue skips to the next round.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.
  • if / else if / else Runs a block only when a condition is true. else if tries another condition when the first was false, else runs when none was true. The condition goes in ( ), the block in { }.
  • window.mouse_x() / window.mouse_y() / window.mouse_down() Tell you about the mouse: mouse_x is how many pixels the pointer is from the window's left edge, mouse_y how many from the top edge, and mouse_down is true while the left mouse button is held down. On a phone, a finger touching the window counts as the mouse.
  • window.color(name) Picks the colour for everything you draw after it: text, lines, rectangles and circles. Think of it as picking up a different pen; it stays until you pick another. The name is a colour word like "red" or "green" (see the colour table), or your own colour as "#rrggbb".
  • window.circle(x, y, radius) Draws a filled circle (a disc) in the current colour. x and y are the centre of the circle (not its corner, unlike rect), and radius is how far the edge is from the centre, so the circle is radius * 2 pixels wide. Example: window.circle(200, 150, 30) draws a circle 60 wide around the point 200, 150.
  • window.sleep(seconds) Waits a short time while keeping the window alive, and shows what you have drawn so far. In a window loop always use this one, never plain sleep(), because plain sleep freezes the window. window.sleep(0.02) waits 0.02 seconds, which makes 50 pictures (frames) a second: a smaller number makes a game faster.

9. Pictures

window.image("file.png", x, y, width, height) draws a picture file that sits next to your program: png, jpg, bmp or gif, transparency included. Leave out the size to get the picture's own size. The file is loaded once and kept, so drawing it in every frame is fast. In the playground city.png and bird.png are ready to use, and the Add image button above the window brings in your own pictures.

use gui

fun main() {
    window = new.window("Pictures", 480, 640)
    y = 400.0
    while (window.open()) {
        y -= 2
        if (y < -40) { y = 640 }
        window.clear()
        window.image("city.png", 0, 0, 480, 640)   // fills the window
        window.image("bird.png", 200, y, 48, 36)   // drawn on top, moving up
        window.sleep(0.02)
    }
}
Commands in this program (9)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • while (condition) Repeats a block as long as the condition is true. break leaves the loop at once, continue skips to the next round.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.
  • if / else if / else Runs a block only when a condition is true. else if tries another condition when the first was false, else runs when none was true. The condition goes in ( ), the block in { }.
  • window.clear() Wipes the window clean: all text, shapes, pictures and buttons disappear, and only the background colour is left. Moving pictures use it at the start of every frame: clear, draw everything at its new place, then window.sleep shows the new picture all at once, so nothing flickers.
  • window.image(file, x, y, width, height) Draws a picture (a png, jpg, bmp or gif file next to your program) in the window. x and y are where its top-left corner goes; width and height stretch it to that size, or leave them out to get the picture's own size. Transparent parts stay see-through. In the playground city.png and bird.png are ready to use, and the Add image button brings in your own.
  • window.sleep(seconds) Waits a short time while keeping the window alive, and shows what you have drawn so far. In a window loop always use this one, never plain sleep(), because plain sleep freezes the window. window.sleep(0.02) waits 0.02 seconds, which makes 50 pictures (frames) a second: a smaller number makes a game faster.

10. A whole game: catch the stars

Everything together: a basket moved by the arrow keys (or the mouse, or your finger), a falling star that is placed at a random spot, a score, a game that gets faster, and a message at the end. Read it top to bottom: it is just the frame loop from lesson 6 with more things to update and draw.

use gui, random

fun main() {
    window = new.window("Catch the stars", 400, 500)
    x = 170                 // the basket
    star_x = random(20 to 380)
    star_y = 0.0
    speed = 3.0
    score = 0
    while (window.open()) {
        // move: arrow keys, or hold the mouse / your finger where the basket should go
        if (window.key_down("left") and x > 0) { x -= 6 }
        if (window.key_down("right") and x < 340) { x += 6 }
        if (window.mouse_down()) { x = window.mouse_x() - 30 }
        // the star falls a bit faster after every catch
        star_y += speed
        if (star_y > 450 and star_x > x and star_x < x + 60) {
            score += 1
            speed += 0.3
            star_y = 0
            star_x = random(20 to 380)
        }
        if (star_y > 500) {
            window.alert("Game over! You caught {score} stars.")
            break
        }
        window.clear()
        window.color("yellow")
        window.circle(star_x, star_y, 10)
        window.color("orange")
        window.rect(x, 460, 60, 15)
        window.color("white")
        window.print("score: {score}", 10, 10)
        window.sleep(0.02)
    }
    window.close()
}
Commands in this program (16)
  • use basic, map, random, chrono, gui The first line of a file says which parts of the language it needs: basic (printing, input, text, numbers, lists, files), map, random, chrono (time and sleep) and gui (windows). A file without any use line gets everything except gui. Fewer modules = a faster compile.
  • fun name(a, b) Makes your own function: a named block you can call again and again. Parameters receive the values you call it with; return gives a value back. Every program needs a fun main(), where it starts.
  • new.window(title, width, height) Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).
  • random(a to b) A whole number from a to b, and both ends can come up. This is the one you want for dice, for picking a position, or any "choose a number" job. Every run gives different results.
  • while (condition) Repeats a block as long as the condition is true. break leaves the loop at once, continue skips to the next round.
  • window.open() / window.wait() / window.close() window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.
  • if / else if / else Runs a block only when a condition is true. else if tries another condition when the first was false, else runs when none was true. The condition goes in ( ), the block in { }.
  • window.key_down(name) Tells you whether one key is being held down right now: true while it is held, false when it is not. Use it for movement, so holding an arrow keeps moving. The name is the key's lowercase name, like "left", "up", "space" or "a".
  • window.mouse_x() / window.mouse_y() / window.mouse_down() Tell you about the mouse: mouse_x is how many pixels the pointer is from the window's left edge, mouse_y how many from the top edge, and mouse_down is true while the left mouse button is held down. On a phone, a finger touching the window counts as the mouse.
  • window.alert(text) / window.ask(text) Pop-up messages. window.alert shows a small box with your text and an OK button, and waits until OK is pressed. window.ask shows your question with Yes and No buttons, waits for the answer, and gives back true for Yes and false for No.
  • window.clear() Wipes the window clean: all text, shapes, pictures and buttons disappear, and only the background colour is left. Moving pictures use it at the start of every frame: clear, draw everything at its new place, then window.sleep shows the new picture all at once, so nothing flickers.
  • window.color(name) Picks the colour for everything you draw after it: text, lines, rectangles and circles. Think of it as picking up a different pen; it stays until you pick another. The name is a colour word like "red" or "green" (see the colour table), or your own colour as "#rrggbb".
  • window.circle(x, y, radius) Draws a filled circle (a disc) in the current colour. x and y are the centre of the circle (not its corner, unlike rect), and radius is how far the edge is from the centre, so the circle is radius * 2 pixels wide. Example: window.circle(200, 150, 30) draws a circle 60 wide around the point 200, 150.
  • window.rect(x, y, width, height) Draws a filled rectangle (a box), filled with the current colour (window.color). x and y say where its top-left corner is: x pixels from the left edge, y pixels down from the top. width says how wide the box is and height how tall, in pixels. Example: window.rect(10, 20, 100, 50) draws a box 100 wide and 50 tall whose corner is 10 from the left and 20 from the top. Use it for walls, bars, buttons, a player, the ground.
  • window.print(text) / window.print(text, x, y) Writes text inside the window, like printl does in the console. With x and y you choose where: x is how many pixels from the left edge, y how many pixels down from the top edge, and that point is the top-left corner of the text. Without x and y, each print goes on the next line under the last one (or where window.goto said). The text stays there until window.clear().
  • window.sleep(seconds) Waits a short time while keeping the window alive, and shows what you have drawn so far. In a window loop always use this one, never plain sleep(), because plain sleep freezes the window. window.sleep(0.02) waits 0.02 seconds, which makes 50 pictures (frames) a second: a smaller number makes a game faster.

Ideas to make it yours: two stars at once (a list of positions), a life counter instead of instant game over, a picture for the basket with window.image, a high score with write_file.

Common mistakes

  • The window flashes and disappears. main ended. Add window.wait(), or a while (window.open()) loop.
  • The window freezes. Use window.sleep(…) in window loops, never plain sleep(…): only the window's own sleep keeps it reacting and shows your drawing.
  • Things smear across the window. Call window.clear() at the start of every frame.
  • The circle is in the wrong place. Circles are placed by their centre, rectangles and text by their top-left corner.
  • A key does nothing. Key names are lowercase ("left", not "Left"), the window needs to be clicked once to get the keyboard, and window.key() gives each press only once.
  • "needs `use gui`". Windows are never on by default: the first line must be use gui (add others with commas: use gui, random).

Every window command

Each command with an explanation and three small examples. The examples are the inside of fun main() with use gui at the top; the Run link opens each one as a whole program. In the console, comet help window.rect (or any other command) shows the same.

new.window(title, width, height)

Opens a new window on the screen and gives it back to you. title is the text in the window's top bar, width and height are its size in pixels (the tiny dots of the screen); leave them out for 640 wide and 480 high. Keep the window in a variable, usually called window: every other window command is written after it, like window.print(...), window.rect(...).

a small window
window = new.window("My app", 400, 300)
window.wait()
the default size
window = new.window("Big")
window.wait()
two windows
one = new.window("One", 200, 100)
two = new.window("Two", 200, 100)
one.print("first")
two.print("second")
one.wait()
window.print(text) / window.print(text, x, y)

Writes text inside the window, like printl does in the console. With x and y you choose where: x is how many pixels from the left edge, y how many pixels down from the top edge, and that point is the top-left corner of the text. Without x and y, each print goes on the next line under the last one (or where window.goto said). The text stays there until window.clear().

line under line
window = new.window("Text")
window.print("first")
window.print("second")
window.wait()
at a position
window = new.window("Text")
window.print("over here", 200, 100)
window.wait()
numbers too
window = new.window("Score")
score = 12
window.print("Score: {score}", 10, 10)
window.wait()
window.goto(x, y)

Moves the writing position: the next window.print or window.input without its own x and y starts at this point (x pixels from the left, y pixels from the top). After each print the position moves one line down, so a few prints after one goto make a neat column.

start somewhere else
window = new.window("Goto")
window.goto(50, 80)
window.print("at 50, 80")
window.wait()
a column of lines
window = new.window("Goto")
window.goto(100, 20)
window.print("one")
window.print("two")
window.wait()
a table with two columns
window = new.window("Table")
window.goto(20, 20)
window.print("name")
window.goto(150, 20)
window.print("score")
window.wait()
window.color(name)

Picks the colour for everything you draw after it: text, lines, rectangles and circles. Think of it as picking up a different pen; it stays until you pick another. The name is a colour word like "red" or "green" (see the colour table), or your own colour as "#rrggbb".

coloured text
window = new.window("Colours")
window.color("yellow")
window.print("yellow text")
window.wait()
your own colour
window = new.window("Colours")
window.color("#ff8800")
window.print("orange text")
window.wait()
coloured shapes
window = new.window("Colours")
window.color("red")
window.rect(20, 20, 50, 50)
window.color("blue")
window.circle(120, 45, 25)
window.wait()
window.background(name)

Paints the whole window in one colour, behind everything else. Without it the window is dark. Usually you set it once, right after new.window.

blue
window = new.window("Blue")
window.background("blue")
window.wait()
light with dark text
window = new.window("Light")
window.background("white")
window.color("black")
window.print("dark on light")
window.wait()
your own colour
window = new.window("Sea")
window.background("#004466")
window.wait()
window.input(prompt) / window.input(prompt, x, y)

Asks the user to type something. It shows the prompt text with a typing box next to it, waits until the user presses Enter, and gives back what they typed, as text. Your program stops on this line until then. x and y choose where the prompt and box appear.

ask a name
window = new.window("Ask")
string name = window.input("name: ")
window.print("hi " + name)
window.wait()
at a position
window = new.window("Ask")
string age = window.input("age: ", 50, 100)
window.print("next year: " + (num(age) + 1), 50, 140)
window.wait()
two questions
window = new.window("Form")
first = window.input("first name: ")
last = window.input("last name: ")
window.print(first + " " + last)
window.wait()
window.button(text, x, y) / window.clicked(text)

window.button puts a clickable button in the window, with text written on it, at x pixels from the left and y pixels from the top; you make it once. window.clicked("text") tells you whether that button was clicked: it is true one time after each click and false otherwise, so you ask it again and again inside a loop.

a quit button
window = new.window("Button")
window.button("Quit", 20, 20)
while (window.open()) {
    if (window.clicked("Quit")) { break }
    window.sleep(0.02)
}
a click counter
window = new.window("Count")
window.button("Click me", 20, 20)
n = 0
while (window.open()) {
    if (window.clicked("Click me")) {
        n += 1
        window.title("clicks: {n}")
    }
    window.sleep(0.02)
}
two buttons
window = new.window("Choose")
window.button("Yes", 20, 20)
window.button("No", 100, 20)
while (window.open()) {
    if (window.clicked("Yes")) { window.alert("yes!") }
    if (window.clicked("No")) { window.alert("no!") }
    window.sleep(0.02)
}
window.image(file, x, y, width, height)

Draws a picture (a png, jpg, bmp or gif file next to your program) in the window. x and y are where its top-left corner goes; width and height stretch it to that size, or leave them out to get the picture's own size. Transparent parts stay see-through. In the playground city.png and bird.png are ready to use, and the Add image button brings in your own.

a background filling the window
window = new.window("City", 480, 640)
window.image("city.png", 0, 0, 480, 640)
window.wait()
a picture on top, with its transparency
window = new.window("Bird", 480, 640)
window.image("city.png", 0, 0, 480, 640)
window.image("bird.png", 100, 200)
window.wait()
a picture that moves
window = new.window("Fly", 480, 300)
for (x in 0 to 400 step 4) {
    window.clear()
    window.image("bird.png", x, 100, 48, 36)
    window.sleep(0.02)
}
window.line(x1, y1, x2, y2)

Draws a straight line from one point to another, in the current colour (window.color). The first point is x1, y1 and the second is x2, y2, each counted in pixels from the window's top-left corner. Example: window.line(0, 0, 100, 100) goes from the corner diagonally down to the right.

one line
window = new.window("Line")
window.line(0, 0, 200, 100)
window.wait()
a coloured cross
window = new.window("Cross")
window.color("red")
window.line(50, 50, 150, 150)
window.line(150, 50, 50, 150)
window.wait()
a fan of lines
window = new.window("Fan")
for (x in 0 to 300 step 30) {
    window.line(150, 200, x, 0)
}
window.wait()
window.rect(x, y, width, height)

Draws a filled rectangle (a box), filled with the current colour (window.color). x and y say where its top-left corner is: x pixels from the left edge, y pixels down from the top. width says how wide the box is and height how tall, in pixels. Example: window.rect(10, 20, 100, 50) draws a box 100 wide and 50 tall whose corner is 10 from the left and 20 from the top. Use it for walls, bars, buttons, a player, the ground.

one rectangle
window = new.window("Rect")
window.color("red")
window.rect(20, 20, 100, 50)
window.wait()
a row of bars
window = new.window("Bars")
for (i in 0 to 5) {
    window.rect(20 + i * 40, 150 - i * 20, 30, i * 20 + 10)
}
window.wait()
a chessboard
window = new.window("Board", 200, 200)
for (y in 0 to 4) {
    for (x in 0 to 4) {
        if ((x + y) % 2 == 0) { window.rect(x * 50, y * 50, 50, 50) }
    }
}
window.wait()
window.circle(x, y, radius)

Draws a filled circle (a disc) in the current colour. x and y are the centre of the circle (not its corner, unlike rect), and radius is how far the edge is from the centre, so the circle is radius * 2 pixels wide. Example: window.circle(200, 150, 30) draws a circle 60 wide around the point 200, 150.

one circle
window = new.window("Circle")
window.color("green")
window.circle(100, 100, 40)
window.wait()
a row of circles
window = new.window("Row")
for (i in 0 to 5) {
    window.circle(40 + i * 60, 80, 20)
}
window.wait()
a target
window = new.window("Target")
window.color("red")
window.circle(100, 100, 60)
window.color("white")
window.circle(100, 100, 40)
window.color("red")
window.circle(100, 100, 20)
window.wait()
window.clear()

Wipes the window clean: all text, shapes, pictures and buttons disappear, and only the background colour is left. Moving pictures use it at the start of every frame: clear, draw everything at its new place, then window.sleep shows the new picture all at once, so nothing flickers.

wipe after a moment
window = new.window("Clear")
window.print("gone in a second")
window.sleep(1)
window.clear()
window.wait()
a moving square
window = new.window("Move")
for (x in 0 to 300 step 3) {
    window.clear()
    window.rect(x, 50, 20, 20)
    window.sleep(0.01)
}
a countdown
window = new.window("Count")
for (i in 3 to 0 step -1) {
    window.clear()
    window.print("{i}", 100, 100)
    window.sleep(1)
}
window.key()

Tells you which key was pressed: it gives back the key's name, like "a", "space" or "left", or "" (empty text) if nothing was pressed since you last asked. Each press is given only once, which is right for single actions like jump, pause or quit. Key names are lowercase: letters, digits, enter, space, escape, left, right, up, down, backspace, tab, shift, ctrl, alt, delete, home, end, f1 and so on.

show each key
window = new.window("Keys")
while (window.open()) {
    k = window.key()
    if (k != "") { window.print(k) }
    window.sleep(0.02)
}
quit with escape
window = new.window("Press escape")
while (window.open()) {
    if (window.key() == "escape") { break }
    window.sleep(0.02)
}
jump on space
window = new.window("Jump")
jumps = 0
while (window.open()) {
    if (window.key() == "space") {
        jumps += 1
        window.title("jumps: {jumps}")
    }
    window.sleep(0.02)
}
window.key_down(name)

Tells you whether one key is being held down right now: true while it is held, false when it is not. Use it for movement, so holding an arrow keeps moving. The name is the key's lowercase name, like "left", "up", "space" or "a".

move left and right
window = new.window("Hold")
x = 100
while (window.open()) {
    if (window.key_down("left")) { x -= 3 }
    if (window.key_down("right")) { x += 3 }
    window.clear()
    window.rect(x, 100, 30, 30)
    window.sleep(0.02)
}
all four arrows
window = new.window("Move")
x = 100
y = 100
while (window.open()) {
    if (window.key_down("left")) { x -= 3 }
    if (window.key_down("right")) { x += 3 }
    if (window.key_down("up")) { y -= 3 }
    if (window.key_down("down")) { y += 3 }
    window.clear()
    window.circle(x, y, 15)
    window.sleep(0.02)
}
letter keys
window = new.window("WASD")
x = 100
while (window.open()) {
    if (window.key_down("a")) { x -= 3 }
    if (window.key_down("d")) { x += 3 }
    window.clear()
    window.rect(x, 80, 30, 30)
    window.sleep(0.02)
}
window.mouse_x() / window.mouse_y() / window.mouse_down()

Tell you about the mouse: mouse_x is how many pixels the pointer is from the window's left edge, mouse_y how many from the top edge, and mouse_down is true while the left mouse button is held down. On a phone, a finger touching the window counts as the mouse.

paint with the mouse
window = new.window("Paint")
while (window.open()) {
    if (window.mouse_down()) { window.circle(window.mouse_x(), window.mouse_y(), 5) }
    window.sleep(0.01)
}
a circle follows the mouse
window = new.window("Follow")
while (window.open()) {
    window.clear()
    window.circle(window.mouse_x(), window.mouse_y(), 20)
    window.sleep(0.01)
}
show the position
window = new.window("Where")
while (window.open()) {
    window.title("{window.mouse_x()}, {window.mouse_y()}")
    window.sleep(0.05)
}
window.alert(text) / window.ask(text)

Pop-up messages. window.alert shows a small box with your text and an OK button, and waits until OK is pressed. window.ask shows your question with Yes and No buttons, waits for the answer, and gives back true for Yes and false for No.

a message
window = new.window("Hi")
window.alert("Saved!")
a question
window = new.window("Save")
if (window.ask("Save the file?")) {
    window.alert("Saved!")
}
play again?
window = new.window("Game")
while (window.ask("Play again?")) {
    window.alert("Playing…")
}
window.sleep(seconds)

Waits a short time while keeping the window alive, and shows what you have drawn so far. In a window loop always use this one, never plain sleep(), because plain sleep freezes the window. window.sleep(0.02) waits 0.02 seconds, which makes 50 pictures (frames) a second: a smaller number makes a game faster.

wait a moment
window = new.window("Wait")
window.print("one moment")
window.sleep(1)
the heart of a game loop
window = new.window("Loop")
while (window.open()) {
    window.sleep(0.02)
}
a slow appearing list
window = new.window("Slow")
for (i in 0 to 5) {
    window.print("line {i}")
    window.sleep(0.3)
}
window.wait()
window.open() / window.wait() / window.close()

window.open() is true while the window is still there and turns false when the user closes it; that is why loops say while (window.open()). window.wait() just waits until the user closes the window, for programs that show something and are done. window.close() closes the window from your program.

show and wait
window = new.window("Stay")
window.print("close me")
window.wait()
a loop that ends with the window
window = new.window("Loop")
while (window.open()) {
    window.sleep(0.05)
}
printl("the window was closed")
close it yourself
window = new.window("Short")
window.sleep(1)
window.close()
window.title(text) / window.width() / window.height()

window.title changes the text in the window's top bar. window.width() and window.height() give the size of the inside of the window in pixels, so window.width() / 2 is always the middle, even after the user made the window bigger.

change the title
window = new.window("Old")
window.title("New")
window.wait()
show the size
window = new.window("Size", 300, 200)
window.title("{window.width()} x {window.height()}")
window.wait()
centre a circle
window = new.window("Centre", 300, 200)
window.circle(window.width() / 2, window.height() / 2, 30)
window.wait()

Key names

For window.key() and window.key_down(name):

NameKey
a … zletters (always lowercase, also with Shift)
0 … 9digits
left right up downthe arrow keys
spacethe space bar
enterEnter / Return
escapeEsc
backspacethe key that deletes back
tabTab
shift ctrl altthe modifier keys
delete home endediting keys
f1 … f12function keys

Colour names

For window.color, window.background and the console's color. Any other colour: "#rrggbb".

NameColour
"white"#ffffff
"black"#000000
"red"#ff5050
"green"#3fd77f
"blue"#4c9dff
"yellow"#f2d633
"orange"#ffa032
"purple"#be78ff
"pink"#ff78c8
"gray" / "grey"#8c8c8c
"brown"#966432
"cyan"#50dce6
"dark"#121212
"light"#ececec