Artificial intelligence for pong

I’m trying to do the pong, I haven’t added the collision with the rackets yet because I’m trying to understand the collisions.

What I would like to know is how to make the racket that controls the cpu have a little intelligence so that it can move on its own and return the ball.

//player------------------------------------------
class Player{
  private float x = 32.0;
  private float y = 160.0;
  private float speed = 5.0;
  private float p_width = 16.0;
  private float p_height = 125.0;
  
  public void draw(){
    move();
    noFill();
    strokeWeight(2.0);
    stroke(200.0,200.0,200.0);
    rect(x,y,p_width,p_height);
  }
  
  private void move(){
    if(keyPressed && key == CODED && keyCode == UP && y > 8){
      y -= speed;
    }else if(keyPressed && key == CODED && keyCode == DOWN && y < 348){
      y += speed;
    }
  }
} //player----------------------------------------

//cpu---------------------------------------------
class Player_cpu{
  private float x = 588.0;
  private float y = 160.0;
  private float speed = 5.0;
  private float p_width = 16.0;
  private float p_height = 125.0;
  
  public void draw(){
    noFill();
    strokeWeight(2.0);
    stroke(200.0,200.0,0.0);
    rect(x,y,p_width,p_height);
  }
} //player cpu--------------------------------------

//ball-------------------------------------------
class Ball{
  private float x = width/2;
  private float y = height/2;
  private float xspeed = 2.5,yspeed = 2.5;
  private float b_width = 16.0;
  private float b_height = 16.0;
  
  public void draw(){
    move();
    noFill();
    strokeWeight(2.0);
    stroke(0.0,200.0,200.0);
    rect(x,y,b_width,b_height);
  }
  
  private void move(){
    x += xspeed;
    if(x < 0 || x > width-16){
      xspeed *= -1;
    }
    
    y += yspeed;
    if(y < 0 || y > height-16){
      yspeed *= -1;
    }
  }
  
} //ball-----------------------------------------


Player player;
Player_cpu cpu;
Ball ball;

void settings(){
  size(640,480);
  noSmooth();
}

void setup(){
  frameRate(60);
  player = new Player();
  cpu = new Player_cpu();
  ball = new Ball();
}

void draw(){
  background(0,0,0);
  player.draw();
  cpu.draw();
  ball.draw();
}

simplest is to move the racket so that its center is always at the middle of the ball.

So just copy the ball position to paddle position (+half of width of the paddle)

you can also use easing here

Besides of Pong, do you know Arkanoid : Arkanoid - Wikipedia

see https://upload.wikimedia.org/wikipedia/en/a/a2/Arkanoid.png

1 Like

I have done what you have told me and now I have a problem, the cpu racket when the ball moves down follows it and disappears at the bottom.

So I had to create a method to stop the cpu racket but it doesn’t work.

Here you have updated the code of the player_cpu.

//cpu---------------------------------------------
class Player_cpu{
  private float x = 588.0;
  private float y = 160.0;
  private float speed = 5.0;
  private float p_width = 16.0;
  private float p_height = 125.0;
  
  public void draw(){
    ball_stop();
    noFill();
    strokeWeight(2.0);
    stroke(200.0,200.0,0.0);
    rect(x,ball.y+(p_width/2),p_width,p_height);
  }
  
  private void ball_stop(){
    if(y < 8){
      y = 8;
    }else if(y > 348){
      y = 348;
    }
  }
} //player cpu--------------------------------------
1 Like

The ball should turn on the border and so would
the racket?

I do not understand the question

1 Like

Does your AI work now for the racket?