Note that there are some explanatory texts on larger screens.

plurals
  1. POBouncing ball off of paddle in breakout game ~java
    primarykey
    data
    text
    <p>So I am currently writing a code for a breakout game in java. Right now, I have it set up so that it prompts the player with a dialog box that asks the player how many bricks they want per row (a number between 4 and 40). The part that gets messed up is the whole collision with the ball and the paddle. I'm very new to programming so forgive me if it's incredibly easy. Using dr. Java.</p> <pre><code> //------------------------------- animate ------------------------- /** * this should send the ball back after hitting the paddle. * */ public void animate( ) { int dX = ball.getXLocation( ); int dY = ball.getYLocation( ); while( true ) { ball.move( ); if ( ball.boundsIntersects( paddle) ) { dY= -1*dY; ball.setLocation( dX,dY ); } for(int i = 0; i &lt; bricks.size(); i++) { if (ball.boundsIntersects(bricks.get(i))) { dY = -dY; ball.setLocation(dX,dY); bricks.get(i).hide(); bricks.remove(i); System.out.println("brick"); } </code></pre> <p>}</p> <p>here is the move method from my ball class. Once again sorry for the horrendous code.</p> <pre><code>//------------------------------- move ---------------------------- /** * This will move the ball by deltaX and deltaY * and bounce the ball off the edges of the Frame. * */ public void move( ) { int dX = this.getXLocation( ) + deltaX; int dY = this.getYLocation( ) + deltaY; this.setLocation( dX, dY ); if( getYLocation( ) &lt; 0 ) { setLocation( dX, 0 ); deltaY *=-1; } else if( getYLocation( ) &gt; ( 500 + size ) ) { setLocation( dX, 500-size); deltaY *=-1; } if( getXLocation() &lt; 0 ) { setLocation( dX , dY ); deltaX *=-1; } else if( getXLocation( ) &gt; ( 500 + size ) ) { setLocation( 500-size, dY ); deltaX *=-1; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload