'F' → Fullscreen
Copy template-console to 1-16-count. Predict the output of each of these before running it, and write both your prediction and the result:
for i = 1, 5 do print(i)end
for i = 3, 7 do print(i)end
for i = 1, 10, 2 do print(i)end
for i = 10, 1, -2 do print(i)endThe third and fourth are the ones people get wrong. Count how many lines each produces before you run it.
Add these two, and work out what they do first:
for i = 1, 10 do print(i .. " times 7 is " .. i * 7)end
for i = 1, 5 do print(i .. " squared is " .. i * i)endA times table in three lines. Change the 7 and you have a different one.
Copy template-game to 1-16-row:
local count = 8local gap = 800 / (count + 1)
function love.draw() for i = 1, count do love.graphics.circle("fill", i * gap, 300, 34) endendRun it, then change count to 3, then to 20. The circles space themselves out, because gap is worked out from count rather than typed in.
count + 1? With 8 circles there are 9 gaps — one before the first and one after the last. Try 800 / count instead and see where the last circle ends up.Use i for the color as well as the position:
for i = 1, count do love.graphics.setColor(i / count, 0.4, 1 - i / count) love.graphics.circle("fill", i * gap, 300, 34)end
Every circle is drawn by the same line of code, and no two look the same. Write down what i / count is on the first pass and on the last.
Change the row into something of your own. Pick at least two:
i for the radiusMove the drawing line outside the loop:
for i = 1, count doend
love.graphics.circle("fill", i * gap, 300, 34)The window turns blue. Copy the message, then explain it using what the slides said about where i exists.
Arrange the circles in a ring instead of a row. You need math.cos and math.sin, and the angle for circle i out of count is i / count * 2 * math.pi.
This is a real use of trigonometry and you are not expected to derive it. Multiply the cosine by a radius, add 400, and see what happens.
The exam has a counting loop, and it does not give you a counter. This is the widest gap between Lua and the exam sheet in the whole quarter.
for i = 1, 8 do print(i)endi ← 1
REPEAT 8 TIMES{ DISPLAY(i) i ← i + 1}REPEAT 8 TIMES runs a block eight times and tells you nothing about which pass you are on. If you need to know, you make your own counter, start it yourself, and add to it yourself — exactly the bookkeeping Lua's for removed.
Lua for | REPEAT n TIMES | |
|---|---|---|
| Runs a fixed number of times | Yes | Yes |
| Gives you a counter | Yes | No |
| Can start somewhere other than 1 | Yes | Only by hand |
| Can step by something other than 1 | Yes | Only by hand |
for loop on the exam sheet at all. Two loops, and that is the whole list: REPEAT n TIMES and REPEAT UNTIL(condition).On a trace question, the counter is usually the thing being asked about — so when you see REPEAT n TIMES, look for the variable somebody had to maintain by hand, and watch where it changes.
1-16-row draws its circles from one circle line inside a loopcount alone changes how many there are and respaces themAnswer the following questions before submitting your work.
while loop needed three separate parts and this session's for puts all three on one line. Explain which kind of bug that makes impossible, and give an example of something while can still do that for cannot.REPEAT n TIMES gives you no counter. Describe what you would have to add to it to draw your row of circles, and say how many extra lines that is.Submit the required files to the appropriate dropbox.