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
Click For Summary
SUMMARY

The discussion centers on a JavaScript method for resetting the position of food in a Snake Game. The original implementation occasionally placed the food off the grid, resulting in coordinates like (500, -50). The user modified the method to include a loop that checks for collisions with the snake, ensuring valid translation units. The final solution successfully prevents the food from being placed under the snake while maintaining grid constraints.

PREREQUISITES
  • JavaScript programming knowledge
  • Understanding of bounding box calculations in SVG (getBBox method)
  • Familiarity with random number generation (randInt function)
  • Basic concepts of game development and collision detection
NEXT STEPS
  • Explore advanced collision detection algorithms in JavaScript
  • Learn about SVG transformations and their impact on game objects
  • Investigate optimization techniques for game loop performance
  • Study best practices for handling game state and object positioning
USEFUL FOR

Game developers, JavaScript programmers, and anyone interested in improving collision detection and object positioning in 2D games.

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?
 
Technology 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:

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K