Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created January 18, 2024 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/01123c58385dfb7b2a43ca4d186eb0fd to your computer and use it in GitHub Desktop.
Save ssaurel/01123c58385dfb7b2a43ca4d186eb0fd to your computer and use it in GitHub Desktop.
Step 3 for creating a Snake on SSaurel's Blog
generatefood(nbx, nby) {
// check food is not on the snake
var self = this;
var touch = true;
var food = null;
while (touch) {
food = { x: randomInteger(0, this.nbx), y: randomInteger(0, this.nby) };
this.elements.every((element) => {
if (food.x == element.x && food.y == element.y) {
touch = true;
return false;
}
touch = false;
return true;
});
}
return food;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment