Snake Game in 30 lines of JavaScript
jsfiddle.net
jsfiddle.net
}, 200);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.Demo: http://tomocchino.github.io/react-snake/
Source (150 lines): https://github.com/tomocchino/react-snake/blob/master/src/sn...
[1] http://facebook.github.io/react/blog/2013/07/02/react-v0-4-a...
You can also take the advantage of getDefaultProps() instead of doing `this.props.something || 12` everywhere.
I wonder if this could be a good method for teaching technologies like CSS, JS, and HTML? If you think about it, there isn't a lot of code here, so it would be easy for a student to go through it line by line and end up learning a lot.
My attempt, 31 lines (although some of them are quite long :) http://jsfiddle.net/8Rqcy/5/
Has score counting, restarts, walls, you can't go back on yourself, and speed increases.
This one has three operations in one line:
newEl.className = newEl.className.replace(' f', ''), length++, hasFood = false; <div id="game"></div>
<script type="text/javascript">
$( "#game" ).snakegame();
</script>Proof of concept apps in few lines of code might be cool, but the devil is in the details. The last 10% takes 90% of the time.