Back

Activity 1.9: Deciding at the Edges

divider

Activity 1.9

Deciding at the Edges

Key Concepts

Running Some Lines and Not Others

Turning Around

Choosing Between Two Things

Where We Left Off

Two sessions ago your circle walked off the screen and kept going.

Last session you could ask whether it had reached the edge.

Where We Left Off

Two sessions ago your circle walked off the screen and kept going.

Last session you could ask whether it had reached the edge.

Today you get to do something about the answer.

What You Are Building

A green circle near the right edge of a black window
A red circle a little left of the right edge of the same window

A ball that bounces, and tells you which way it is going.

The Shape of a Decision

if condition then
end

Everything between then and end runs only when the condition is true.

Another box. You have seen two of these already.

The Condition Is Last Session's Question

if x > 400 then
print("Past halfway.")
end

The Condition Is Last Session's Question

if x > 400 then
print("Past halfway.")
end

x > 400 produced true or false last session and you printed it. Now it decides something.

Or Choose Between Two

if x > 400 then
print("Past halfway.")
else
print("Not yet.")
end

Exactly one of those two runs. Never both, never neither.

Turning Around Is One Line

speedX = -speedX

Turning Around Is One Line

speedX = -speedX

200 becomes -200. The next frame adds a negative number, so the circle goes the other way.

Same line as score = score + 10: work out the right side, put it back in the name.

Which Edge, Exactly?

if x > 800 then

This waits until the center passes 800 — by then the whole circle is outside.

x > 800 - radius turns it around when its edge touches the wall.

Do Not Use == on the Edge

if x == 800 - radius then
speedX = -speedX
end

x moves by about 3.33 each frame, so it jumps over 760 without ever being it.

This is last session's decimal warning with a picture attached. Use >, not ==.

Today's Objectives

  • Write an if that runs only sometimes
  • Write an if / else
  • Reverse a direction by negating a speed
  • Say why the edge test uses > and not ==

Key Terms

Condition
The true-or-false question an if asks.
Branch
A block of code that runs only in some cases.
if / else
Two branches. Exactly one of them runs.
Selection
The exam's word for choosing which lines to run.

'F' → Fullscreen

divider

Build

Task 1: One Wall

Copy 1-07-move to 1-09-bounce and add the first decision:

main.lua
local x = 100
local y = 300
local radius = 40
local speedX = 200
function love.update(dt)
x = x + speedX * dt
if x > 800 - radius then
speedX = -speedX
end
end
function love.draw()
love.graphics.circle("fill", x, y, radius)
end

Run it. The circle crosses, touches the right wall, comes back — and leaves through the left side, because nothing is watching that wall.

Task 2: The Other Wall

main.lua
function love.update(dt)
x = x + speedX * dt
if x > 800 - radius then
speedX = -speedX
end
if x < radius then
speedX = -speedX
end
end

Now it bounces forever. Leave it running while you write the next part — if it ever escapes or gets stuck, you want to see it happen.

Why x < radius and not x < 0? For the same reason the right wall is 800 - radius. The circle is drawn from its center, so its left edge is radius pixels earlier than its center.

Task 3: Say Which Way It Is Going

The bounce is hard to see at the moment it happens. Add an if / else in the drawing box:

main.lua
function love.draw()
if speedX > 0 then
love.graphics.setColor(0.2, 0.8, 0.3)
else
love.graphics.setColor(0.9, 0.2, 0.2)
end
love.graphics.circle("fill", x, y, radius)
end
The ball green, moving right, close to the right wall
The ball red, moving left, having just bounced

Green going right, red going left, and the swap happens exactly at the wall. You now have a decision in each box — one that changes the world, one that changes the picture.

Task 4: Bounce Vertically Too

Add speedY, move y the same way x moves, and give the top and bottom walls their own two decisions. The window is 600 tall.

Four walls, four if statements. Test it by starting the ball in a corner.

Challenge (Optional): Break It on Purpose

Change the right wall test to ask for an exact match:

main.lua
if x == 800 - radius then
speedX = -speedX
end

The ball leaves and never comes back. No error, no crash, and the line looks perfectly reasonable. Explain on your sheet why the condition is never true, using what your readout showed you last session.

Then try to find a speedX that does make it work, and say why relying on that would be a bad idea.

Challenge (Optional): Stuck

Start the ball outside the window — change local x = 100 to local x = 900 and run it.

The ball never appears. Add print(x, speedX) to the update box and watch the terminal for a few seconds.

Write down what the two numbers do. This is a real bug that shows up in real games, and the fix is not another if — it is understanding why two decisions can undo each other.

divider

Same Idea, Exam Notation

The exam calls this selection, and it is one of the three things every program in the course is built from.

Lua
if score > 100 then
print("Win")
else
print("Keep going")
end
Exam Notation
IF(score > 100)
{
DISPLAY("Win")
}
ELSE
{
DISPLAY("Keep going")
}
LuaExam notation
Start the conditionif x > 400 thenIF(x > 400)
Hold the blockthenend{}
The other branchelseELSE

Braces instead of then and end

The exam wraps a block in { and }, each on its own line, and there is no space before the parenthesis in IF(condition). Both of those are worth copying exactly when you write notation by hand.

There are only two selection forms on the exam sheet: IF and IF / ELSE. There is no ELSE IF at all. Lua has one, and what the exam does instead is the next session.
divider

Checkpoint

  • 1-09-bounce bounces off all four walls and never escapes
  • The ball is green going right and red going left
  • Every wall test uses > or <, and accounts for the radius
  • You can say what happened when the test was changed to ==
divider

Reflection

Answer the following questions before submitting your work.

  1. Your program has two decisions in it that look similar and do very different jobs — one in love.update and one in love.draw. Explain what each one decides and why it belongs in the box it is in.
  2. Changing the wall test to == let the ball escape, with no error. Explain why the condition is never true, and say what that tells you about testing a moving value for an exact match.
  3. The right wall is 800 - radius rather than 800. Describe what the program looks like with 800, and say what you would use for a rectangle instead of a circle.
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete