Code viewer for World: Aim training (by Antoine K...

// Cloned by Duarte Martinho on 1 Dec 2022 from World "Aim training (by Antoine Klein)" by Antoine Klein 
// Please leave this clone trail here.
 
// needs mouse drag before anything happens 
//choose settings

const maxBalls = 8; //number of targets at the same time
let chosenTime = 20; //in seconds
//Change the radius at ligne 84 (remove random if you prefer a constant radius (e.g. 50)

//-----------------------------------------------------------------------------------//
let value = 50;


let radius;
let x, y;
//let accuracy;
let score = 0;
let clickCount = 0;
let targets = [];
let timeLimit = 3; 
let countDown;
let SOUND_SHOT;
let starttime;

let img;
function preload() {
  img = loadImage('/uploads/antkln/1669724251.png');
  a1 = loadImage('/uploads/antkln/chiffre1b.jpg');
  a2 = loadImage('/uploads/antkln/chiffre2b.jpg');
  a3 = loadImage('/uploads/antkln/chiffre3b.jpg');
  SOUND_SHOT = loadSound('/uploads/antkln/shotsound.mp3');
  MUSIC_BACK = loadSound('/uploads/antkln/videogame11.mp3');
}

function setup() {    
    //loop();
    createCanvas(1000, 500); //(800, 400)
    x = random(width);
    y = random(height);
    sizeSlider = createSlider(10, 80, 50);// change size of targets but do not work on Ancient Brain
    //sizeSlider.style('width', '80px');
    MUSIC_BACK.play();
}

function draw() {
    //background(0);
    fill(0, 0, 0);
    noStroke();
    let currentTime = int(millis() / 1000); //convert time to second as an integer
    countDown = timeLimit - currentTime;
    image(a3, 0, 0, width, height);
    if (countDown < 2) {
        image(a2, 0, 0, width, height);
        currentTime = int(millis() / 1000);
        countDown = timeLimit - currentTime;
    }
    if (countDown < 1) {
        image(a1, 0, 0, width, height);
        currentTime = int(millis() / 1000);
        countDown = timeLimit - currentTime;
    }
    if (countDown < 0) {
        background(0);
        noFill();
        //fill(0, 0, 0);
        noStroke();
        //radius = sizeSlider.value(); not working
        textSize(25);
        fill(240, 255, 250); // color for "Accuracy"
        let accuracy = score / clickCount;
        var roundedString = accuracy.toFixed(4);
        var rounded = Number(roundedString);
        text('Accuracy: ' + rounded*100 + '%', width-200, 25);
        
        //for (let i = maxBalls - 1; i >= 0; i--) {
        let Lx = [];
        let Ly = [];
        if (targets.length < maxBalls) { //targets must not touch each other otherwise the accuracy is multiplied by 2 when hitting two targets 
            xx = random(width);
            append(Lx, xx);
            yy = random(height);
            append(Ly, yy);
            //while (xx > Lx - radius || xx < Lx + radius || yy > Ly - radius || yy < Ly + radius) {
           //    xx = random(width);
            //    yy = random(height);
           // }
            radius = random(20, 80);
            append(targets, new Target(xx, yy, radius, radius));
        }
        for (let i = targets.length - 1; i >= 0; i--) {
            //targets[i].expand(expansionRate);
            targets[i].show();
      }
        starttime = millis().toFixed(0)/1000;
        end = chosenTime-starttime+4.1; //I calculated 0.1 second to read the code and display the 3 images
        fill(240, 240, 240);
        text(end, width-50, 50);
        if (end <= 0) {
            //musicPause(); 
            clear();
            // const score2 = Object.assign({}, score); // the score should not change once the game is over
            fill(0, 0, 0);
            text("Congratulation!!!\nYou have a score of " + score + " targets in " + chosenTime + "s.\nYour accuracy is: " + rounded*100 + '%\nReload to try again!\nPRESS RIGHT_ARROW TO RELOAD\nPRESS LEFT_ARROW TO QUIT ' , width/3, height/3);
        } 
    }
}


function keyPressed() {
    if (keyCode === RIGHT_ARROW) {
        value = 0;
        window.location.reload();
    } 
    else if (keyCode === LEFT_ARROW) {
        value = 0;
        window.open('', '_self', '');
        window.close();
    }
}


function mousePressed() {
    SOUND_SHOT.play();
    SOUND_SHOT.setVolume(0.2);
    clickCount++;
        for (var i = targets.length - 1; i >= 0; i--) {
            if (targets[i].test() < radius/2) { //and no one has been remove yet
                targets.splice(i, 1);
                score++;
            }
        }
}

class Target {
  constructor(x, y, size) {
    this.x = x;
    this.y = y;
    this.size = size;
    //this.size = radius;
    this.color = color(random(50, 255), random(50, 255), random(50, 255)); //if black, we won't see it
  }
  show() {
    fill(this.color);
    //texture(img); 
    image(img, this.x, this.y, this.size, this.size);
    //ellipse(this.x, this.y, this.size); // if using ellipse, you have to remplace radius by radius/2 in l.123 and remove one radius l.84
  } 
  test() {
    return (dist(this.x + this.size/2, this.y + this.size/2, mouseX, mouseY));
  }
  //expand() {
    //this.size= constrain(this.size, 0, this.size);
  //}
}


//const MUSIC_BACK  = '/uploads/antkln/videogame.mp3';//'uploads/rkj43/gaming-8bit-music-hyperloop-124334.mp3';
//const MUSIC_START  = '/uploads/antkln/countdown.mp3' ;
//const SOUND_SHOT = '/uploads/antkln/shotsound.mp3' ;
//AB.backgroundMusic ( MUSIC_BACK );