Created
January 18, 2024 15:29
-
-
Save ssaurel/01123c58385dfb7b2a43ca4d186eb0fd to your computer and use it in GitHub Desktop.
Step 3 for creating a Snake on SSaurel's Blog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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