Stanford CS106A Breakout Game Create Moving Paddle Solution

The second part of the Breakout game in Assignment 3 is creating the paddle and making it move as the mouse moves according to the following instructions:

Here is my solution:

The hardest part of this was getting the paddle to actually move! It took me forever to figure out how to do it, mostly because I didn’t create a paddle object outside of the paddle method. I was actually ready to give up programming, and then finally figure it out! It felt really good to figure this part out… Here is what I came up with, which you can also find on gist.

&lt;pre&gt;//adding individual paddle object
	private GRect paddle;
	private double lastX;

	//paddle set-up
	private void drawPaddle() {
		//starting the paddle in the middle of the screen
		double x = getWidth()/2 - PADDLE_WIDTH/2; 
		//the paddle height stays consistent throughout the game
		//need to make sure to subtract the PADDLE_HEIGHT, 
		//since the rectangle gets drawn from the top left corner
		double y = getHeight() - PADDLE_Y_OFFSET - PADDLE_HEIGHT;
		paddle = new GRect (x, y, PADDLE_WIDTH, PADDLE_HEIGHT);
		add (paddle);

	//making the mouse track the paddle
	public void mouseMoved(MouseEvent e) {
		lastX = e.getX();
		/* The mouse tracks the middle point of the paddle. 
		 * If the middle point of the paddle is between half paddle width of the screen
		 * and half a paddle width before the end of the screen, 
		 * the x location of the paddle is set at where the mouse is minus half a paddle's width, 
		 * and the height remains the same
		if ((e.getX() &amp;lt; getWidth() - PADDLE_WIDTH/2) &amp;amp;&amp;amp; (e.getX() &amp;gt; PADDLE_WIDTH/2)) {
			paddle.setLocation(lastX - PADDLE_WIDTH/2, getHeight() - PADDLE_Y_OFFSET - PADDLE_HEIGHT);

