*{
padding: 0;
margin: 0;
}
#game {
width: 800px;
height: 600px;
background: url(img/chengwomen.jpg) repeat-x;
background-size: cover;
overflow: hidden;
position: relative;
}
#bid {
width: 40px;
height: 40px;
background-color: red;
position: absolute;
top: 100px;
left: 100px;
}
</style>
if(running){
sky.x -= 5;
game.style.backgroundPositionX = sky.x + ‘px’;
bird.speedY +=1;
bird.y +=bird.speedY;
if(bird.y<0){
running = false;
bird.y = 0;
}
if(bird.y+birdred.offsetHeight>600){
running = false;
bird.y = 600-birdred.offsetHeight;
console.log(bird.y);
}
birdred.style.top = bird.y + ‘px’;
}
},30);
//点击文档的时候实现小鸟向上运动
document.onclick = function () {
bird.speedY = -10;
}
function createPipe(position){
var pipe = {};
pipe.x = position;
pipe.uHeight = 200+parseInt(Math.random()*100);
pipe.dHeight = 400-pipe.uHeight;
pipe.dtop = pipe.uHeight+200;
var uPipe = document.createElement('div');
uPipe.style.width = "50px";
uPipe.style.height = pipe.uHeight + 'px';
uPipe.style.position = 'absolute';
uPipe.style.backgroundColor="red";
uPipe.style.top = 0;
uPipe.style.left = pipe.x + 'px';
game.appendChild(uPipe);
var dPipe = document.createElement('div');
dPipe.style.width = "50px";
dPipe.style.height = pipe.dHeight + 'px';
dPipe.style.backgroundColor="green";
dPipe.style.position = 'absolute';
dPipe.style.top = pipe.dtop+'px';
dPipe.style.left = pipe.x + 'px';
game.appendChild(dPipe);
setInterval(function (){
if(running){
pipe.x -= 2;
uPipe.style.left = pipe.x + 'px';
dPipe.style.left = pipe.x + 'px';}
if(pipe.x< -50){
pipe.x = 800;
}
var uCheck = bird.x +40 > pipe.x &&bird.x<pipe.x+ 50&&bird.y<pipe.uHeight;
var dCheck = bird.x +40 > pipe.x &&bird.x<pipe.x+ 50&&bird.y>pipe.uHeight+200;
if(uCheck||dCheck){
running = false;
}
},30)
}
createPipe(400);
createPipe(600);
createPipe(800);
createPipe(1000);
</script>