015-案例(1)

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>
		/*
			让小鸟飞起来
				1.背景向后移动
		*/
		//获取元素
		var game = document.getElementById('game');
		var birdEl = document.getElementById('bird');

		//以键值对的形式保存小鸟和背景的参数
		var sky = {
			x : 0
		}
		//初始化bird值
		var bird = {
			speedX : 5,
			speedY : 0,
			x : birdEl.offsetLeft,
			y : birdEl.offsetTop
		}
		//判断游戏是否开始  需要一个状态值
		var running = true;
		//定义定时器 使游戏背景移动
		setInterval(function() {
			if (running) {
			sky.x -= 5;
			//sky的值水平移动5
			game.style.backgroundPositionX = sky.x + 'px';
			//实现小鸟的上下移动
			//距离= 时间 * 速度
			bird.speedY += 1; //加速度为1
			bird.y += bird.speedY;
			if (bird.y < 0) {
				running = false;
				// alert('You juse like silly boy!!!');
				bird.y = 0;
			}
			//当小鸟offsettop + 自身高度 大于背景高度时游戏停止
			if (bird.y + birdEl.offsetHeight > 600) {
				running = false;
				// alert('You juse like silly boy!!!');
				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;

				//创建一个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) 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值