【问题标题】:Snake Game random colors贪吃蛇游戏随机颜色
【发布时间】:2016-08-19 14:55:48
【问题描述】:

我创建了一个蛇游戏,但我希望添加到其中的每个新块都生成随机的新颜色。这就是我现在所拥有的,但它所做的只是发送一个“null”并使其变为白色。

// Draw a square for each segment of the snake's body
Snake.prototype.draw = function () {
for (var i = 0; i < this.segments.length; i++) {
this.segments[i].drawSquare(randomColor());
}
};








function randomColor() {
    return '#' + ('00000' + (Math.random() * 16777216 << 0).toString(16)).substr(-6);
}

【问题讨论】:

    标签: javascript random colors


    【解决方案1】:

    我从 Dimitry K - http://www.paulirish.com/2009/random-hex-color-code-snippets/ 找到了一个似乎效果很好的解决方案

    function randomColor() {
        var color = '#' + ("000000" + Math.random().toString(16).slice(2,8).toUpperCase()).slice(-6);
        return color
    }
    

    【讨论】:

      【解决方案2】:

      randomColor 函数工作查找,如果仍然有问题,请告诉我您的Snake (和this 中的segments

      function randomColor() {
          return '#' + ('00000' + (Math.random() * 16777216 << 0).toString(16)).substr(-6);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多