'F' → Fullscreen
This activity draws, so it needs a window. Copy template-game, rename the copy to 1-18-tables, and work in its main.lua.
Type this and run it. You should get the picture at the top of the page.
local xs = {150, 400, 650}
function love.draw() love.graphics.setBackgroundColor(0.11, 0.11, 0.15) love.graphics.setColor(0.28, 0.72, 0.95) love.graphics.circle("fill", xs[1], 300, 40) love.graphics.circle("fill", xs[2], 300, 40) love.graphics.circle("fill", xs[3], 300, 40)endRead the three circle lines out loud before you run it. Each one asks xs for a different position, and the only thing that changes between them is the number in the brackets.
Add one line, above love.draw:
local xs = {150, 400, 650}
xs[2] = 500
function love.draw() love.graphics.setBackgroundColor(0.11, 0.11, 0.15) love.graphics.setColor(0.28, 0.72, 0.95) love.graphics.circle("fill", xs[1], 300, 40) love.graphics.circle("fill", xs[2], 300, 40) love.graphics.circle("fill", xs[3], 300, 40)endRun it. The middle circle moves and the other two do not. You did not touch the lines that draw them, and you did not touch the other two values — you changed one slot in the table, and the circle that reads that slot moved with it.
Predict first, on your sheet. Then add these two lines to the top of your file and run it.
local xs = {150, 400, 650}
print("xs[1] is " .. tostring(xs[1]))print("xs[0] is " .. tostring(xs[0]))xs[1] is 150xs[0] is nilnil is Lua's word for nothing is here. There is no element at index 0, because the first one is at index 1.
nil and carried straight on. Hold that thought — it is the whole subject of the next activity, and it is more dangerous than it looks.Add a fourth position to the table and a fourth circle to the screen. Count how many separate places in the file you had to edit to do it.
Write that number on your sheet. Two activities from now you will do the same thing by editing exactly one place.
Read this panel twice, because it is good news and you will not get much of it.
local xs = {150, 400, 650}local first = xs[1]xs[2] = 500print(xs[1])xs ← [150, 400, 650]first ← xs[1]xs[2] ← 500DISPLAY(xs[1])xs[1] is the first element on the left and the first element on the right. Not the second. Not off by one. The same.
| Lua | Exam notation | |
|---|---|---|
First element of xs | xs[1] | xs[1] |
| Change the second element | xs[2] = 500 | xs[2] ← 500 |
←, not =.DISPLAY(xs[1]), and Lua prints it with print(xs[1]).None of those change an answer. They are spelling. The index is the part that changes answers, and the index agrees.
xs[0] printed and its result written on your sheetAnswer the following questions before submitting your work.
xs[0] and got nil instead of an error message. What does that tell you about where the list actually begins?xs[0] to be the first element. Describe a question on the exam they could get wrong because of that habit.Submit the required files to the appropriate dropbox.