JavaScript-Snake icon indicating copy to clipboard operation
JavaScript-Snake copied to clipboard

Snake collision

Open lilplayer6k opened this issue 4 years ago • 3 comments

Hey there dear patorjk! i was playing a slightly altered version of your snake game recently and i wanted to ask if there could be a way to fix this weird snake collision bug where the snake could go backwards into itself and make you lose: image

lilplayer6k avatar Mar 11 '21 21:03 lilplayer6k

That problem doesn't appear to be in the current version of the project:

https://codesandbox.io/s/github/patorjk/JavaScript-Snake?file=/index.html

Looks like this line prevents the problem:

https://github.com/patorjk/JavaScript-Snake/blob/master/js/snake.js#L279

patorjk avatar Mar 12 '21 01:03 patorjk

that line is already in the code though

lilplayer6k avatar Mar 12 '21 17:03 lilplayer6k

So I was making my own snake game for a school project and happened to notice this funny little thing. Then I decided to see if other snake games had this. It still exists, but it's less than easy to pull off. Basically, because only two inputs are stored, if you manage to input three or more inputs before the next update, you can do a 180.

stillexists

In my project, I solved this by storing them in a list instead. Realistically though, this is a minor issue, and you would only encounter this if you were deliberately trying to.

Pikabyte avatar Nov 30 '21 21:11 Pikabyte