文章分类 Classification
72行代码贪吃蛇
稿件来源: csdn论坛 撰稿作者: 雾海树妖 发表日期: 2013-06-28 阅读次数: 70 查看权限: 游客查看
牛人在csdn论坛发布的原创贪吃蛇
<script type='text/javascript'> X = Y = 30; var S=''; for (var y = 0; y < Y; y++) for (var x = 0; x < X; x++) S+='<div style="display:inline-block;_display:inline;width:10px;height:10px;font-size:1px;background-color:#c0c0c0;" id="x' + x + 'y' + y + '"></div>'+(x == X - 1?'<br>':''); document.write(S); var sk = { Snake: [[X >> 1, Y >> 1]], Dir: [0, 1] }; function $(xy) { return document.getElementById('x' + xy[0] + 'y' + xy[1]); } function setc(obj, color) { obj.style.backgroundColor = color; } var cancel, R ,add = 2; (function() { cancel = 0; var st; with(sk) { for (var i=Snake.length; i--;) { if(i==Snake.length-1){ setc($(Snake[i]), '#c0c0c0'); st = Snake[i].join(',').split(','); } i && setc($(Snake[i] = Snake[i - 1].join(',').split(',')), 'blue'); if (uequ(Snake[i], R)) R = 0 * ++add; } if (add) { --add; Snake.push(st); } Snake[0][0] += Dir[0]; Snake[0][1] += Dir[1]; var res; if (res = ('x'+Snake.join('xx')+'x').match(new RegExp('x'+X + ',|,' + Y + 'x|-|(x\\d+,\\d+x).*?x\\1', 'ig'))) return alert(((res + '').match(/x.+?x/) ? '咬到自己!': '撞墙!')); setTimeout(arguments.callee,100); setc($(Snake[0]), 'red'); } setc($(R = R || [Math.random() * X | 0, Math.random() * Y | 0]), 'green'); })(); document.onkeydown = function(e) { e = e || event; e = e.keyCode || e.which; var w = { '38': [0, -1], '39': [1, 0], '40': [0, 1], '37': [ - 1, 0] }; if (!cancel && !uequ(sk.Dir, w[e])) { cancel = sk.Dir = w[e]||sk.Dir; } }; function uequ(a1, a2) { return (a1 + '\n' + a2).replace(/-|,/g, '').match(/^(\d+)\n\1$/); } alert('按确定开始游戏!'); </script>
关键词: csdn,贪吃蛇 编辑时间: 2013-06-28
0
高兴0
支持0
搞笑0
不解0
谎言0
枪稿0
震惊0
无奈0
无聊0
反对0
愤怒
0%(0)
0%(0)
- 暂无评论
文章图片 article Pictrue
网友评论