Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 990713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:59:06+00:00 2026-05-16T05:59:06+00:00

i have written a snake program using javascript.. the problem is that the snake

  • 0

i have written a snake program using javascript..
the problem is that the snake does not grow more than 2 blocks size….

<html>
<head>
<script type="text/javascript">

var matrix, body, dir, key, lastx, lasty, start, applex, appley, eat, hal;


function node(x, y) {
    this.x = x;
    this.y = y;
}


function draw() {
    var str;
    for (var i = 0; i < body.length; i++) {
        matrix[body[i].x * 50 + body[i].y].bgColor = "black";
    }

}


function halt() {
    hal = 1 - hal;
    if (hal == 0) automove();
}


function check_status() {
    if (start == 1 && hal == 0) {

        var ch;
        if (eat == 1) {
            do {
                ch = 0;
                applex = Math.round(49 * Math.random());
                appley = Math.round(49 * Math.random());
                for (var i = 0; i < body.length; i++)
                if (body[i].x == applex && body[i].x == appley) ch = 1;
            } while (ch == 1);


            matrix[applex * 50 + appley].bgColor = "blue";
            eat = 0;
        }



        lastx = body[body.length - 1].x;
        lasty = body[body.length - 1].y;
        for (var i = 1; i < body.length; i++) {
            body[i].x = body[i - 1].x;
            body[i].y = body[i - 1].y;
        }


        if (dir == 1)--body[0].x;
        else if (dir == -1)++body[0].x;
        else if (dir == 2)--body[0].y;
        else if (dir == -2)++body[0].y;

        if (body[0].x == -1 || body[0].x == 50 || body[0].y == 50 || body[0].y == -1) {
            alert("GAME OVER!!");
            start = 0;
        }


        for (var i = 1; i < body.length; i++) {
            if (body[0].x == body[i].x && body[0].y == body[i].y) {
                alert("GAME OVER!!");
                start = 0;
                i = 10000;
            }
        }


        if (body[0].x == applex && appley == body[0].y) {
            eat = 1;
            body[body.length] = new node(lastx, lasty);
        }
        matrix[lastx * 50 + lasty].bgColor = "white";
        draw();
    }
}



function automove() {
    if (start == 1 && hal == 0) {
        if (key != -dir) dir = key;
        check_status();
        window.setTimeout("automove()", 200);
    }
}


function init() {
    start = 1;
    var x = document.getElementById("mine");
    var str = "<table id='tab' align='center' height='500px' cellSpacing='0' cellPadding='0' width='500px' border='4' >";


    for (var i = 0; i < 50; i++) {
        str += "<tr>";
        for (var j = 0; j < 50; j++)
        str += "<td></td>";
        str += "</tr>";
    }
    str += "</table>";
    x.innerHTML = str;


    matrix = document.getElementsByTagName("td");
    body = new Array();
    body[0] = new node(0, 0);
    draw();
    dir = key = -1;

    eat = 1;
    v = 0;
    hal = 0;
    automove();
}


function keypress(e) {
    if ((e.keyCode == 38) || ((e.which) && (e.which == 38))) //up
    key = 1;
    else if ((e.keyCode == 40) || ((e.which) && (e.which == 40))) //down
    key = -1;
    else if ((e.keyCode == 37) || ((e.which) && (e.which == 37))) //left
    key = 2;
    else if ((e.keyCode == 39) || ((e.which) && (e.which == 39))) //right
    key = -2;
    check_status();
}

</script>
</head>
<body onkeydown=keypress(event)>
<br/>
<input type="button" onClick="init()" value="play">
<input type="button" onClick="halt()" value="pause">
<p id="mine"></p>
<br><h5 id="score"></h5>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:59:07+00:00Added an answer on May 16, 2026 at 5:59 am

    The problem is here:

    lastx=body[body.length-1].x;
    lasty=body[body.length-1].y;
    for(var i=1;i<body.length;i++)
    {
        body[i].x=body[i-1].x;
        body[i].y=body[i-1].y;
    }
    

    In that loop, body[1] is assigned to body[0], then body [2] is assigned to body[1] etc. This means that everything from index 1 to the end will be set equal to body[0], then body[0] is altered based on direction – so there are only two positions.

    Look into the javascript unshift method.

    You could replace that loop with:

    body.unshift(body[0]);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple program using parallel python, and all works well. However,
I have written this code in JavaScript and works perfectly fine when I include
I have written a function that sorts a big scale of data. To test
I have written a java program generates lots of files (say txt files) in
I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written a very simple CTE expression that retrieves a list of all
I have lots of directories with text files written using (g)vim, and I have
We have an old (written in CakePHP 1.1!) application that has started groaning under
I've written a Binary Tree program that asks the user questions to learn about
I'm developing a little snake game written in C++ and using SFML 2D library.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.