Pong AI

My first AI

Created as a personal side project, Pong IA is my first attempt at developing artificial intelligence.

Summary

Created as a personal side project, Pong IA is my first attempt at developing artificial intelligence. Crafted to enhance my understanding and initiate my learning in designing artificial intelligences, Pong IA is a recreation of the original game from 1972. I built this using Processing, which is Java-based programming software. The project is playable on a computer.

How It Works

For starters, the project is object-oriented. The ball and the players are basic objects instantiated on a black canvas. The red and blue sides serve as visual markers to identify which side we are on. The interesting part of this project is the prediction formula. The entire ‘prediction’ process is calculated using a linear equation from calculus.

  1. I obtain the coordinates of the player, marking the starting point for the prediction.
  2. I retrieve the coordinates of the side the ball is anticipated to hit.
  3. From the hit coordinate, I calculate the projected endpoint, and that’s where the enemy will move.

See the code below for the implementation.

//---------------------------- Variables
let BallX1 = PlayerX; // This variable is Set at is assigned value at every tick
let BallY1 = PlayerY; // This variable is Set at is assigned value at every tick

let BallX2;
let BallY2;

let BallX3 = EnemyX; // This variable is Set at is assigned value at every tick
let BallY3;

let TopCanvasBoundery = 12;
let BottomCanvasBoundery = 588;

let EnemyTurn;

//---------------------------- Function
function bouncePrediction() {
//If IT IS the Enemy turn to play, make the enemy predict the ball arriving point
  if (EnemyTurn) {
    if (balleSpeedY < 0) { // The ball is going upwards
      BallY2 = TopCanvasBoundery; // Top canvas boundery
      BallX2 = BallX1-BallY1+TopCanvasBoundery; // Linear equation to get the side the ball is anticipated to hit
      BallY3 = BallY2+BallX2-EnemyHeight; // The arriving point of the ball of wich the enemy should move to
    } 
	else if (balleSpeedY > 0) { // The ball is going downwards
      BallY2 = BottomCanvasBoundery; // Bottom canvas boundery
      BallX2 = BallX1+BallY1-BottomCanvasBoundery; // Linear equation to get the side the ball is anticipated to hit
      BallY3 = BallY2-BallX2+EnemyHeight; // The arriving point of the ball of wich the enemy should move to
    }
	// Move enemy
    if (BallY3 >= EnemyY) {
      EnemyY += EnemySpeed; // Enemy Go down
    } 
	else if (BallY3 <= EnemyY) {
      EnemyY -= EnemySpeed; // Enemy Go up
    }
  } 
//If it is NOT the Enemy turn to play, make the enemy follow the ball normally
  else if (!EnemyTurn){
	// Move enemy
    if (BallY >= EnemyY) {
      EnemyY += EnemySpeed; // Enemy Go down
    } 
	else if (BallY <= EnemyY) {
      EnemyY -= EnemySpeed; // Enemy Go up
    }
  }
}

Contact me

Play it now

Play on browser

Main involvement

  • Prediction ai

Made by

Émile Paré-Allinger

Made with

Other projects

All projects

Take a look at all the projects I’ve done as part of my classes, contracts, or just out of personal interest.

Portfolio

About