building Air hockey game help me for cpu move

i building air hockey game and it’s going to finish
 

i have only cpu move problem for move on Y

i make cpu move X this way :
 

function cpumove()
    if ball.x > cpuplayer.x then
        cpuplayer.x = cpuplayer.x + speed
    elseif ball.x < cpuplayer.x then
        cpuplayer.x = cpuplayer.x - speed
    end
end

    Runtime:addEventListener(“enterFrame”, cpumove)

what can i do for Y move cpu?

thank u guys

The simple way to do it would be to mimic how pong works, that is to move in the Y direction up when the ball is above the paddle (to a certain threshold so that the cpu isnt playing in the players space) and move the paddle down when the ball is below the paddle. In this way it will be very simple to implement with your current X movements.

To get a move ‘human’ like reaction for your ai, you will need to do a little more work, if you want begin with reading some web pages like:https://gamedev.stackexchange.com/questions/50344/how-would-i-make-an-air-hockey-ai

thank u my friend your help was efficient.

:slight_smile:

The simple way to do it would be to mimic how pong works, that is to move in the Y direction up when the ball is above the paddle (to a certain threshold so that the cpu isnt playing in the players space) and move the paddle down when the ball is below the paddle. In this way it will be very simple to implement with your current X movements.

To get a move ‘human’ like reaction for your ai, you will need to do a little more work, if you want begin with reading some web pages like:https://gamedev.stackexchange.com/questions/50344/how-would-i-make-an-air-hockey-ai

thank u my friend your help was efficient.

:slight_smile: