[HELP REQUEST] Singleplayer pong opponent

I’m new to codea and I’ve tried to make a pong game which the opponent is a bot that makes the paddle follow the ball but the paddle updates to fast that the bias and inaccuracies don’t effect it so you can’t beat is which is no fun

bias = math.random(-20, 20)
inaccuracy = bias + math.random(-100, 100)
paddleB.position.x = ball.position.x + inaccuracy

If anyone can help that would be much appreciated.

@Kyle instead of setting the paddle position of the ball you could move the paddle position towards the x position of the ball.

Something along the lines of

if paddle.position.x<ball.position.x then paddle.position.x=paddle.position.x + 5 end

with a corresponding check if the paddle is to the right of the ball position

Instead of updating the paddle position each frame, you could give it the chance to update each frame using

if math.random(6)==1 then

--Run your update code


in this example, it will have a 1 in 6 chance of updating each loop of the draw function (which is approx 1/60th second). Change the 6 for a larger value to reduce the speed of updating

Thx for the quick reply and the help it helped heaps :))

@Kyle - instead of continually creating random inaccuracies each frame, which tends to average out to zero, I would create an error factor just once, each time the human player hits the ball, and then that error number stays the same until the computer has hit the ball back.

@west I have tired the
if paddle.position.x<ball.position.x then paddle.position.x=paddle.position.x + 5 end
It works well but the paddle doesn’t move left only right I have tried to fix it but I only make it worse so I’ve turned to you for help.


you are only testing on one side. You need to test on the other side as well, ie

if if paddle.position.x<ball.position.x then     
    paddle.position.x=paddle.position.x + 5 
elseif paddle.position.x>ball.position.x then   
    paddle.position.x=paddle.position.x - 5