Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Can someone help me figure out the flaw here? (JavaScript)

  1. Sep 1, 2014 #1
    Either because I'm tired or because I'm stupid, I'm having trouble getting a method of my Snake Game to work correctly. This is the method that is supposed to reset the position of the snake's food.

    Code (Text):

                    this.moveFood = function()
                    {
                        var tx, ty, fc, bbfc;
                        do {
                             fc = this.food; // copy of food
                             bbfc = fc.getBBox(); // bounding box of food copy
                             // tx, ty: random translation units
                             tx = randInt(0, C_w / this.linkSize - 1) * this.linkSize - bbfc.x;
                             ty = randInt(0, C_h / this.linkSize - 1) * this.linkSize - bbfc.y;
                             fc.translate(tx, ty); // translate copy of food
                             bbfc = fc.getBBox(); // update fcbb
                             console.log("new units: (" + bbfc.x + "," + bbfc.y + ")");
                        } while (this.hitSnake(bbfc));
                        /* If we made it here, then tx and ty are valid translation units, since
                        they won't land the food on the snake */
                        this.food.translate(tx, ty);
                    }
     
    For some reason, though, it's sometimes throwing the food off the grid. I added

    Code (Text):

                        var bbf = this.food.getBBox();
                        console.log("("+bbf.x+","+bbf.y+")");
     
    at the end of the function as a test, and saw (500, -50) outputted on my JS console. The reason this doesn't make sense to me is that my previous method was simply

    Code (Text):

                    this.moveFood = function()
                    {
                         var bbf = this.food.getBBox(); // bounding box for the food
                         /* tx, ty: random translation units */
                         var tx = randInt(0,C_w / this.linkSize - 1) * this.linkSize - bbf.x;
                         var ty = randInt(0,C_h / this.linkSize - 1) * this.linkSize - bbf.y;
                         this.food.translate(tx, ty);
                    }
     
    and that never threw the food off the grid (although it sometimes threw the food on the snake -- the problem I'm trying to elegantly fix with my updated method).

    Any ideas on what I'm not seeing?
     
  2. jcsd
  3. Sep 1, 2014 #2
    Hmmm. This seems to work:

    Code (Text):

                    this.moveFood = function()
                    {  
                        var bbf = this.food.getBBox(); // bounding box for food
                        do {
                        /* tx, ty: random translation units */
                        tx = randInt(0, C_w / this.linkSize - 1) * this.linkSize - bbf.x;
                        ty = randInt(0, C_h / this.linkSize - 1) * this.linkSize - bbf.y;
                         // translate copy of food
                        this.food.translate(tx, ty);
                        bbf = this.food.getBBox(); // update bbf
                        } while (this.hitSnake(bbf));
                       
                    }
     
     
  4. Sep 1, 2014 #3
    Nevermind. Let me know if the food ever gets place under the snake when you try the game: http://jaminweb.com/snake_TEST_PHP.php [Broken]
     
    Last edited by a moderator: May 6, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Can someone help me figure out the flaw here? (JavaScript)
  1. Javascript Help (Replies: 4)

Loading...