Treat goes bye-bye

When the treat [and our mouse] is near the cat’s mouth, the treat should disappear. The cat’s mouth is approximately at x: 200, y: 285.

Let’s start by adding a new if statement after the last one:

if(dist(mouseX, mouseY, 200, 285) < 20) {
	// Make the treat disappear
}

We can read this if statement in plain English like so…

image

dist(x1, y1, x2, y2) is a useful function. You can give it two points [x1, y1] and [x2, y2], and it will return the pixel distance between them. Useful for knowing when we’re near the cat’s hungry mouth. Here, we’re giving dist() the positions of our mouse and the cat’s mouth.

All that’s left here is to make the treat disappear. Remember the showTreat boolean we made earlier?

if(dist(mouseX, mouseY, 200, 285) < 20) {
	showTreat = false;
}

💬 Discuss and try

  • How can we make the treat disappear when it’s even further from the cat’s mouth?
  • How can we get another treat without restarting the program?

Next up…

Cat says thank you