News for hackers. Stuff that matters.
Re:Show HN: I taught my little brother JS, and he made this ... by wrayjustin on Wednesday January 09, 2019 @11:47PM (#18871598)
I did the same, but played around a bit more and made an "AI" mode:

  maxScore = 0;
  setInterval( function() {
  	if (score > maxScore) {
  		maxScore = score;
  	}
  	if (distance(hero.y, hero.x, ennemi.y, ennemi.x) < (vitesseEnnemi * 15 + 30)) {
  		hero.y += (hero.y - ennemi.y) * speedLimit / distance(hero.y, hero.x, ennemi.y, ennemi.x);
  		hero.x += (hero.x - ennemi.x) * speedLimit / distance(hero.y, hero.x, ennemi.y, ennemi.x);
  	}
  	if (spawn > PowerUpspawnRate) {
  		hero.y += (powerUp.y - hero.y) * speedLimit / distance(hero.y, hero.x, powerUp.y, powerUp.x);
  		hero.x += (powerUp.x - hero.x) * speedLimit/ distance(hero.y, hero.x, powerUp.y, powerUp.x);
  	} else {
  		hero.y += (reward.y - hero.y) * speedLimit / distance(hero.y, hero.x, reward.y, reward.x);
  		hero.x += (reward.x - hero.x) * speedLimit/ distance(hero.y, hero.x, reward.y, reward.x);
  	}
  }, 25);
It's oddly fun to watch the little green guy pull off some sick dekes.

If you want your green guy to flat-out avoid the enemy, just wrap the second set of conditions there in an else block.

edit: Formatting for readability/copy+paste

Re:Show HN: I taught my little brother JS, and he made this ... by krozgen on Wednesday January 09, 2019 @11:51PM (#18871621)
I love when the red square comes close and the green one jukes in a tight spiral around it, just thinking "Whoa nope nope NOPE ah it's okay"
Re:Show HN: I taught my little brother JS, and he made this ... by wrayjustin on Thursday January 10, 2019 @12:03AM (#18871674)
LOL yeah, it's like watching a sport. By about a score of 300 the enemy is just too fast for our hero. =/
Re:Show HN: I taught my little brother JS, and he made this ... by toabi on Thursday January 10, 2019 @01:23AM (#18871938)
Interesting, I just watched it and for me the limit is around ~130.
Re:Show HN: I taught my little brother JS, and he made this ... by toabi on Thursday January 10, 2019 @01:26AM (#18871953)
Lower the timeout from 25 to 5 and it's like Neo fighting agent smith :D