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.

<pre>//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);
		paddle.setFilled(true);
		add (paddle);
		addMouseListeners();
	}

	//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() < getWidth() - PADDLE_WIDTH/2) && (e.getX() > PADDLE_WIDTH/2)) {
			paddle.setLocation(lastX - PADDLE_WIDTH/2, getHeight() - PADDLE_Y_OFFSET - PADDLE_HEIGHT);
		}

	}</pre>

Enjoy the article? Join over 17,500+ Swift developers and enthusiasts who get my weekly updates.

  • Pingback: CS106A Assignment 3, part 2: Creating the paddle | PROGRAMMING ZERO TO HERO()

  • Hey 🙂 Did you manage to get the ball to bounce off the paddle’s short edges? I’m thinking that that would be reversing the x velocity but am not sure if people bother…!! 🙂

    • Hey Liam,

      I was pretty happy to move on from this, so haven’t gone the extra mile. There are answers for it if you google around though 🙂

  • Everlast66

    Hi All,

    I am probably missing something, but what is the purpose of the lastX local var in the mouseMoved method, as it is set equal to the mouse X coord at the beginning?
    Both will have the same value until the mouse is moved, but then the method will get called again and lastX would be set equal to e.getX().

    lastX would be needed if paddle.move() method is used instead, as it requires displacement values and lastX would be set before the mouseMoved call and then at the end of mouseMoved set equal to e.getX() to prep for the next mouseMoved call.

  • Hong

    what do those symbols mean? <????

    • It either means greater then or less than. The code syntax highlighter just has trouble with these symbols.