# Artificial intelligence for pong

**URL:** https://discourse.processing.org/t/artificial-intelligence-for-pong/41713
**Category:** Coding Questions
**Created:** [April 13, 2023, 8:05pm UTC](https://discourse.processing.org/t/artificial-intelligence-for-pong/41713 "2023-04-13T20:05:08Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hokuto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hokuto/32/2139_2.png) [@hokuto](https://discourse.processing.org/u/hokuto)
#### Post date: [April 13, 2023, 8:05pm UTC](https://discourse.processing.org/t/artificial-intelligence-for-pong/41713/1 "2023-04-13T20:05:08Z")

</div>

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.

```auto
//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();
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 13, 2023, 8:08pm UTC](https://discourse.processing.org/t/artificial-intelligence-for-pong/41713/2 "2023-04-13T20:08:42Z")

</div>

> [@hokuto](#):
>
> to make the racket that controls the cpu have a little intelligence so that it can move on its own and return the ball.

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](https://en.wikipedia.org/wiki/Arkanoid)

see [https://upload.wikimedia.org/wikipedia/en/a/a2/Arkanoid.png](https://upload.wikimedia.org/wikipedia/en/a/a2/Arkanoid.png)

---

<div class="post-metadata">

### Author: ![hokuto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hokuto/32/2139_2.png) [@hokuto](https://discourse.processing.org/u/hokuto)
#### Post date: [April 14, 2023, 12:41pm UTC](https://discourse.processing.org/t/artificial-intelligence-for-pong/41713/3 "2023-04-14T12:41:26Z")

</div>

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.

```auto
//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--------------------------------------

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 14, 2023, 2:03pm UTC](https://discourse.processing.org/t/artificial-intelligence-for-pong/41713/4 "2023-04-14T14:03:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![hokuto](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hokuto/32/2139_2.png) [@hokuto](https://discourse.processing.org/u/hokuto)
#### Post date: [April 14, 2023, 3:40pm UTC](https://discourse.processing.org/t/artificial-intelligence-for-pong/41713/5 "2023-04-14T15:40:57Z")

</div>

I do not understand the question

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 15, 2023, 7:16am UTC](https://discourse.processing.org/t/artificial-intelligence-for-pong/41713/6 "2023-04-15T07:16:57Z")

</div>

Does your AI work now for the racket?
