Back

Activity 1.23: The Fleet, and Who Calls What

divider

Activity 1.23

The Fleet, and Who Calls What

Key Concepts

A Parameter That Changes Size

Two Kinds of Function

Only One Kind Counts in May

What You Are Building

Four pale blue triangular ships of different sizes with orange portholes, scattered across a black window

Four ships, four sizes, one description.

Yesterday's Ship Was One Size

local function drawShip(x, y)
love.graphics.polygon("fill",
x, y - 40,
x - 28, y + 40,
x + 28, y + 40)
end

Yesterday's Ship Was One Size

local function drawShip(x, y)
love.graphics.polygon("fill",
x, y - 40,
x - 28, y + 40,
x + 28, y + 40)
end

40 and 28 are stuck in there. Every ship in the game is that big, forever.

So Make the Size a Blank Too

local function drawShip(x, y, size)
love.graphics.setColor(0.75, 0.82, 0.92)
love.graphics.polygon("fill",
x, y - size,
x - size * 0.7, y + size,
x + size * 0.7, y + size)
love.graphics.setColor(0.95, 0.55, 0.2)
love.graphics.circle("fill", x, y + size * 0.35, size * 0.28)
end

So Make the Size a Blank Too

local function drawShip(x, y, size)
love.graphics.setColor(0.75, 0.82, 0.92)
love.graphics.polygon("fill",
x, y - size,
x - size * 0.7, y + size,
x + size * 0.7, y + size)
love.graphics.setColor(0.95, 0.55, 0.2)
love.graphics.circle("fill", x, y + size * 0.35, size * 0.28)
end

Every measurement is now a fraction of size, not a number of pixels.

size * 0.7 means "seven tenths as wide as it is tall", whatever tall turns out to be.

Miss One and You Will See It

local function drawShip(x, y, size)
love.graphics.polygon("fill",
x, y - size,
x - size * 0.7, y + size,
x + size * 0.7, y + 40)
end

One 40 left behind, and the ship bends as the size changes.

Same test as session 21's house with the door left behind, and easier to see.

Now the Question This Quarter Has Been Building To

function love.draw()
drawShip(400, 300, 50)
end

Two functions on that slide. Who calls each one?

Two Kinds, and They Are Not Equal

The engine calls these
love.draw
love.update
love.keypressed
love.mousepressed

Two Kinds, and They Are Not Equal

The engine calls these
love.draw
love.update
love.keypressed
love.mousepressed
You call these
drawShip
drawTree
area
isEven

You wrote the bodies of all eight. Only four are yours in the sense that matters in May.

The Rule, in the College Board's Own Words

"Event handlers are built-in abstractions in some languages and will therefore not be considered student-developed."

love.draw does not count as a procedure you wrote.

Why That Matters in Quarter 3

The Create Task requires at least one student-developed procedure with one or more parameters, and an algorithm inside its body.

A program that is nothing but love.draw and love.update does not have one, however much code is in it.

Which Is Not a Reason to Avoid Them

You need love.draw. It is how anything appears at all.

The rule is about what you point at in May, not about how to write a program.

drawShip is what you point at.

Today's Objectives

  • Add a size parameter and scale everything from it
  • Draw a fleet from three parallel tables
  • Sort the functions in your file into two kinds
  • Say which kind the Create Task counts, and why

Key Terms

Callback
A function the engine calls for you, at a moment it decides.
Event handler
A callback that runs in response to something happening. The College Board's word.
Student-developed procedure
A function you wrote and call yourself. The Create Task requires one.
Scaling
Measuring every part as a fraction of one size, so the whole thing grows together.

'F' → Fullscreen

divider

Build

Task 1: One Ship, Any Size

Copy template-game to 1-23-fleet:

main.lua
local function drawShip(x, y, size)
love.graphics.setColor(0.75, 0.82, 0.92)
love.graphics.polygon("fill",
x, y - size,
x - size * 0.7, y + size,
x + size * 0.7, y + size)
love.graphics.setColor(0.95, 0.55, 0.2)
love.graphics.circle("fill", x, y + size * 0.35, size * 0.28)
end

Call it once at size 40, then 15, then 90. The shape must stay the same shape — only bigger.

love.graphics.polygon is new and it is the last drawing call this quarter. After "fill" it takes any number of x and y pairs and joins them up. Three pairs is a triangle.

Task 2: Break the Scaling on Purpose (Required)

Put one hardcoded number back:

main.lua
local function drawShip(x, y, size)
love.graphics.polygon("fill",
x, y - size,
x - size * 0.7, y + size,
x + size * 0.7, y + 40)
end

Draw it at 15, 40 and 90. Describe what happens to the shape at each size, then put the scaling back.

This is session 21's move-it test with one more dimension: it is not enough for the parts to move together, they have to grow together.

Task 3: A Fleet

Three parallel tables, one loop, one call:

main.lua
local xs = {140, 330, 520, 680}
local ys = {180, 330, 200, 420}
local sizes = {30, 70, 45, 25}
function love.draw()
for i, x in ipairs(xs) do
drawShip(x, ys[i], sizes[i])
end
end
Four ships of different sizes at the positions in the tables

Add a fifth ship by adding three numbers. Count what is in your file: how many ships, how many drawShip calls are written out.

Task 4: Sort Your Own Functions

Open the file and list every function in it. For each one, write down who calls it:

FunctionWho calls itCounts as student-developed?
love.draw
love.keypressed
drawShip

You wrote the body of all three. Only one of them is a procedure you developed, as far as the Create Task is concerned.

Task 5: Make the Fleet Do Something

Pick one, and it has to work through drawShip rather than around it:

  • Every ship drifts down the screen and wraps to the top
  • The ships pulse — each size grows and shrinks on its own
  • Clicking a ship makes it bigger
  • A fourth table gives each ship its own color, passed as parameters

If you find yourself writing drawing code outside drawShip, stop and add a parameter instead.

Challenge (Optional): Zero and Negative

Call drawShip(400, 300, 0) and then with a negative size.

Neither one is an error. Write down what you see in each case and what you think happened. Then decide whether a real program should let that happen, and say what you would do about it.

divider

Checkpoint

  • 1-23-fleet draws four ships at four sizes from one function
  • The shape is identical at size 15 and size 90
  • The bent-ship experiment is described on your sheet
  • Your function table is filled in, all three rows
  • You can say which functions in your file the Create Task would count
divider

Reflection

Answer the following questions before submitting your work.

  1. You wrote the body of love.draw and the body of drawShip. Explain why the College Board counts one and not the other, and say whether you think that is fair.
  2. One hardcoded number made the ship bend instead of grow. Explain what "scaling" means in your own words, and describe how you would test any drawing function for it.
  3. Adding a fifth ship took three numbers and no new code. Trace that back through the quarter: name the three separate ideas that had to be in place for that to be true.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete