+ 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

11th Nov 2024, 6:59 PM
Ethan
Ethan - avatar
14 Respuestas
+ 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!! }
12th Nov 2024, 12:39 AM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 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.
12th Nov 2024, 1:51 AM
Gulshan Mahawar
Gulshan Mahawar - avatar
+ 4
I've been accused of being a bot a thousand times over and even joked about it in various community groups ... 🙃
13th Nov 2024, 3:21 AM
BroFar
BroFar - avatar
+ 3
Bob_Li thats actually sick thanks so much
12th Nov 2024, 11:18 PM
Ethan
Ethan - avatar
+ 3
Xmosity Maybe some are more convincing than others... The difficulty level for "AI Among Us" gets higher as the technology evolves.
13th Nov 2024, 12:52 AM
Bob_Li
Bob_Li - avatar
+ 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.
12th Nov 2024, 9:15 AM
Bob_Li
Bob_Li - avatar
+ 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
12th Nov 2024, 9:27 AM
Bob_Li
Bob_Li - avatar
+ 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
12th Nov 2024, 12:32 PM
Bob_Li
Bob_Li - avatar
+ 1
only person on this site that dosent type like ai!?!1?1
12th Nov 2024, 8:11 PM
Xmosity
Xmosity - avatar
0
first of all. where is your snake??
11th Nov 2024, 8:11 PM
Starlight
0
Starlight its the green cube😭😭
11th Nov 2024, 9:01 PM
Ethan
Ethan - avatar
0
there's no green cube in the output
11th Nov 2024, 10:05 PM
Starlight
0
Starlight there is for me?
11th Nov 2024, 11:20 PM
Ethan
Ethan - avatar
0
I'm on android app
12th Nov 2024, 9:16 AM
Starlight