codebeginnerg
Member
- Joined
- Oct 13, 2014
- Messages
- 6
- Programming Experience
- Beginner
I have a problem with my code where the ball doesn't accurately detect the collision of the paddle on all point it deflect the ball at a certain area, but not all. Here is the paddle dimensions from paddle class:
x= 550
y= 170
width = 15;
height = 90;
x= 550
y= 170
width = 15;
height = 90;
C#:
class ball
{
int x = 35; // initial x position of the ball
int y = 190; // initial y position of the ball
int dx = 20; // position increment for the ball on the x axis
int dy = 20; // position increment for the ball on the y axis
int xBall = 40;// width of the ball on the x axis
int yBall = 40;// width of the ball on the y axis
court c = new court();
paddle p = new paddle();
public ball()
{
}
public void drawBall(Graphics b)
{
b.FillEllipse(Brushes.Green, x, y, xBall, yBall);
}
public void MoveBall()
{
int newBall_x = x + dx;
int newBall_y = y + dy;
// Bounce the ball if it has collided with a wall
if ((newBall_x < 30) || (newBall_x > 570)) dx = -dx;
if ((newBall_y < 50) || (newBall_y > 320)) dy = -dy;
// Bounce the ball if it has collided with the paddle
if (((newBall_x + xBall > p.paddle_x) && (newBall_x + xBall < (p.paddle_x +
p.paddle_width))) && ((newBall_y + yBall > p.paddle_y) && (newBall_y + yBall < (p.paddle_y +
p.paddle_height))))
{
dx = -dx;
}
x = x + dx;
y = y + dy;
}
}