Back

Section Summary: Getting Started

divider

Activities 1.1 – 1.6. Use this to review, or to fill in something you missed.


1. Programs and the Workspace

A program is a list of instructions carried out in order, and the computer does exactly what the list says. You copy a template folder for every new program rather than editing the original, and you never edit conf.lua.

Program
A list of instructions a computer carries out in order.
Template
A starting folder you copy once for each new program. There are two: console and game.
Terminal
The panel where output appears and where you type input.
print
Shows a line of text in the terminal.
Syntax error
The program is not valid Lua, so nothing runs at all. Nothing is printed.

2. Drawing

love.draw is called by the engine about sixty times a second. Coordinates start at the top left and y grows downward. Rectangles are placed by their top left corner; circles by their center.

Coordinate
A pair of numbers giving a position. (0, 0) is the top left of the window; the window is 800 by 600.
love.graphics.rectangle
Takes a mode, then x, y, width and height. The x and y are the corner.
love.graphics.circle
Takes a mode, then x, y and radius. The x and y are the center.
love.graphics.setColor
Takes red, green and blue as numbers from 0 to 1, not 0 to 255. It stays set until you change it.

The 0-to-255 mistake is silent. Values above 1 are clamped to 1, so a color meant to be gray comes out white and everything drawn after it does too.


3. Variables

A variable is a name holding a value. = is an instruction, not a claim: work out the right side, then put the answer in the name on the left. That is why score = score + 10 makes sense.

Variable
A name that holds a value.
local
Makes a new variable. Used once per variable, when it is created.
Assignment
Putting a value into a variable. Written = in Lua and on the exam.
Reassignment
Putting a different value into a variable that already exists. No local.
Hardcoded
A raw number typed straight into the program instead of named.

4. Input, and the Three Kinds of Wrong

io.read() always hands back a string, whatever the person typed. With 16 typed at a prompt and stored in age:

ExpressionWhat happens
age * 232 — it silently works
age == 16false — it silently lies
age > 10an error — it crashes

The fix is tonumber at the moment you collect the value. The silent lie is the dangerous one, because nothing tells you.

io.write
Prints without moving to a new line, so a prompt and the typed answer sit together.
io.read
Waits for a line to be typed and hands it back as a string.
tonumber
Turns a string into a number, or hands back nil if it cannot.
Type
What kind of value something is. So far: number, string, boolean.

5. Arithmetic

Normal order of operations, in Lua and on the exam. / never truncates17 / 5 is 3.4, not 3.

%
Remainder. 17 % 5 is 2. Written MOD on the exam, with the same precedence as * and /.
math.floor
Rounds down to a whole number. What you use when you want division to truncate.
Precedence
* and / before + and -. Parentheses beat everything.
Concatenation
.. joins two pieces of text. Not the same as adding.

6. Exam Notation From This Part

LuaExam notation
Make a variablelocal x = 400x ← 400
Change itx = x + 50x ← x + 50
Show a valueprint(x)DISPLAY(x)
Read a valueio.read()INPUT()
Remaindera % ba MOD b

Two things to remember about DISPLAY: it is how the exam prints, and it puts a space after what it shows. TwoDISPLAY calls in a row give you space-separated output on one line, not two lines.

There is no local on the exam. The arrow makes the variable the first time and changes it every time after.