Reputation: 185
I am making a pong game for MiniLD using Slick2d (and therefore java), but when I try to make the AI it is not gonna work. I get a paddle flickering between two positions. I need help. Here is the code I use.
static int maxSpeed = 3;
(...)
float ypos = ball.getCenterY() - (paddleCPU.getY() + maxSpeed);
paddleCPU.setY(ypos);
Upvotes: 1
Views: 72
Reputation: 140309
Perhaps try actually limiting the rate of change of y to being between -maxSpeed and +maxSpeed:
float dy = ball.getCenterY() - paddleCPU.getY());
dy = Math.max(-maxSpeed, Math.min(maxSpeed, dy));
paddleCPU.setY(paddleCPU.getY() + dy);
Upvotes: 1