'F' → Fullscreen
Copy template-console to 1-17-order:
for row = 1, 3 do for col = 1, 4 do print(row .. ", " .. col) endend1, 11, 21, 31, 42, 12, 22, 32, 43, 13, 23, 33, 4Before you run it, write down how many lines you expect. Then run it and count.
Now swap the two loop headers so col is on the outside:
for col = 1, cols do for row = 1, rows doSame twelve lines, different order. Write down what changed and what did not.
Copy template-game to 1-17-grid:
local cols = 10local rows = 8local size = 60
function love.draw() for row = 1, rows do for col = 1, cols do love.graphics.rectangle("fill", 100 + (col - 1) * size, 60 + (row - 1) * size, size - 6, size - 6) end endendRun it. Eighty squares from one rectangle line.
col * size instead of (col - 1) * size. The whole grid shifts one square right and the last column falls off the edge. Put it back, and remember the subtraction — you will need it for the rest of the year.Then change rows and cols and watch the grid resize. Work out the largest grid that still fits before you try it.
Add one decision inside the inner loop:
for row = 1, rows do for col = 1, cols do if (row + col) % 2 == 0 then love.graphics.setColor(0.3, 0.7, 1) else love.graphics.setColor(0.15, 0.15, 0.2) end
love.graphics.rectangle("fill", 100 + (col - 1) * size, 60 + (row - 1) * size, size - 6, size - 6) endend
Work out why row + col and not just col. Try it with only col and write down what you get instead.
Fill this in from your own program, without running anything:
| rows | cols | Rectangles drawn | Per second, at 60 frames |
|---|---|---|---|
| 8 | 10 | ||
| 20 | 20 | ||
| 100 | 100 |
The last row is why nested loops are the first thing anyone checks when a program is slow.
Pick at least two:
Back in the terminal, print a full times table with io.write so each row stays on one line, and print() with nothing in it to end the row.
io.write is the one from session 4 that does not add a new line. That is exactly why it is the right tool here.
Make the inner loop's end depend on the outer counter: for col = 1, row do.
The grid becomes a triangle. Work out how many squares it draws for 8 rows, and say why it is not 64.
1-17-order printed twelve lines, and you recorded what swapping the loops changed1-17-grid draws a checkerboard from one rectangle linerows or cols resizes the grid correctly, with no square off the edgeAnswer the following questions before submitting your work.
col - 1 rather than col. Explain what goes wrong without the subtraction, and say what you would change instead if you wanted the grid to start 100 pixels from the left.Submit the required files to the appropriate dropbox.