3 ms·
There's an annoying bug where you lose if you tap the button to go opposite the direction in which you're heading. e.g. if you're heading right and you tap left
by jlas 13y ago
There's an annoying bug where you lose if you tap the button to go opposite the direction in which you're heading. e.g. if you're heading right and you tap left, you lose.
- cmdkeen 13y agoPlenty of actual snake implementations had the same thing, so it is at least "authentic"!
- jlas 13y agoHere's my fix: http://jsfiddle.net/jlas/CT2pG/1/ http://jsfiddle.net/jlas/CT2pG/1/ Easy one line fix for this: need to check if new dx / dy is opposite the old dx / dy and use the old one if so. var _dx = (e.keyCode - 38) % 2, _dy = (e.keyCode - 39) % 2; dx = (_dx*-1 === dx) ? dx : _dx, dy = (_dy*-1 === dy) ? dy : _dy; I also added another fix to disallow moving twice during one cycle.