Currently, I am in the process of developing the PONG Game and I encountered an issue with making the computer play against the player. My goal is to have the computer follow the y-axis position of the ball, ensuring that both entities have distinct movement speeds. However, while the ball is moving correctly, I am facing difficulties in getting the computer to move at all within the game. The code snippet provided below is within the requestAnimationFrame() function.
If you would like to see an example of PONG, you can check out this link:
delta_t = 0.02;
t = t + delta_t;
// Computer should go up
if (computer.position.y < ball.position.y ) {
y_computer = computer.position.y;
vy_computer = vy_computer;
t = 0;
}
// Computer should go down
if (computer.position.y > ball.position.y ) {
y_computer = computer.position.y;
vy_computer = -vy_computer;
t = 0;
}
computer.position.set(-370, y_computer + vy_computer * t , 40);
You can access the site I am referring to for my project here: