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

In summary, the author is having trouble getting a method of his Snake Game to work correctly. This is the method that is supposed to reset the position of the snake's food. The author has added a test at the end of the function to check if the food ever gets placed under the snake.
  • #1
Jamin2112
986
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?
 
Technology news on Phys.org
  • #2
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));
                    
                }
 
  • #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:

1. What is the purpose of the code in question?

The purpose of the code in question is to perform a specific task or function using JavaScript. Without knowing the specific code, it is difficult to determine the exact purpose.

2. What is the expected outcome of the code?

The expected outcome of the code is the result of the function or task it is trying to perform. This could be a specific value, action, or change to a webpage.

3. Is the code producing any errors or unexpected behavior?

It is important to check for any errors or unexpected behavior in the code, as this could indicate a flaw or mistake. This can be done by using a debugger or console.log statements to track the code's execution.

4. Have you checked for syntax errors or typos?

Syntax errors and typos are common mistakes in coding and can cause the code to not function as intended. It is important to carefully review the code for any spelling or formatting errors.

5. Have you tried researching the issue or asking for help from others?

If you are struggling to figure out the flaw in the code, it can be helpful to research the issue or ask for help from other programmers. There are many online resources and communities available for troubleshooting and problem-solving in JavaScript.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
Back
Top