Activities 1.7 – 1.14. Use this to review, or to fill in something you missed.
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.
love.updatelove.draw every frame.dtA 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.
A comparison produces a boolean, and a boolean is a value like any other. There are exactly two of them.
| Question | Lua | Exam notation |
|---|---|---|
| Greater than | a > b | a > b |
| Greater or equal | a >= b | a ≥ b |
| Is equal to | a == b | a = b |
| Is not equal to | a ~= b | a ≠ 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.
true or false. Not the word in quotes.tostring...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 <.
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.
if asks.elseifThe 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.
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.
andAND on the exam.orOR on the exam.notNOT on the exam.not is also how you flip a variable: paused = not paused.
Two ways, and they are not interchangeable.
love.keyboard.isDownif. Used in love.update for movement.love.keypressedlove.mouse.getPositionlove.mousepressedUsing 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.
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 then | count it |
touching = overlapping | afterwards, 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.