2019 10 15
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>fly bird</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
#game {
position: relative;
width: 800px;
height: 600px;
-ms-background-position-x: -50px;
background: url(images/sky.png);
overflow: hidden;
}
#bird {
width: 34px;
height: 25px;
background: url(images/birds.png) -8px -10px;
top: 100px;
left: 100px;
position: absolute;
}
</style>
</head>
<body>
<div id="game">
<div id="bird">
</div>
</div>
<script>
var game = document.getElementById('game');
var birdEl = document.getElementById('bird');
var sky = {
x : 0
}
var bird = {
speedX : 5,
speedY : 0,
x : birdEl.offsetLeft,
y : birdEl.offsetTop
}
var running = true;
setInterval(function() {
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 + birdEl.offsetHeight > 600) {
running = false;
bird.y = 600-birdEl.offsetHeight;
}
birdEl.style.top = bird.y + 'px';
}
},20)
document.onclick = function() {
bird.speedY = -10;
}
function createPipe(position) {
var pipe = {};
pipe.x = position;
pipe.uHeight =200 + parseInt((Math.random() *100));
pipe.dHeight =600 - pipe.uHeight -200;
pipe.dTop = pipe.uHeight +200;
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) no-repeat center bottom';
game.appendChild(uPipe);
var dPipe = document.createElement('div');
dPipe.style.width = '52px';
dPipe.style.height = pipe.dHeight + 'px';
dPipe.style.background = 'url(images/pipe1.png) no-repeat center top';
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 <-52) {
pipe.x = 800;
}
var uCheck = bird.x + 34 > pipe.x && bird.x < pipe.x + 52 && bird.y < pipe.uHeight;
var dCheck = bird.x + 34 > pipe.x && bird.x < pipe.x + 52 && bird.y > pipe.uHeight + 200;
if (uCheck || dCheck) {
running = false;
alert('You juse like silly boy!!!');
}
}
},20)
}
createPipe(400);
createPipe(600);
createPipe(800);
createPipe(1000);
</script>
</body>
</html>