AABotG03

In this example below you will see how to do a AABotG03 with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by seattleacademy, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright seattleacademy ©
  • HTML
  • CSS
  • JavaScript
    <div>wall hits: <span id="wallHits">0</span>    </div>
<canvas id="myCanvas" width="600" height="300"></canvas>

/*Downloaded from https://www.codeseek.co/seattleacademy/aabotg03-JvQYex */
    #myCanvas{
  background:#cccccc;
}


/*Downloaded from https://www.codeseek.co/seattleacademy/aabotg03-JvQYex */
    console.clear();
const c = document.getElementById("myCanvas");
const ctx = c.getContext("2d");
let bots = [];
let wallHits = 0;
function makeBot() {
  const bot = {};
  bot.r = 10 + Math.random() * 5;
  bot.x = (c.width - 2 * bot.r) * Math.random() + bot.r;
  bot.y = (c.height - 2 * bot.r) * Math.random() + bot.r;

  //bot.theta= -Math.PI/4;
  bot.dx = Math.random() * 2 - 1;
  bot.dy = Math.random() * 2 - 1;
  bot.theta = Math.atan2(bot.dy, bot.dx);
  return bot;
}

for (let i = 0; i < 5; i++) {
  bot = makeBot();
  bots.push(bot);
}

function drawBot(bot) {
  ctx.beginPath();
  ctx.arc(bot.x, bot.y, bot.r, 0, 2 * Math.PI);
  ctx.stroke();
  ctx.moveTo(bot.x, bot.y);
  x = bot.x + bot.r * Math.cos(bot.theta);
  y = bot.y - bot.r * Math.sin(bot.theta);
  ctx.lineTo(x, y);
  ctx.stroke();
  if(bot.r == bots[0].r){
    ctx.fillStyle = "rgba(255,0,0,.5)";
    ctx.fill();
  }
  
}

function moveBot(bot) {
  bot.x = bot.x + bot.dx;
  bot.y = bot.y - bot.dy;
    if (bot.y - bot.r < 0) {
      bot.dy = -bot.dy;
      wallHits++;
    }
    if (bot.x + bot.r >= c.width) {
      bot.dx = -bot.dx;
      wallHits++;
    }
    if (bot.y + bot.r >= c.height) {
      bot.dy = -bot.dy;
      wallHits++;
    }
    if (bot.x - bot.r <= 0) {
      bot.dx = -bot.dx;
      wallHits++;
    }
  bot.theta = Math.atan2(bot.dy, bot.dx);

  drawBot(bot);

  wallHitsSpan = document.getElementById("wallHits");
  wallHitsSpan.innerHTML = wallHits;
}
function moveBots() {
  checkCollide();
  ctx.clearRect(0, 0, c.width, c.height);
  for (let i = 0; i < bots.length; i++) {
    moveBot(bots[i]);
  }
}
function checkCollide() {
  for (i = 0; i < bots.length; i++) {
    for (j = 0; j < bots.length; j++) {
      if (i == j) continue;
      d = Math.sqrt(
        (bots[i].x - bots[j].x) ** 2 + (bots[i].y - bots[j].y) ** 2
      );
      if (d <= bots[i].r + bots[j].r) {
        if (bots[i].r < bots[j].r) {
          bots[j].r = Math.sqrt(bots[i].r ** 2 + bots[j].r ** 2);
          bots[i].r = 0;
        }
      }
    }
  }
}
setInterval(moveBots, 100);
function dokeydown(e) {
  var stepSize = 2;
  if (e.key == "ArrowRight") {
   bots[0].dx += stepSize;
  }
  if (e.key == "ArrowLeft") {
    bots[0].dx += -stepSize;
  }

  if (e.key == "ArrowUp") {
    bots[0].dy += -stepSize;
  }
  if (e.key == "ArrowDown") {
    bots[0].dy += stepSize;
  }
  e.stopPropagation();
  //console.log(e);
}
keyListener = document.addEventListener("keydown", dokeydown);


Comments