'F' → Fullscreen
Copy 1-09-bounce to 1-10-zones. Add two faint lines at the top of love.draw so you can see where the thirds are:
love.graphics.setColor(0.25, 0.25, 0.25)love.graphics.line(267, 0, 267, 600)love.graphics.line(533, 0, 533, 600)love.graphics.line takes two points: the x and y it starts at, then the x and y it ends at. These two run from the top of the window to the bottom.
The ball will now be gray, because the color you set for the lines is still set when the circle is drawn. That is session 2's sticky color, and the next task fixes it.
Replace the if / else in love.draw with a chain, and add a label:
function love.draw() love.graphics.setColor(0.25, 0.25, 0.25) love.graphics.line(267, 0, 267, 600) love.graphics.line(533, 0, 533, 600)
local label = ""
if x < 267 then love.graphics.setColor(0.3, 0.5, 1) label = "Left third" elseif x < 533 then love.graphics.setColor(0.9, 0.8, 0.2) label = "Middle third" else love.graphics.setColor(0.2, 0.8, 0.3) label = "Right third" end
love.graphics.circle("fill", x, y, radius)
love.graphics.setFont(font) love.graphics.print(label, 40, 500)endAdd local font = love.graphics.newFont(24) at the top with the other variables. Run it and watch the ball change color and label as it crosses each line.
local label is inside the box on purpose. In session 7 that was a bug, because the value had to survive to the next frame. This one does not — it is built, used, and thrown away within a single picture, so inside is exactly right.Swap the first two conditions so the chain reads:
if x < 533 then label = "Middle third"elseif x < 267 then label = "Left third"else label = "Right third"endRun it and watch the ball cross the left third without ever saying "Left third". Write down what it says there instead, and explain why.
No error, no warning. A branch that can never run looks exactly like a branch that has not run yet.
Put the order back, then add a fourth zone. Split the window into quarters instead of thirds: 200, 400 and 600. Four colors, four labels, three lines drawn.
Write the conditions on your sheet before you type them, and check that every one of the four can actually happen.
Add a second chain that labels how fast the ball is going — slow, medium or fast — based on speedX. Then change speedX in the code and check all three labels can appear.
Careful: a ball moving left has a negative speed. Decide whether −300 counts as fast, and make your conditions say what you decided.
Give the ball a speedY if it does not have one, and add a second label for which third of the window it is in vertically. Two chains, two labels, nine possible combinations.
ELSE IF on the exam reference sheet. The only two selection forms are IF and IF / ELSE. This is the first place Lua has something the exam does not.So a three-way choice is written by putting a whole second decision inside the first one's ELSE:
if score > 100 then print("Gold")elseif score > 50 then print("Silver")else print("Bronze")endIF(score > 100){ DISPLAY("Gold")}ELSE{ IF(score > 50) { DISPLAY("Silver") } ELSE { DISPLAY("Bronze") }}Read the right hand column carefully. Silver is only considered if Gold was false, because the second IF lives inside the first ELSE. That is precisely what elseif means.
| score | Lua chain | Exam nesting |
|---|---|---|
| 120 | Gold | Gold |
| 75 | Silver | Silver |
| 20 | Bronze | Bronze |
The indentation is the whole story. Each nested decision sits one level further in, so a four-way choice on the exam is indented three times. Follow the braces when you read one — they always tell you which ELSE you are in.
You will not be asked to write this by hand often. You will be asked to read it in May and say which branch runs.
1-10-zones shows four zones with four colors and four labelsAnswer the following questions before submitting your work.
if statements and an if / elseif pair look almost identical. Describe a case where they give different results, using your own zone conditions.ELSE IF and writes a three-way choice by nesting instead. Say whether the two are really the same, and give one reason someone might prefer each.Submit the required files to the appropriate dropbox.