+ 2
canāt add boundaries need help
so iām working on like a rip off of google snake rn and i have the borders but itās not actually stopping the snake and i can run into myself and idk how to fix it https://sololearn.com/compiler-playground/WIA474VMbN2S/?ref=app
14 Answers
+ 6
To fix the error, DOM Content must be loaded before any script.This guarantees that all elements on the page are available and fully rendered, preventing issues with scripts that depend on specific elements being present.
So, wrap your entire js code inside:-
document.addEventListener("DOMContentLoaded", () => {
//Your entire js program!!
});
OR
window.onload = () => {
//Your entire js program!!
}
+ 6
Try this in update function:-
if(newHead.x <= 0 || newHead.x >= canvas.width || newHead.y <= 0 || newHead.y >= canvas.width){
alert("game over");
}
And add the code you want to execute inside the if statement when the snake touches the border.
+ 4
I've been accused of being a bot a thousand times over and even joked about it in various community groups ... š
+ 3
Bob_Li thats actually sick thanks so much
+ 3
Xmosity
Maybe some are more convincing than others...
The difficulty level for "AI Among Us" gets higher as the technology evolves.
+ 2
Ethan
Android apps can't see it. You are probably using the website or iPhone.
Better to always wrap it inside onload function so Android app people can also see it without errors.
+ 1
Starlight
as suggested above, put all the js code inside an onload function.
In the js tab:
onload = () =>{ // <-- add this
//.... js code here ...
// ...
//at the very bottom, close the curly braces
} // <-- and this
+ 1
Ethan
I played around with your code a bit.
Wall collision would make the game hard to play. I made an easier wraparound option that is activated when the walls are off. Only self-collision ends the game in this mode.
I also added a walls button on top if you want to play with walls or not. In this mode, in addition to self-collision, collision with any wall ends the game.
https://sololearn.com/compiler-playground/WuhLXrKkvKw8/?ref=app
+ 1
only person on this site that dosent type like ai!?!1?1
0
first of all. where is your snake??
0
Starlight its the green cubešš
0
there's no green cube in the output
0
Starlight there is for me?
0
I'm on android app