<html lang="en">
<head>
<meta charset="UTF-8">
<title>snake</title>
<style type="text/css">
ul {
list-style: none;
margin: 0;
padding: 0;
height: 25px;
}
li {
width: 20px;
height: 20px;
float: left;
border: 1px solid #ccc;
margin: 1px;
}
li.on {
background-color: red;
}
li.head {
border: 1px dashed #FF7F00;
}
</style>
</head>
<body>
<script type="text/javascript">
function Snake(width, height) {
this.width = width;
this.height = height;
this.body = [];
this.orient = 1;
this.container = null;
this.timer = null;
this.speed = 300;
this.list = null;
this.count = width * height;
}
Snake.prototype = {
constructor: Snake,
init: function() {
var div = this.container = document.createElement('div');
ul = document.createElement('ul'),
li = document.createElement('li'),
rows = this.width,
cols = this.height;
while (rows-- > 0) {
ul.appendChild(li.cloneNode());
}
while (cols-- > 0) {
div.appendChild(ul.cloneNode(true));
}
document.getElementsByTagName('body')[0].appendChild(div);
this.list = document.getElementsByTagName('li');
[].forEach.call(this.list, function(item, i) {
item.id = 'li_' + (i + 1);
});
this.body.push(this.random());
return this;
},
random: function() {
if (this.count == this.body.length) {
return null;
}
var num, item;
do {
num = Math.floor((this.count - 1) * Math.random()) + 1;
item = document.getElementById('li_' + num);
} while (this.body.indexOf(item) > -1);
this.chioce(item);
return item;
},
getEle: function(item) {
var num = item.id.split('_').pop() - 0;
switch (this.orient) {
case 1:
num--;
num = num % this.width == 0 ? num + this.width : num;
break;
case 2:
num = num % this.width == 0 ? num - this.width + 1 : num + 1;
break;
case 3:
num = num / this.width <= 1 ? this.count - this.width + num : num - this.width;
break;
case 4:
num = num + this.width;
num = num > this.count ? num - this.count : num;
break;
}
return document.getElementById('li_' + num);
},
move: function() {
var next = this.getEle(this.body[0]);
if (this.body.indexOf(next) > -1) {
alert('game over!');
clearInterval(this.timer);
} else {
this.body.unshift(next);
if (!next.className.length) {
this.chioce(this.body[0]);
this.unchioce(this.body.pop());
} else {
this.random();
}
}
},
chioce: function(item) {
item.className = 'on';
},
unchioce: function(item) {
item.className = '';
},
start: function() {
var that = this;
that.random();
that.timer = setInterval(function() {
that.move();
}, that.speed);
return this;
}
}
window.onload = function() {
var snake = new Snake(10, 10).init().start();
document.onkeydown = function(e) {
var num = e.which;
if (num <= 40 && num >= 37) {
switch (num) {
case 37:
if (snake.orient != 2) {
snake.orient = 1;
}
break;
case 38:
if (snake.orient != 4){
snake.orient = 3;
}
break;
case 39:
if (snake.orient != 1){
snake.orient = 2;
}
break;
case 40:
if (snake.orient != 3){
snake.orient = 4;
}
break;
}
e.preventDefault();
}
}
}
</script>
</body>
</html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |