'F' → Fullscreen
Copy template-console to 1-08-compare. Write these six lines, and predict every answer before you run it:
print(7 > 7)print(7 >= 7)print(7 < 10)print(7 <= 3)print(7 == 7)print(7 ~= 7)Record all six on your sheet, with your predictions beside them. The first two are the pair worth thinking about.
Add two more lines to the same file:
print(0.1 + 0.2)print(0.1 + 0.2 == 0.3)0.3falseWrite both results down. One line prints 0.3 and the next says it is not 0.3. You do not need to know why yet. You need to know not to trust == on a calculated decimal.
Copy your moving circle from 1-07-move to 1-08-readout and add the readout:
local x = 100local speed = 200local font = love.graphics.newFont(20)
function love.update(dt) x = x + speed * dtend
function love.draw() love.graphics.setFont(font) love.graphics.circle("fill", x, 150, 30)
love.graphics.print("x is " .. x, 40, 300) love.graphics.print("x > 400 " .. tostring(x > 400), 40, 340) love.graphics.print("x < 400 " .. tostring(x < 400), 40, 380) love.graphics.print("x == 400 " .. tostring(x == 400), 40, 420) love.graphics.print("x ~= 400 " .. tostring(x ~= 400), 40, 460)end
Run it and watch the words change as the circle crosses. Write down what happens to each of the four lines, and at what point.
love.graphics.newFont(20) just makes the text big enough to read. It goes at the top with the other variables, because building a font sixty times a second is wasteful even though it would work.Replace the four comparisons with four of your own. At least one has to use >= or <=, and at least one has to compare x against another variable rather than a fixed number.
Try x > 800 - radius — the question "has it reached the right edge?" Next session does something about the answer.
Take the tostring off one of the readout lines:
love.graphics.print(x > 400, 40, 340)Error: main.lua:14: bad argument #1 to 'print' (string expected, got boolean)Read the message carefully. It says exactly what it wanted and exactly what it got. Write down both, then put tostring back.
Add a second circle with its own x and its own speed, and print tostring(x1 > x2). Start them so the answer changes partway through, and work out in advance how many seconds it will take.
Comparisons on the exam produce the same two values, and four of the six operators are written exactly as Lua writes them.
local past = x > 400local exact = x == 400local different = x ~= 400past ← x > 400exact ← x = 400different ← x ≠ 400| Question | Lua | Exam notation |
|---|---|---|
| Greater than | a > b | a > b |
| Less than | a < b | a < b |
| Greater or equal | a >= b | a ≥ b |
| Less or equal | a <= b | a ≤ b |
| Is equal to | a == b | a = b |
| Is not equal to | a ~= b | a ≠ b |
=, which in Lua means assignment. And it uses a real ≠ sign where Lua uses ~=.The exam gets away with a single = because assignment is always the arrow. Nothing is ambiguous: a line with ← stores, a line with = asks.
Reading is the skill being tested here. You will never type ≠ in this course — you will read it on a screen in May and have to know it is not something new.
1-08-readout shows four comparisons changing as the circle movesAnswer the following questions before submitting your work.
x = 400 and x == 400 differ by one character and do completely different things. Explain both, and say what would happen to your moving circle if you used the wrong one in the readout.0.3 and then reported that 0.1 + 0.2 is not 0.3. You do not know why yet. Describe the rule you are going to follow because of it.Submit the required files to the appropriate dropbox.