Skip to content

Bumping into Things 💥

Find out when shapes touch: stop at walls, collect coins and score points


Are They Touching? 🤔

if ball is touching wall checks whether two shapes touch right now. Check it again and again in a forever, and the ball rolls until it reaches the wall:

Canvas room having width 400, length 300, fill "lightyellow"
draw circle ball at 50, 150 having radius 20, fill "tomato"
draw rectangle wall at 300, 50 having width 20, length 200, fill "gray"
forever {
  if ball is not touching wall {
    move ball by 2, 0
  }
  wait 0.02 seconds
}

is not touching is the opposite: true while the ball hasn't reached the wall yet.


Each Time They Meet 🪙

when ball touches coin runs each time the two start touching, just like when ... is clicked runs each time you click:

Canvas field having width 400, length 300, fill "lightgreen"
draw circle ball at 30, 150 having radius 20, fill "tomato"
draw circle coin at 200, 150 having radius 12, fill "gold"
score = 0
draw text points at 50, 30 having content "Score: " + score, size 20
when ball touches coin {
  hide coin
  score = score + 1
  points.content = "Score: " + score
}
move ball to 370, 150 in 3 seconds

The ball keeps rolling over the coin, but the score only goes up once. when runs when they start touching, not the whole time they touch.


if or when? 🧭

I want to... Write
do something once, each time they meet when ball touches coin { }
keep doing something the whole time they touch if ball is touching coin { } inside a forever

Lots of Coins 💰

Put the when inside a repeat, and each coin gets its own:

Canvas field having width 400, length 300, fill "lightgreen"
draw circle ball at 20, 150 having radius 15, fill "tomato"
repeat 5 times with n {
  draw circle coin at 60 * n, 150 having radius 8, fill "gold"
  when ball touches coin {
    hide coin
  }
}
move ball to 380, 150 in 4 seconds

Steer into the Coins 🎮

Put it all together with the keys from Playing with Keys. Click the drawing, then drive the ball over the coins. They're somewhere new each time you start again:

Canvas field having width 400, length 300, fill "lightgreen"
draw circle ball at 200, 270 having radius 15, fill "tomato"
score = 0
draw text points at 60, 25 having content "Score: " + score, size 18
repeat 6 times with n {
  draw circle coin at random 20 to 380, random 50 to 230 having radius 8, fill "gold"
  when ball touches coin {
    hide coin
    score = score + 1
    points.content = "Score: " + score
  }
}
forever {
  if key leftarrow is down { move ball by -3, 0 }
  if key rightarrow is down { move ball by 3, 0 }
  if key uparrow is down { move ball by 0, -3 }
  if key downarrow is down { move ball by 0, 3 }
  wait 0.02 seconds
}

Watch Out 👀

  • when ... touches runs once as two shapes start touching; if ... is touching checks whether they touch right now.
  • Every shape is solid, even one with no fill, and its border counts.
  • A hidden shape doesn't touch anything, so a coin you've hidden can't be collected twice.
  • A shape that moves a long way in one step can jump right over a thin wall. Move in small steps, or make the wall thicker.

Challenge Time! 🏆

  • 🌟 Challenge 1: Make the ball turn gold each time it touches the wall.
  • 🌟 Challenge 2: Add a wall to the coin game, and stop the ball going through it.
  • 🌟 Challenge 3: When every coin is collected, show "You win!" 🎉

What's Next? 🚀

Your shapes can move, listen for clicks and keys, and bump into each other. Time to make your own game! 🎮