Reputation: 21
I created a snake game but i want every new block added onto it to genarate a random new color. This is what I have right now but all it does is send a "null" back and causes it to be white.
// 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);
}
Upvotes: 2
Views: 547
Reputation: 11
the randomColor
function work find, If something still wrong, let me know about your Snake
(and segments
of this
)
function randomColor() {
return '#' + ('00000' + (Math.random() * 16777216 << 0).toString(16)).substr(-6);
}
Upvotes: 1
Reputation: 18835
I found a solution which seems to work quite nicely from 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
}
Upvotes: 1