Click here to Skip to main content
15,905,015 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
unfortunately the ball movement is very repetitive, Despite of some effort I can not bounce the ball on the bounds, so I need your help:


Java
public void run() {

        boolean bounceBall = false;

        while (true) {

            if (ball_Service) {

                if (leftDisplacement && ball_X > BALL_X_MIN) {

                    bounceBall = (ball_Y >= computerPaddle_Y && ball_Y < (computerPaddle_Y + LENGTH_PADDLES));

                    ball_X = ball_X - SPEED_BALL;
                    ball_Y = ball_Y - verticalDisplacement;
                    table.ballPosition(ball_X, ball_Y);

                    if (ball_X <= PADDLES_COMPUTER_X && bounceBall) {

                        leftDisplacement = false;
                    }
                }

                if (!leftDisplacement && ball_X <= table.ball_x_max) { 

                    bounceBall = (ball_Y >= playerPaddle_Y && ball_Y < (playerPaddle_Y + LENGTH_PADDLES));

                    ball_X = ball_X + SPEED_BALL;

                    table.ballPosition(ball_X, ball_Y);

                    if (ball_X >= table.place_paddles && bounceBall) {

                        leftDisplacement = true;
                    }
                }

                if (computerPaddle_Y < ball_Y && computerPaddle_Y < table.down_Table) {

                    computerPaddle_Y = computerPaddle_Y + SPEED_PADDLES;
                } else if (computerPaddle_Y > TOP_TABLE) {

                    computerPaddle_Y = computerPaddle_Y - SPEED_PADDLES;
                }

                table.computerMovementPaddle(computerPaddle_Y);

                if (ballInPlay()) {

                    if (ball_X > table.ball_x_max) {

                        score_computer++;
                        displayScore();
                    } else if (ball_X < BALL_X_MIN) {

                        score_plyer++;
                        displayScore();
                    }
                }
            }

            try {

                Thread.sleep(table.slow_game);
            } catch (InterruptedException exception) {

                exception.printStackTrace();
            }
        }
    }
Posted
Comments
Sergey Alexandrovich Kryukov 15-Apr-14 16:44pm    
The problem is not really explained. Bouncing simply means inversion of the horizontal or vertical component of the speed. If you also need to take into account energy dissipation, it's a bit harder to do; the most difficult part is the stopping criteria. But I think you did not still approached that level of complexity. The only problem is to correctly evaluate the point of collision with a wall, which is pretty simple. Are you taking into account of gravitation?
—SA
Member 10751678 15-Apr-14 16:58pm    
the game work fine, but the problem is when the ball get out from table horizontally, specially from left area and nothing happen. it suppose to count score for getting ball out. so the ball miss the table horizontally from left side without counting score or returning back
Sergey Alexandrovich Kryukov 15-Apr-14 17:02pm    
Use the debugger. Do you have a kinematic problem, as visualized? Or is it a problem of just scoring?
—SA

1 solution

If there is a problem with the ball moving off and the score not being updated then the error is most likely in your ballInPlay() function - because if that returns false the score will not change.

Alternatively there may be some problem with

C#
if (ball_X > table.ball_x_max) {

                      score_computer++;
                      displayScore();
                  } else if (ball_X < BALL_X_MIN) {

                      score_plyer++;
                      displayScore();


as you seem to have table.ball_x_max and BALL_X_MIN - did you maybe mean table.ball_x_min?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900