Internship14

本文介绍了一个基于HTML5 Canvas实现的小鸟游戏案例,详细展示了如何使用CSS和JavaScript来创建游戏背景、小鸟角色及管道障碍物,并通过代码实现了小鸟的上下移动、背景滚动及碰撞检测等功能。

小鸟游戏案例

今天所学的小鸟案例代码

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style type="text/css">
		* {
			margin: 0;
			padding: 0;
		}

		#game {
			width: 800px;
			height: 600px;
			background: url(imagesgame/sky.png);
			position: relative;
			background-position-x:50px;
			overflow: hidden;
		}

		#bird {
			width: 34px;
			height: 25px;
			background: url(imagesgame/birds.png) -8px -10px;
			position: absolute;
			top: 100px;
			left: 100px;

		}
	</style>
</head>
<body>
	<!--搭建骨架-->
	<div id="game">
		<div id="bird"></div>
	</div>
</body>
<script>
	window.onload = function(){
		


	}
	//让小鸟飞起来 背景向后移动
	//获取元素
	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;
		game.style.backgroundPositionX = sky.x + 'px';
		//实现小鸟的上下移动
		//距离 = 时间 * 速度
		bird.speedY +=1;  // 加速度 为 1
		bird.y += bird.speedY;
		if (bird.y < 0) {
			running = false;
			bird.y = 0;
		}
		//当小鸟的offsetTop + 自身的高度大于背景高度时游戏停止 小鸟固定
		if (bird.y + birdel.offsetHeight > 600) {
			running = false;
			bird.y = 600 - birdel.offsetHeight;
		}
			birdel.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 = 600 - pipe.uHeight - 200;
		//下管top值
		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(imagesgame/pipe2.png) center bottom';
		game.appendChild(uPipe);
		//上管道end

		//下管道start
		var dPipe = document.createElement('div');
		dPipe.style.width = '52px';
		dPipe.style.height = pipe.dHeight + 'px';

		dPipe.style.position = 'absolute';
		dPipe.style.top = pipe.dTop + 'px';
		dPipe.style.left = pipe.x + 'px';
		dPipe.style.background = 'url(imagesgame/pipe1.png) center top';
		game.appendChild(dPipe);

		//让管道动起来
		setInterval(function() {
			if(running) {
				pipe.x -= 1;
				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;
			}

		},2)
	}


createPipe(400);
createPipe(600);
createPipe(800);
createPipe(1000);
	


	
</script>
</html>
unsupported at new Hash (node:internal/crypto/hash:79:19) at Object.createHash (node:crypto:139:10) at module.exports (D:\javaEE\internship\personmis\personmis-vue\node_modules\webpack\lib\util\createHash.js:135:53) at NormalModule._initBuildHash (D:\javaEE\internship\personmis\personmis-vue\node_modules\webpack\lib\NormalModule.js:417:16) at handleParseError (D:\javaEE\internship\personmis\personmis-vue\node_modules\webpack\lib\NormalModule.js:471:10) at D:\javaEE\internship\personmis\personmis-vue\node_modules\webpack\lib\NormalModule.js:503:5 at D:\javaEE\internship\personmis\personmis-vue\node_modules\webpack\lib\NormalModule.js:358:12 at D:\javaEE\internship\personmis\personmis-vue\node_modules\loader-runner\lib\LoaderRunner.js:373:3 at iterateNormalLoaders (D:\javaEE\internship\personmis\personmis-vue\node_modules\loader-runner\lib\LoaderRunner.js:214:10) at iterateNormalLoaders (D:\javaEE\internship\personmis\personmis-vue\node_modules\loader-runner\lib\LoaderRunner.js:221:10) at D:\javaEE\internship\personmis\personmis-vue\node_modules\loader-runner\lib\LoaderRunner.js:236:3 at runSyncOrAsync (D:\javaEE\internship\personmis\personmis-vue\node_modules\loader-runner\lib\LoaderRunner.js:130:11) at iterateNormalLoaders (D:\javaEE\internship\personmis\personmis-vue\node_modules\loader-runner\lib\LoaderRunner.js:232:2) at Array.<anonymous> (D:\javaEE\internship\personmis\personmis-vue\node_modules\loader-runner\lib\LoaderRunner.js:205:4) at Storage.finished (D:\javaEE\internship\personmis\personmis-vue\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:55:16) at D:\javaEE\internship\personmis\personmis-vue\node_modules\enhanced-resolve\lib\CachedInputFileSystem.js:91:9 10% building 2/5 modules 3 active ...\personmis\personmis-vue\node_modules\eslint-loader\index.js??ref--13-0!D:\javaEE\internship\personmis\personmis-vue\src\main.jsnode:internal/crypto/hash:79 this[kHandle] = new _Hash(algori
05-28
根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值