javascript编写贪吃蛇游戏
作者:hebedich 时间:2024-04-22 22:31:00
代码很简单,这里就不多BB了,小伙伴们直接看示例吧
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<style>
*{
margin:0;
padding:0;
}
#wrap{
position:relative;width:400px;height:400px;
border:1px solid #ccc;
margin:10px auto;
}
.snak, .snakBody{
position:absolute;
width:10px;height:10px;
background:#666;
}
.food{
position:absolute;width:10px;height:10px;
background:#09F;display:block;
}
</style>
<div id="wrap">
</div>
<script src="http://ftp152341.host180.web522.com/%E6%89%93%E8%9C%9C%E8%9C%82/zepto.js"></script>
<script>
var Snak, Food;;
Food = function(op){ //op为zepto对象
var food = $('<span class="food"></span>');
food.css({ left : Math.floor(((op[0].clientWidth-10)*Math.random())), top : Math.floor(((op[0].clientHeight-10)*Math.random())) })
op.append( food );
};
Snak = function(op){
this.obj = document.createElement('div');
this.obj.className = 'snak';
this.op = op;
};
Snak.prototype.ev = function(){
var _this = this, code;
$(window).bind('keydown',function(e){
clearInterval(_this.downTimer);
code = e.keyCode;
_this.downTimer = setInterval(function(){
var l = _this.obj.offsetLeft, t = _this.obj.offsetTop;
switch( code ){
case 37 :
l = l - 5;
break;
case 38 :
t = t - 5;
break;
case 39 :
l = l + 5;
break;
case 40:
t = t + 5;
break;
};
if( (l<0) || (t<0) || (l>400) || (t>400)){location.reload()};
_this.obj.style.left = l + 'px';
_this.obj.style.top = t + 'px';
var snakB = $('.snakBody');
for(var i=snakB.length-1; i>=0; i--){
if(i == 0){
snakB[0].style.left = l + 'px';
snakB[0].style.top = t + 'px';
}else{
snakB[i].style.left = snakB[i-1].offsetLeft + 'px';
snakB[i].style.top = snakB[i-1].offsetTop + 'px';
};
};
if( pz(_this.obj,$('.food')[0]) ){
$('.food').remove();
new Food(wrap);
$('<div class="snakBody"></div>').appendTo(wrap)
};
},30);
}).bind('keyup',function(e){
});
};
function pz(obj1,obj2){
var L1 = obj1.offsetLeft;
var T1 = obj1.offsetTop;
var R1 = L1 + obj1.offsetWidth;
var B1 = T1 + obj1.offsetHeight;
var L2 = obj2.offsetLeft;
var T2 = obj2.offsetTop;
var R2 = L2 + obj2.offsetWidth;
var B2 = T2 + obj2.offsetHeight;
if(L1 >= R2 || T1 >= B2 || R1 <= L2 || B1 <= T2){
return false;
}
return true;
};
var wrap = $('#wrap'),snak = new Snak(food);
var food = new Food( wrap );
snak.ev();
wrap.append( $('<div class="snakBody"></div>') )[0].appendChild( snak.obj );
</script>
</body>
</html>
以上所述就是本文的全部内容了,希望大家能够喜欢。
标签:javascript,贪吃蛇
0
投稿
猜你喜欢
Python torch.fft.rfft()函数用法示例代码
2022-02-15 02:03:36
Mysql数据库之Binlog日志使用总结(必看篇)
2024-01-20 20:15:16
MySQL 编码机制
2024-01-14 23:52:38
Win7 安装软件时无法连接sql server解决方法
2024-01-18 12:10:59
MySQL触发器的使用场景及方法实例
2024-01-23 04:45:12
Python算法的时间复杂度和空间复杂度(实例解析)
2022-09-26 03:07:06
Golang实现http server提供压缩文件下载功能
2024-05-09 14:56:00
Python有关Unicode UTF-8 GBK编码问题详解
2021-04-01 10:40:13
Tensorflow 卷积的梯度反向传播过程
2021-02-05 09:15:29
asp.net连接查询SQL数据库并把结果显示在网页上(2种方法)
2024-01-12 13:28:10
在vue中使用G2图表的示例代码
2024-05-28 15:52:22
php+ajax无刷新上传图片实例代码
2023-11-17 11:27:58
Centos7下安装MySQL8.0.23的步骤(小白入门级别)
2024-01-22 02:10:34
python django中8000端口被占用的解决
2021-07-14 02:43:19
用python下载百度文库的代码
2023-07-31 00:40:44
Vue2.0系列之过滤器的使用
2024-04-10 10:32:24
Python中弱引用的神奇用法与原理详解
2023-01-12 04:27:08
SQL Server并发处理存在就更新解决方案探讨
2024-01-17 16:54:06
大牌 Banner 设计欣赏(468x60)
2008-01-20 13:02:00
用 onerror 获取错误信息 js Debug
2008-11-03 19:08:00