0

Code:

function SnakeGame() {
    "use strict";

    /***** Constant & Global Variables ***********************************/
    var canvas = $("#canvas")[0];
    var ctx  = canvas.getContext('2d');
    var width = $("#canvas").width();
    var height = $("#canvas").height();

    var snake, food;

    function Snake() {
    var startLength = 5; // default for starting size of snake
    this.body = [];

    this.chgStartLength = function(length) {
        startLength = length;
    };

    this.create = function() {
        var i;
        for(i=0; i>5; i++) {
        this.body.push({x:i, y:0});
        }
    };  
    }

    var paintCanvas = function() {
    ctx.fillStyle = 'white';
    ctx.fillRect(0, 0, width, height);
    ctx.strokeStyle = 'black';
    ctx.strokeRect(0, 0, width, height);
    };

    var paintFrame = function() {
    var i, length = snake.body.length;
    for(i=0; i<length; i++) {
        var cell = snake.body[i];
        ctx.fillStyle = 'black';
        ctx.fillRect(cell.x*10, cell.y*10, 10, 10);
        ctx.strokeStyle = 'white';
        ctx.strokeRect(cell.x*10, cell.y*10, 10, 10);
    }
    };

    this.start = function() {
    snake = new Snake();
    snake.create();

    paintCanvas();
    paintFrame();
    console.log(snake.body); //for testing
    };

}

For some reason snake.body is an empty array even after start is executed.

1 Answer 1

6
for(i=0; i>5; i++) {

That > is facing in the wrong direction, I believe.

for(i = 0; i < 5; i++) {
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks... So damn simply. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.