Back

Section Summary: Motion and Decisions

divider

Activities 1.7 – 1.14. Use this to review, or to fill in something you missed.


1. Motion Is Change Between Frames

love.draw was always running sixty times a second; the picture looked still because every frame was identical. love.update is a second box that runs first, and changing a variable there is what makes something move.

Frame
One pass of update and draw. About sixty per second.
love.update
Where things change. Runs before love.draw every frame.
dt
Seconds since the last frame, measured by the engine. About 0.0167.
Frame rate
How many frames a computer manages per second. Not the same on every machine.

A variable that has to survive between frames lives above both boxes. Declared inside love.update, it is remade every frame and nothing ever moves.

x = x + 2 is two pixels per frame, so the program runs faster on a faster computer. x = x + speed * dt is pixels per second on every machine.


2. Booleans

A comparison produces a boolean, and a boolean is a value like any other. There are exactly two of them.

QuestionLuaExam notation
Greater thana > ba > b
Greater or equala >= ba ≥ b
Is equal toa == ba = b
Is not equal toa ~= ba ≠ b

The bottom two rows are the ones that cost marks. The exam writes equality as one =, which in Lua means assignment. It gets away with that because assignment on the exam is always the arrow.

Boolean
A value that is true or false. Not the word in quotes.
tostring
Turns any value into text. Needed before a boolean can be drawn or joined with ...

Never ask == about a decimal you calculated. 0.1 + 0.2 prints as 0.3 and compares as not equal to 0.3. Use > or <.


3. Decisions

An if runs its block only when the condition is true. With an else, exactly one of the two blocks runs — never both, never neither.

Condition
The true-or-false question an if asks.
Branch
A block that runs only in some cases.
Selection
The exam's word for choosing which lines run.
elseif
A chain. The first true condition wins and the rest are skipped.

The exam sheet has no ELSE IF. There are two selection forms and no more: IF and IF / ELSE. A chain is written by nesting the next IF inside the ELSE.

Order matters in a chain. A condition that is wider than the one above it can never be reached, and Lua will not warn you.


4. Edges, and Two Ways to Handle Them

A ball bounces by reversing its speed: speedX = -speedX. A player stops at a wall by overwriting its position: x = 0. Same shape of if, completely different behavior.

Test an edge with >, never ==. A moving value jumps several pixels per frame and steps straight over any exact number you name.

Account for the shape's size. A circle drawn from its center reaches the right wall at 800 - radius, not at 800.

Clamping
Forcing a value back inside a range when it goes past.

5. Combining Conditions

and
True only when both sides are true. AND on the exam.
or
True when either side is, or both. OR on the exam.
not
Flips a boolean. NOT on the exam.

not is also how you flip a variable: paused = not paused.


6. Input From a Person

Two ways, and they are not interchangeable.

love.keyboard.isDown
Asks whether a key is held right now. Produces a boolean, so it goes into an if. Used in love.update for movement.
love.keypressed
Called by the engine once, at the moment a key goes down. For jumping, pausing, firing.
love.mouse.getPosition
Hands back two values at once: x and y.
love.mousepressed
Called by the engine when a button goes down, and told where.

Using the wrong one is not an error. Movement written in love.keypressed moves once per press however long the key is held, which runs perfectly and feels broken.


7. Collision, and Counting Once

A program never looks at the picture. Two shapes overlap when four comparisons are all true — two across, two down — joined with and.

Asked every frame, is it touching is true for a few dozen frames in a row, so a counter inside it climbs by twenty per touch. The question you meant was did it just start touching:

if overlapping and not touching thencount it
touching = overlappingafterwards, always

The order of those two is load-bearing and silent. Put the bookkeeping line first and the condition reads overlapping and not overlapping, which is never true, and the counter never moves.

Collision detection
Working out whether two things overlap by comparing coordinates.
State
A variable whose only job is to let the program compare now against then.
Edge
The moment a condition changes from false to true.