Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** this method moves the computer */
- public void move (Ball)
- {
- // calculate the middle of the paddle
- real_y_pos = y_pos + (size_y / 2);
- /* If the ball is moving in opposite direction to the paddle and is no danger for computer's goal move paddle back to the middle y - position*/
- if (ball.getXSpeed() > 0)
- {
- // if the paddle's position is over the middle y - position
- if (real_y_pos < 148)
- {
- y_pos += y_speed;
- }
- // Paddle is under the middle y - position
- else if (real_y_pos > 152)
- {
- y_pos -= y_speed;
- }
- }
- // ball is moving towards paddle
- else if (ball.getXSpeed() < 0)
- {
- // As long as ball's y - position and paddle's y - position are different
- if ( real_y_pos != ball.getYPos())
- {
- // If ball's position smaller than paddle's, move up
- if (ball.getYPos() < real_y_pos)
- {
- y_pos -= y_speed;
- }
- // If ball's position greater than padle's, move down
- else if (ball.getYPos() > real_y_pos)
- {
- y_pos += y_speed;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement