leslie
leslie

Reputation: 15

move() method in paddle game

Currently implementing the move() method for the ball class in my paddle game. The paddle game is the one with the paddle on the bottom, that can move left/right. And the ball bounces off of the three walls. I realize that my move method is wrong, wondering if someone can point to me the right way to go about implementing it. I need to account for when the ball bounces off the sides and top

EDIT: I played around with my code but it's still not bouncing off the walls

My code:

    if (this.y+speed<=0+radius)     //Checking for top          
    {
        System.out.println("in y"); //Checking if it's in this condition
        flipYDir();  


    }
    else if (this.x+speed<=0+radius) //Checking for left wall
    {
        System.out.println("in x<"); //Checking if it's in this condition
        flipXDir();


    }
    else if (this.x+speed<courtWidth-radius) //Checking for right wall
    {
        System.out.println("in x>"); //Checking if it's in this condition
        flipXDir();

    }
    else  //Update move
    {
    x+=speed*xDir;
    y+=speed*yDir;
    }

Upvotes: 0

Views: 355

Answers (1)

Chris Bogart
Chris Bogart

Reputation: 472

There are a couple problems here:

  • Since you're adding the same value, speed, to both x and y, your ball is always going to move at exactly a 45 degree angle. Could be that's what you want, though...

  • You're checking for x==0 and x==courtWidth, and reversing the directions at that time, but what if x=17, courtwidth=20, xDir =1, and speed=5? When you increment x, it'll be 22, and it'll never exactly equal courtwidth and just keep incrementing forever. Somehow you have to handle the situation where it doesn't land exactly on the edge of the court.

  • You might not want to flip both X and Y directions at all three walls -- if you do, the ball will bounce straight back the direction it came no matter where it hits

Upvotes: 1

Related Questions