mardi 15 mai 2018

JavaScript loop and change variables

I am making an artificial intelligent agent to play google chromes t-rex game and need help creating a loop for the game to continuously play at the moment to begin the game I have to actually press space.

document.getElementById("botStatus").addEventListener("change", function() {

  if (this.checked === true) {
    // Activate bot
    var INTERVAL = 2;
    window.tRexBot = setInterval(function() {
      var tRex = Runner.instance_.tRex;
      var obstacles = Runner.instance_.horizon.obstacles;
      var lengthsize = 0;
      var a = 0.1;
      var b = 5;
      var c = 35;
      var d = 160;
      var posWidth = 20;

      // if (!tRex.jumping && (obstacles.length > 0) && (obstacles[0].xPos + obstacles[0].width) <= ((parseInt(Runner.instance_.currentSpeed - 0.1) - 5) * 34 + 160) && (obstacles[0].xPos + obstacles[0].width) > 20) {
      if (!tRex.jumping && (obstacles.length > lengthsize) && (obstacles[0].xPos + obstacles[0].width) <= ((parseInt(Runner.instance_.currentSpeed - a) - b) * c + d) && (obstacles[0].xPos + obstacles[0].width) > posWidth) {
        // console.log(obstacles[0].xPos + obstacles[0].width + " | " + ((parseInt(Runner.instance_.currentSpeed - 0.1) - 5) * 34 + 160));
        tRex.startJump();
      }
    }, INTERVAL);
  } else {
    // Disable bot
    clearInterval(tRexBot);
  }
});

what I require is for once the Interval ends, I would like it to take the games current speed and distance of obstacles then either increase or decrease the variables I have set into a new interval, so that it will hopefully learn how to get further and further into the game as it picks up speed...

If this sort of makes sense?

Aucun commentaire:

Enregistrer un commentaire