previousnext

We are now ready to control the paddle by the mouse.

To do this, we need to do the following:

  1. Check to see if the mouse has moved; we do this by binding a function to a mouse event
  2. Check to see if the mouse is within the canvas boundaries.
  3. If it is, move the x coordinate of the paddle with that of the mouse
  4. Make sure that the paddle stays entirely within the canvas boundaries.
  5. For cosmetic reasons, hide the cursor when the mouse can move the paddle.

And here is the code to do it:

Question for you

Why do we only check to see if the paddle stays within bounds on the right hand side of the canvas?

Experiment!

Comment out some lines of code to better see their effect. For instance, you can comment out the lines of code that ensure that the paddle stay within the canvas boundaries, or those that hide and show the cursor.

previousnext