首页 > 解决方案 > 每当蛇吃了加速器时,我如何暂时加速我的蛇?

问题描述

每当蛇吃了提升(蓝色)时,我想暂时加速我的蛇。

如有任何反馈和修改,不胜感激

if (ateFood) {
  this.food.active = false;
  this.game.score += 10;
  this.$score.innerText = this.game.score;
  this.soundEffects.score.play();
  this.snake.pop();
} else if (ateBoost) {
  this.boost.active = false;
  this.soundEffects.score.play();
  speed = 50;
  //stuck at here
  this.snake.pop();
} else {
  this.snake.pop();
}

完整的代码可以在这里看到

标签: javascript

解决方案


在你加速蛇的speed = 50工作后,你可以setTimeout(function(){ speed = oldSpeed; }, millisecondsOfBoost);在下一行。因此,如果默认速度为 10 并且您想要提升 2 秒,那么它将是setTimeout(function(){ speed = 10; }, 2000);


推荐阅读