<script type="text/javascript">
//让小鸟飞起来
var game = document.getElementById('game');
var idEl = document.getElementById('id');
//以键值对的形式保存小鸟和背景的参数
var sky = {
x:0
}
var id = {
speadX:5,
speadY:0,
x:idEl.offsetLeft,
y:idEl.offsetTop
}
//判断游戏进程 需要一个状态值
var running = true;
// 定义定时器 是游戏背景移动
setInterval(function(){
if(running){
sky.x -=5;
game.style.backgroundPositionX= sky.x+'px';
//实现小鸟上下移动
//距离 = 时间*速度
id.speadY +=1;
id.y+=id.speadY;
if(id.y<0){
running = false;
id.y=0;
}
//当小鸟的offsetTop +自身高度 大于背景高度 游戏停止 小鸟固定
if(id.y+idEl.offsetHeight> 600){
running = false;
id.y = 600-idEl.offsetHeight;
}
idEl.style.top = id.y +'px';
}
},20)
//点击文档小鸟实现向上运动
document.onclick = function(){
id.speadY = -15;
}
//创建管道的函数
function creatrPipe(position){
//管道有很多参数 水平位置 自身高度 上下两根管子
var pipe = {};
//水平位置
pipe.x = position;
//上管的高度
pipe.uHeight =200 + parseInt((Math.random()*100))
// console.log(pipe.uHeight);
//下管的高度
pipe.dHeight =600 -pipe.uHeight-200;
pipe.dTop = pipe.uHeight+200;
//创建一个div
var uPipe = document.createElement('div');
uPipe.style.width = '52px';
uPipe.style.height = pipe.uHeight +'px';
uPipe.style.position='absolute';
uPipe.style.top = '0px';
uPipe.style.left= pipe.x+'px';
uPipe.style.background ='url(images/pipe2.png) center bottom';
game.appendChild(uPipe);
}
creatrPipe(400);
</script>
10.14 brid
最新推荐文章于 2024-08-01 10:40:07 发布