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

  • Context: Java 
  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Figure Javascript
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
Jamin2112
Messages
973
Reaction score
12
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:
                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:
                    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:
                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?
 
Physics news on Phys.org
Hmmm. This seems to work:

Code:
                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));
                    
                }
 
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
 
Last edited by a moderator: