Cheese gone crazy Lesson 27 Project

My student is trying to make the cheese move slowly at random locations across the screen with no luck. I know I’ve seen this problem before but I can’t remember the fix.

@langilla1,

Right now, the cheese moves to a random location each time the draw loop passes. That is happening very quickly (at least for a human eye).

What if you created a counter variable at the top of the code and each time through the draw loop had the counter increase by 1. Then, you could have the cheese change positions only when the counter hit multiples of 20…

The code would be something like:

if (counter%20==0) {
    cheese1.x = randomNumber(100, 400);
    cheese1.y = 0;
  }
}

This would make the cheese only move once per 20 passes through the draw loop. The % is the modulo operator and the translation of the conditional statement is:

If the remainder of the math problem counter/20 is equal to 20, then do the actions.

Is this what you are after?

Mike

1 Like