Reputation: 46
Why arent rectangles being drawn in loop? I just started making snake game n tried to draw body of snake via loop,but for some reason ctx doesnt draw in loop. if i write rect without loop it works.
var canv = document.getElementById('canv');
var ctx = canv.getContext("2d");
var snake = [];
snake[0] = {x:100, 100};
snake[1] = {x:90, 100};
snake[2] = {x:80, 100};
var i;
function draw() {
for (i = 0; i > snake.length; i++) {
ctx.fillStyle = "yellow";
ctx.rect(snake[i].x, snake[i].y, 22, 300);
ctx.fill();
}
setInterval(draw,100);
Upvotes: 1
Views: 44
Reputation: 18968
The for loop is wrong(you had an infinite loop and make it less than i < snake.length
):
function draw() {
for (i = 0; i < snake.length; i++) {
ctx.fillStyle = "yellow";
ctx.rect(snake[i].x, snake[i].y, 22, 300);
ctx.fill();
}
}
Upvotes: 1