2016-04-26 91 views
2

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

// 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); 
 
}

回答

1

randomColor功能工作发现,如果还有什么不对,让我知道您的Snake (和segmentsthis

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