'F' → Fullscreen
Copy template-game to 1-21-trees. Write the definition first, then one call:
local function drawTree(x, y) love.graphics.setColor(0.45, 0.3, 0.15) love.graphics.rectangle("fill", x - 14, y, 28, 110)
love.graphics.setColor(0.2, 0.7, 0.35) love.graphics.circle("fill", x, y - 20, 60)end
function love.draw() drawTree(180, 320) drawTree(400, 320) drawTree(620, 320)endRun it with only the first call, and check the tree is where you expect. Then comment out the call and run it again.
Put all three calls back and run it. Then change the trunk color in one place and confirm all three change.
Count what is on your sheet: how many lines describe a tree, and how many trees are on the screen.
A call is a statement, so it can go anywhere a statement goes — including inside a loop:
function love.draw() for i = 1, 5 do drawTree(i * 130, 320) endendFive trees, one call. Then draw the trees at the positions in a table, using the traversal from last session.
Leave one argument out:
drawTree(180)Error: main.lua:3: bad argument #3 to 'rectangle' (number expected, got nil)Lua did not say "you forgot an argument." It filled the blank with nil and complained later, inside the function. Write down which line the error names, and which line the mistake was on.
Now put four arguments in instead of two. Write down what happens — it is not what the last experiment would lead you to expect, and the two together say something about how careful Lua is being on your behalf.
Write a second function that draws something else — a house, a star, a robot, a flower. Requirements:
That last one is the test. If part of your house stays put when you move it, a number in there is not measured from the parameters.
Build a scene from your two functions plus a third, and draw it all from tables of positions. The whole of love.draw should be a few loops and nothing else.
The exam calls this a procedure, and it is one of the two things the Create Task is graded on.
local function drawTree(x, y)
end
drawTree(180, 320)PROCEDURE drawTree(x, y){
}
drawTree(180, 320)| Lua | Exam notation | |
|---|---|---|
| Define it | local function drawTree(x, y) | PROCEDURE drawTree(x, y) |
| Hold the block | end | { … } |
| Call it | drawTree(180, 320) | drawTree(180, 320) |
The call is written identically. Only the definition differs, and only in its first line and its braces.
1-21-trees draws three trees from one definitionAnswer the following questions before submitting your work.
function love.draw() since session 2 without knowing what the first word meant. Explain what it actually means, and say what is different about the functions you wrote today.Submit the required files to the appropriate dropbox.