pta2002
pta2002

Reputation: 185

I get a weird glitch when trying to make an AI for a Pong paddle

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

Answers (1)

Andy Turner
Andy Turner

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

Related Questions