- #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.
For some reason, though, it's sometimes throwing the food off the grid. I added
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
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?
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);
}
Any ideas on what I'm not seeing?