Javascript 为什么AI';当他们互相追逐/奔跑时,他们会振动 //让喵喵=喵喵 let cell=[] 设Xgrid=200; 让Ygrid=150; 让cpu=[]; 设CPUteam=2; 设AIdist=[]; 让Cpudist=[]; 让Cpudist2=[]; 让迷失=虚假; 函数设置(){ 光滑的(); 帧率(999) createCanvas(600450); } 函数绘图(){ Newcelltimer++; 设cellDist=[]; 让cpuDist=[]; 如果(丢失){ 背景(0) }否则{ 背景(220); 翻译(200150) 刻度(0.9) if(Newcelltimer==1){ push(ceil(random(-Xgrid,2*Xgrid)),ceil(random(-Ygrid,2*Ygrid)),20,CPUteam) OffSetX.push(ceil(随机(0,1000000))) OffSetY.push(ceil(随机(1000000000000))) cell.push(cell(随机(0-Xgrid,2*Xgrid)), ceil(随机(0-Ygrid,2*Ygrid))) CPUteam++; } if(Newcelltimer==0){ push(ceil(random(-Xgrid,2*Xgrid)),ceil(random(-Ygrid,2*Ygrid)),20,CPUteam) OffSetX.push(ceil(随机(0,1000000))) OffSetY.push(ceil(随机(1000000000000))) cell.push(cell(随机(0-Xgrid,2*Xgrid)), ceil(随机(0-Ygrid,2*Ygrid))) CPUteam++; } 对于(设j=0;j200){ 迷失=真实; } 让cpuDist=[]; //约束画布中的AI -------------------------------------------- 如果(cpu[j*4]>(2*Xgrid)-(cpu[j*4+2]/2)){ cpu[j*4]=(2*Xgrid)-(cpu[j*4+2]/2) } if(cpu[j*4](2*Ygrid)-(cpu[j*4+2]/2)){ cpu[j*4+1]=(2*Ygrid)-(cpu[j*4+2]/2); } 如果(cpu[j*4+1]=距离){ 如果(cpu[m*4+2]>cpu[j*4+2]){ cpu[m*4+2]+=cpu[j*4+2] cpu.拼接(j*4,4) }else if(cpu[j*4+2]>cpu[m*4+2]){ cpu[j*4+2]+=cpu[m*4+2] cpu.拼接(m*4,4) }//否则 }//如果 否则{ cpuDist.push(远程); }//否则 }//如果 }//如果 }//为了 //查找最接近的CPU索引值 ------------------------------------------------- 让ClosestCpu=round(min(cpuDist)); 让我们闭上眼睛; 对于(var q=0;q

Javascript 为什么AI';当他们互相追逐/奔跑时,他们会振动 //让喵喵=喵喵 let cell=[] 设Xgrid=200; 让Ygrid=150; 让cpu=[]; 设CPUteam=2; 设AIdist=[]; 让Cpudist=[]; 让Cpudist2=[]; 让迷失=虚假; 函数设置(){ 光滑的(); 帧率(999) createCanvas(600450); } 函数绘图(){ Newcelltimer++; 设cellDist=[]; 让cpuDist=[]; 如果(丢失){ 背景(0) }否则{ 背景(220); 翻译(200150) 刻度(0.9) if(Newcelltimer==1){ push(ceil(random(-Xgrid,2*Xgrid)),ceil(random(-Ygrid,2*Ygrid)),20,CPUteam) OffSetX.push(ceil(随机(0,1000000))) OffSetY.push(ceil(随机(1000000000000))) cell.push(cell(随机(0-Xgrid,2*Xgrid)), ceil(随机(0-Ygrid,2*Ygrid))) CPUteam++; } if(Newcelltimer==0){ push(ceil(random(-Xgrid,2*Xgrid)),ceil(random(-Ygrid,2*Ygrid)),20,CPUteam) OffSetX.push(ceil(随机(0,1000000))) OffSetY.push(ceil(随机(1000000000000))) cell.push(cell(随机(0-Xgrid,2*Xgrid)), ceil(随机(0-Ygrid,2*Ygrid))) CPUteam++; } 对于(设j=0;j200){ 迷失=真实; } 让cpuDist=[]; //约束画布中的AI -------------------------------------------- 如果(cpu[j*4]>(2*Xgrid)-(cpu[j*4+2]/2)){ cpu[j*4]=(2*Xgrid)-(cpu[j*4+2]/2) } if(cpu[j*4](2*Ygrid)-(cpu[j*4+2]/2)){ cpu[j*4+1]=(2*Ygrid)-(cpu[j*4+2]/2); } 如果(cpu[j*4+1]=距离){ 如果(cpu[m*4+2]>cpu[j*4+2]){ cpu[m*4+2]+=cpu[j*4+2] cpu.拼接(j*4,4) }else if(cpu[j*4+2]>cpu[m*4+2]){ cpu[j*4+2]+=cpu[m*4+2] cpu.拼接(m*4,4) }//否则 }//如果 否则{ cpuDist.push(远程); }//否则 }//如果 }//如果 }//为了 //查找最接近的CPU索引值 ------------------------------------------------- 让ClosestCpu=round(min(cpuDist)); 让我们闭上眼睛; 对于(var q=0;q,javascript,p5.js,Javascript,P5.js,您应该重新使用对象。两个CPU的示例数组如下所示: //let meow=meowmeow let cell = [] let Xgrid = 200; let Ygrid = 150; let cpu = []; let CPUteam = 2; let AIdist = []; let Cpudist = []; let Cpudist2 = []; let lost = false;

您应该重新使用对象。两个CPU的示例数组如下所示:

    //let meow=meowmeow
     let cell = []    
     let Xgrid = 200;
     let Ygrid = 150;
     let cpu = [];
     let CPUteam = 2;
     let AIdist = [];
     let Cpudist = [];
     let Cpudist2 = [];
     let lost = false;
     function setup() {
       smooth();
       frameRate(999)
       createCanvas(600, 450);
     }
    
     function draw() {
       Newcelltimer++;
       let cellDist = [];
       let cpuDist = [];
       if (lost) {
         background(0)
       } else {
         background(220);
    
         translate(200,150)
         scale(0.9)
         if (Newcelltimer == 1 ) {
           cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
           OffSetX.push(ceil(random(0, 1000000)))
           OffSetY.push(ceil(random(1000000, 2000000)))
    
           cell.push(ceil(random(0 - Xgrid, 2 * Xgrid)),
             ceil(random(0 - Ygrid, 2 * Ygrid)))
           CPUteam++;
         }
           if (Newcelltimer == 0) {
           cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
           OffSetX.push(ceil(random(0, 1000000)))
           OffSetY.push(ceil(random(1000000, 2000000)))
    
           cell.push(ceil(random(0 - Xgrid, 2 * Xgrid)),
             ceil(random(0 - Ygrid, 2 * Ygrid)))
           CPUteam++;
         }   
         
        
        for (let j = 0; j < (cpu.length / 4); j++) {
    
           if (cpu[j*4+2]>200) {
             lost = true;
           }
           
           let cpuDist = [];
    //Constrains the AI in the Canvas
--------------------------------------------
           if (cpu[j * 4] > (2 * Xgrid) - (cpu[j * 4 + 2] / 2)) {
             cpu[j * 4] = (2 * Xgrid) - (cpu[j * 4 + 2] / 2)
           }
    
           if (cpu[j * 4] < (-Xgrid) + (cpu[j * 4 + 2] / 2)) {
             cpu[j * 4] = (-Xgrid) + (cpu[j * 4 + 2] / 2);
           }
    
           if (cpu[j * 4 + 1] > (2 * Ygrid) - (cpu[j * 4 + 2] / 2)) {
             cpu[j * 4 + 1] = (2 * Ygrid) - (cpu[j * 4 + 2] / 2);
           }
    
           if (cpu[j * 4 + 1] < (-Ygrid) + (cpu[j * 4 + 2] / 2)) {
             cpu[j * 4 + 1] = (-Ygrid) + (cpu[j * 4 + 2] / 2)
           }
---------------------------------------------------------------------
           for (let m = 0; m < cpu.length / 4; m++) {
             if (m != j) {
               if (cpu[j * 4 + 3] % 2 != cpu[m * 4 + 3] % 2) {
                 distant = dist(cpu[m * 4], cpu[m * 4 + 1], cpu[j * 4], cpu[j * 4 + 1])
                 let higher = (cpu[m * 4 + 2] > cpu[j * 4 + 2]) ? cpu[m * 4 + 2] : cpu[j * 4 + 2];
    
                 if (higher/2 >= distant) {
                   if (cpu[m * 4 + 2] > cpu[j * 4 + 2]) {
                     cpu[m * 4 + 2] += cpu[j * 4 + 2]
                     cpu.splice(j * 4, 4)
                   } else if(cpu[j*4+2]>cpu[m*4+2]) {
                     cpu[j * 4 + 2] += cpu[m * 4 + 2]
                     cpu.splice(m * 4, 4)
                   } //Else
                 } //If
                 else {
                   cpuDist.push(distant);
                 } //Else
               } //If
             } //If
           } //For
           //Finds the Closest CPU's index value
    -------------------------------------------------
           let ClosestCpu = round(min(cpuDist));
           let ClosestCpupos;
    
           for (var q = 0; q < cpu.length / 4; q++) {
             if (ClosestCpu == round(int(dist(cpu[q * 4], cpu[q * 4 + 1], cpu[j * 4], cpu[j * 4 + 1])))) {
               ClosestCpupos = q;
               break;
             }
           }
   ---------------------------------------------------

    //Checks if AI ate a cell
    --------------------------------------------------
           for (let n = 0; n < cell.length / 2; n++) {
             let dis = int(dist(cell[n * 2], cell[n * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]))
             if (dis <= cpu[j * 4 + 2] / 2) {
               cpu[j * 4 + 2] += 5;
               cell.splice(n * 2, 2)
             } else {
    //If cell is NOT eaten, pushes the distance between AI and cell into an array
               cellDist.push(dis)
             }
           }
        --------------------------------------------------

    //Find the closest Cell's index value
    -------------------------------------
           let ClosestCell = min(cellDist);
           let ClosestCellpos;
           for (let r = 0; r < cell.length / 2; r++) {
             if (ClosestCell == int(dist(cell[r * 2], cell[r * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]))) {
               ClosestCellpos = r;
               break;
             }
           }
        --------------------------------------
           
Checks if the closest AI is close enough to either chase or run away from it.
    -------------------------------------

      if (Cpudist[j]) {
      Cpudist[j] = dist(cpu[ClosestCpupos * 4], cpu[ClosestCpupos * 4 + 1], cpu[j*4], cpu[j*4+1]) < 250 &&  cpu[ClosestCpupos*4+2] < cpu[j * 4 + 2];
    } else {
      Cpudist[j] = dist(cpu[j * 4], cpu[j * 4 + 1], cpu[ClosestCpupos*4],cpu[ClosestCpupos*4+1]) < 200 && cpu[ClosestCpupos*4+2] < cpu[j * 4 + 2];
    }     
             if (Cpudist2[j]) {
      Cpudist2[j] = dist(cpu[ClosestCpupos * 4], cpu[ClosestCpupos * 4 + 1], cpu[j*4], cpu[j*4+1]) < 250 &&  cpu[ClosestCpupos*4+2] > cpu[j * 4 + 2];
    } else {
      Cpudist2[j] = dist(cpu[j * 4], cpu[j * 4 + 1], cpu[ClosestCpupos*4],cpu[ClosestCpupos*4+1]) < 200 && cpu[ClosestCpupos*4+2] > cpu[j * 4 + 2];
    }  
    -------------------------------------

  let amp = round((cpu[j * 4 + 2] / Math.pow(cpu[j * 4 + 2], 1.44)) * 20000) / 2000;                        
  

//Moves the AI in the following way: If the closest AI is less than an arbitrary 250 then you run, if not, check if the closest cell is less than another arbitrary 250, then you chase the cell. Otherways, you use noise values to move (I deleted that because it was quite repetitive) 
     if (Cpudist2[j]) {
       //Run        
       
          let distXpos = cpu[ClosestCpupos * 4] - cpu[j * 4];
          let distYpos = cpu[ClosestCpupos * 4 + 1] - cpu[j * 4 + 1];
    
             let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
             
             let MultVal = 250/abs(higherVal);
             
             distXpos*=MultVal;
             distYpos*=MultVal;
             
             distXpos = map(distXpos, -250, 250, -1, 1);
             distYpos = map(distYpos, -250, 250, -1, 1);
             cpu[j * 4] -= distXpos * amp;
             cpu[j * 4 + 1] -= distYpos * amp;
           } else if (dist(cpu[ClosestCpupos*4],cpu[ClosestCpupos*4+1],cpu[j*4],cpu[j*4+1])<250&&cpu[ClosestCpupos*4+2]<cpu[j*4+2]) {
             //Chase
             let distXpos = cpu[ClosestCpupos * 4] - cpu[j * 4];
             let distYpos = cpu[ClosestCpupos * 4 + 1] - cpu[j * 4 + 1];
    
             let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
             
             let MultVal = 250/abs(higherVal);
             
             distXpos*=MultVal;
             distYpos*=MultVal;
             
             distXpos = map(distXpos, -250, 250, -1, 1);
             distYpos = map(distYpos, -250, 250, -1, 1);
             cpu[j * 4] += distXpos * amp;
             cpu[j * 4 + 1] += distYpos * amp;
           } else if (dist(cell[ClosestCellpos * 2], cell[ClosestCellpos * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]) < 250) {
             
             let distXpos = cell[ClosestCellpos * 2] - cpu[j * 4];
             let distYpos = cell[ClosestCellpos * 2 + 1] - cpu[j * 4 + 1];
    
             let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
             
             let MultVal = 250/abs(higherVal);
             
             distXpos*=MultVal;
             distYpos*=MultVal;
    
             distXpos = map(distXpos, -250, 250, -1, 1);
             distYpos = map(distYpos, -250, 250, -1, 1);
             cpu[j * 4] += distXpos * amp;
             cpu[j * 4 + 1] += distYpos * amp;
           }
           if (cpu[j * 4 + 3] % 2 == 0) {
             fill(0, 0, 255)
             circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2]);
           } else {
             fill(255, 0, 0)
             circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2])
           }
         }
       }
Displays the cells on the canvas
       for (let k = 0; k < cell.length / 2; k++) {
    
         fill(0, 255, 255, 100)
         circle(cell[k * 2], cell[k * 2 + 1], 7)
       }
     }

cpu = [
  {
    x: 100,
    y: 150,
    size: 25,
    team: 3
  },
  {
    x: 250,
    y: 600,
    size: 15,
    team: 4
  }
]
然后您可以简单地按个循环通过
cpu
,并访问每个cpu的属性,如下所示:

    //let meow=meowmeow
     let cell = []    
     let Xgrid = 200;
     let Ygrid = 150;
     let cpu = [];
     let CPUteam = 2;
     let AIdist = [];
     let Cpudist = [];
     let Cpudist2 = [];
     let lost = false;
     function setup() {
       smooth();
       frameRate(999)
       createCanvas(600, 450);
     }
    
     function draw() {
       Newcelltimer++;
       let cellDist = [];
       let cpuDist = [];
       if (lost) {
         background(0)
       } else {
         background(220);
    
         translate(200,150)
         scale(0.9)
         if (Newcelltimer == 1 ) {
           cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
           OffSetX.push(ceil(random(0, 1000000)))
           OffSetY.push(ceil(random(1000000, 2000000)))
    
           cell.push(ceil(random(0 - Xgrid, 2 * Xgrid)),
             ceil(random(0 - Ygrid, 2 * Ygrid)))
           CPUteam++;
         }
           if (Newcelltimer == 0) {
           cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
           OffSetX.push(ceil(random(0, 1000000)))
           OffSetY.push(ceil(random(1000000, 2000000)))
    
           cell.push(ceil(random(0 - Xgrid, 2 * Xgrid)),
             ceil(random(0 - Ygrid, 2 * Ygrid)))
           CPUteam++;
         }   
         
        
        for (let j = 0; j < (cpu.length / 4); j++) {
    
           if (cpu[j*4+2]>200) {
             lost = true;
           }
           
           let cpuDist = [];
    //Constrains the AI in the Canvas
--------------------------------------------
           if (cpu[j * 4] > (2 * Xgrid) - (cpu[j * 4 + 2] / 2)) {
             cpu[j * 4] = (2 * Xgrid) - (cpu[j * 4 + 2] / 2)
           }
    
           if (cpu[j * 4] < (-Xgrid) + (cpu[j * 4 + 2] / 2)) {
             cpu[j * 4] = (-Xgrid) + (cpu[j * 4 + 2] / 2);
           }
    
           if (cpu[j * 4 + 1] > (2 * Ygrid) - (cpu[j * 4 + 2] / 2)) {
             cpu[j * 4 + 1] = (2 * Ygrid) - (cpu[j * 4 + 2] / 2);
           }
    
           if (cpu[j * 4 + 1] < (-Ygrid) + (cpu[j * 4 + 2] / 2)) {
             cpu[j * 4 + 1] = (-Ygrid) + (cpu[j * 4 + 2] / 2)
           }
---------------------------------------------------------------------
           for (let m = 0; m < cpu.length / 4; m++) {
             if (m != j) {
               if (cpu[j * 4 + 3] % 2 != cpu[m * 4 + 3] % 2) {
                 distant = dist(cpu[m * 4], cpu[m * 4 + 1], cpu[j * 4], cpu[j * 4 + 1])
                 let higher = (cpu[m * 4 + 2] > cpu[j * 4 + 2]) ? cpu[m * 4 + 2] : cpu[j * 4 + 2];
    
                 if (higher/2 >= distant) {
                   if (cpu[m * 4 + 2] > cpu[j * 4 + 2]) {
                     cpu[m * 4 + 2] += cpu[j * 4 + 2]
                     cpu.splice(j * 4, 4)
                   } else if(cpu[j*4+2]>cpu[m*4+2]) {
                     cpu[j * 4 + 2] += cpu[m * 4 + 2]
                     cpu.splice(m * 4, 4)
                   } //Else
                 } //If
                 else {
                   cpuDist.push(distant);
                 } //Else
               } //If
             } //If
           } //For
           //Finds the Closest CPU's index value
    -------------------------------------------------
           let ClosestCpu = round(min(cpuDist));
           let ClosestCpupos;
    
           for (var q = 0; q < cpu.length / 4; q++) {
             if (ClosestCpu == round(int(dist(cpu[q * 4], cpu[q * 4 + 1], cpu[j * 4], cpu[j * 4 + 1])))) {
               ClosestCpupos = q;
               break;
             }
           }
   ---------------------------------------------------

    //Checks if AI ate a cell
    --------------------------------------------------
           for (let n = 0; n < cell.length / 2; n++) {
             let dis = int(dist(cell[n * 2], cell[n * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]))
             if (dis <= cpu[j * 4 + 2] / 2) {
               cpu[j * 4 + 2] += 5;
               cell.splice(n * 2, 2)
             } else {
    //If cell is NOT eaten, pushes the distance between AI and cell into an array
               cellDist.push(dis)
             }
           }
        --------------------------------------------------

    //Find the closest Cell's index value
    -------------------------------------
           let ClosestCell = min(cellDist);
           let ClosestCellpos;
           for (let r = 0; r < cell.length / 2; r++) {
             if (ClosestCell == int(dist(cell[r * 2], cell[r * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]))) {
               ClosestCellpos = r;
               break;
             }
           }
        --------------------------------------
           
Checks if the closest AI is close enough to either chase or run away from it.
    -------------------------------------

      if (Cpudist[j]) {
      Cpudist[j] = dist(cpu[ClosestCpupos * 4], cpu[ClosestCpupos * 4 + 1], cpu[j*4], cpu[j*4+1]) < 250 &&  cpu[ClosestCpupos*4+2] < cpu[j * 4 + 2];
    } else {
      Cpudist[j] = dist(cpu[j * 4], cpu[j * 4 + 1], cpu[ClosestCpupos*4],cpu[ClosestCpupos*4+1]) < 200 && cpu[ClosestCpupos*4+2] < cpu[j * 4 + 2];
    }     
             if (Cpudist2[j]) {
      Cpudist2[j] = dist(cpu[ClosestCpupos * 4], cpu[ClosestCpupos * 4 + 1], cpu[j*4], cpu[j*4+1]) < 250 &&  cpu[ClosestCpupos*4+2] > cpu[j * 4 + 2];
    } else {
      Cpudist2[j] = dist(cpu[j * 4], cpu[j * 4 + 1], cpu[ClosestCpupos*4],cpu[ClosestCpupos*4+1]) < 200 && cpu[ClosestCpupos*4+2] > cpu[j * 4 + 2];
    }  
    -------------------------------------

  let amp = round((cpu[j * 4 + 2] / Math.pow(cpu[j * 4 + 2], 1.44)) * 20000) / 2000;                        
  

//Moves the AI in the following way: If the closest AI is less than an arbitrary 250 then you run, if not, check if the closest cell is less than another arbitrary 250, then you chase the cell. Otherways, you use noise values to move (I deleted that because it was quite repetitive) 
     if (Cpudist2[j]) {
       //Run        
       
          let distXpos = cpu[ClosestCpupos * 4] - cpu[j * 4];
          let distYpos = cpu[ClosestCpupos * 4 + 1] - cpu[j * 4 + 1];
    
             let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
             
             let MultVal = 250/abs(higherVal);
             
             distXpos*=MultVal;
             distYpos*=MultVal;
             
             distXpos = map(distXpos, -250, 250, -1, 1);
             distYpos = map(distYpos, -250, 250, -1, 1);
             cpu[j * 4] -= distXpos * amp;
             cpu[j * 4 + 1] -= distYpos * amp;
           } else if (dist(cpu[ClosestCpupos*4],cpu[ClosestCpupos*4+1],cpu[j*4],cpu[j*4+1])<250&&cpu[ClosestCpupos*4+2]<cpu[j*4+2]) {
             //Chase
             let distXpos = cpu[ClosestCpupos * 4] - cpu[j * 4];
             let distYpos = cpu[ClosestCpupos * 4 + 1] - cpu[j * 4 + 1];
    
             let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
             
             let MultVal = 250/abs(higherVal);
             
             distXpos*=MultVal;
             distYpos*=MultVal;
             
             distXpos = map(distXpos, -250, 250, -1, 1);
             distYpos = map(distYpos, -250, 250, -1, 1);
             cpu[j * 4] += distXpos * amp;
             cpu[j * 4 + 1] += distYpos * amp;
           } else if (dist(cell[ClosestCellpos * 2], cell[ClosestCellpos * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]) < 250) {
             
             let distXpos = cell[ClosestCellpos * 2] - cpu[j * 4];
             let distYpos = cell[ClosestCellpos * 2 + 1] - cpu[j * 4 + 1];
    
             let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
             
             let MultVal = 250/abs(higherVal);
             
             distXpos*=MultVal;
             distYpos*=MultVal;
    
             distXpos = map(distXpos, -250, 250, -1, 1);
             distYpos = map(distYpos, -250, 250, -1, 1);
             cpu[j * 4] += distXpos * amp;
             cpu[j * 4 + 1] += distYpos * amp;
           }
           if (cpu[j * 4 + 3] % 2 == 0) {
             fill(0, 0, 255)
             circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2]);
           } else {
             fill(255, 0, 0)
             circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2])
           }
         }
       }
Displays the cells on the canvas
       for (let k = 0; k < cell.length / 2; k++) {
    
         fill(0, 255, 255, 100)
         circle(cell[k * 2], cell[k * 2 + 1], 7)
       }
     }

cpu = [
  {
    x: 100,
    y: 150,
    size: 25,
    team: 3
  },
  {
    x: 250,
    y: 600,
    size: 15,
    team: 4
  }
]
这不会太难实现,只需使用find和replace即可

对于你的实际问题,我不能像他们在评论中所说的那样运行你的代码。但是,我相信问题在于,你只是在逃离一个最近的CPU。与你之前的问题类似,振动是行为改变太快的结果

为了说明这一点,假设您在一个维度中有3个CPU。将它们的位置设置为0、49和100。让中间的CPU的大小为10,外部的CPU的大小为20。我只想假设较小的CPU每帧移动3个像素,较大的CPU每帧移动1个像素。它们的进展如下:

  • (0,49100)
    • 49向左,51向右,向右移动
  • (1,52,99)
    • 51向左,47向右,向左移动
  • (2、49、98)
    • 47远离左侧,49远离右侧,向右移动
  • (3、52、97)
    • 离左49,离右45,向左移动
  • (4、49、96)
    • 左45,右47,向右移动
  • (5,52,95)
    • 等等,你明白了
  • 看到中间一个是如何振动的了吗?这就是代码中发生的情况。如果要解决此问题,可以执行建议中的一个建议

    如果您想逃离多个CPU,我建议为每个CPU提供一个跟踪它的CPU列表。我不会尝试使用数组格式来解决它,但以下是CPU作为对象的外观:

    for (let ai of cpu) {
      circle(ai.x, ai.y, ai.size)
    }
    
    与当前版本中更改
    Cpudist[j]
    的方式类似,您可以动态更改
    chased_
    列表。它可能看起来像这样(在
    j