Stanford CS106A Breakout Game Create Moving Paddle Solution

8 Nov

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

	}</pre>
Advertisements

4 Responses to “Stanford CS106A Breakout Game Create Moving Paddle Solution”

  1. Liam Matthews April 28, 2012 at 1:32 pm #

    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…!! 🙂

    • Natasha Murashev April 28, 2012 at 5:26 pm #

      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 🙂

  2. Everlast66 April 28, 2012 at 7:57 pm #

    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.

Trackbacks/Pingbacks

  1. CS106A Assignment 3, part 2: Creating the paddle | PROGRAMMING ZERO TO HERO - April 25, 2012

    […] Natasha The Robot’s solution […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s