IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Juqery+html 小游戏 -> 正文阅读

[游戏开发]Juqery+html 小游戏

创建HTML样式


/*外部样式*/


*{

				margin: 0;
				padding: 0;
				}
				body,html{
					width: 100%;
					height: 100%;
					background:pink;
					overflow: hidden;	
				}


            /*云朵运动*/
					.coloud_bg{
						animation: coloud_bg 20s linear infinite;
					}
					@keyframes coloud_bg{
						0%{
							transform: scale(1);
						
						}
						50%{
							transform: scale(2);
							
						}
						100%{
							transform: scale(1);
						
						}
					}

          /* 云朵定位*/
				.cloud{
					display: block;
					position: absolute;
				}
				
				/* 父级容器 */
				.car_box{
					width: 60%;
					height: 100%;
					background:skyblue;
					position: absolute;
					left: 25%;
					border-left: 8px solid white;
					border-right: 8px solid white;
					box-shadow: 0px 0px 30px black;
				}
				
				/* 我的样式 */
				#ar{
					width:15%;
					height: 20%;
					background: url(img/11.png) no-repeat center;
					position: absolute;
					background-size: 80%;
					left:42% ;
					bottom: 10%;
				}
				/* 敌方样式 */
				.dicar{
					width:15%;
					height: 20%;
					background: url(img/1.png) no-repeat center;
					position: absolute;
					background-size: 80%;
				}
				
				
				/* 开始样式 */
			#start{
				    width: 150px;
				    height:40px;
				    position:absolute;
				    left: 30px;
				    top: 20px;
				    border-radius: 10px;
				    box-shadow: #BBBBBB;
				    cursor: pointer;
					color: red;
					display: block;
					border: 1px;
				   line-height:50px;
				   font-size: 30px;
				   background:white;
				   text-align:center; 
				}

       /* 分数样式 */
		   #score{
			    width: 150px;
			     height: 90px;
			       position:absolute;
				   top: 100px;
				   font-size: 24px;
				   background:white;
				    border-radius: 10px;
					color: red;
					 left: 30px;
					 margin: auto;
				} 
		</style>

body部分

</head>
	<body>
		<img class="cloud  coloud_bg" src="img/cloud.png"/>
		<div class="car_box">
			<div id="ar"></div>
			<!-- <div class="dicar"></div> -->
		</div>
		<div> 
		   <span id="start"> 开始</span>
		    <span id="score">得分:</span>
		   </div>

创建好样式的图片

?编写script

<script src="js/jquery-1.12.4.js" type="text/javascript"></script>
	<script type="text/javascript">
		//初始
		var score=0
		var timer=null
		var  scot=null
		//恐龙
		$(function () {
			//移动
			carMove()
			//生产
		  addItemCar()
				
		})
		//我方恐龙
		var code=2
		function carMove(){
			document.onkeydown=function (even){
				//alert("键盘"+even.keyCode)
				if(even.keyCode==37){
					//左移
					code--
					if(code<0){
						code=0
					}
				}
				if(even.keyCode==39){
					//右移
					code++
					if(code>4){
						code=4
					}
				}
				var left_c=code*20+2
				//动画
				$("#ar").animate({"left":left_c+"%" },50)
			}
		}
		//生产水果的方法
		function item_car(){
			//水果
		var item=document.createElement("div")
		
		 $(".car_box").append(item)
		 
		 item.className="dicar"
		 //随机图片
		var rand_img=Math.floor(Math.random()*3+1)
		//设定
		item.style.backgroundImage="url(img/"+rand_img+".png)"
		//随机出现
		var rand_code=Math.floor(Math.random()*5) 
		//给left赋值
		$(item).css("left",rand_code*20+2+"%")
		//速度
		var rand_speed=Math.floor(Math.random()*2000+800)
		$(item).animate({"top":"100%"},rand_speed,function(){
		    $(this).remove()
			 //加分
		     score++
		})
		     //碰
			 carko(item)	 
		}
	 //批量生产水果
		function addItemCar(){
			setInterval(function(){
				item_car()
		},1000)
		}
		  //碰撞检测
	  function carko(item){
		  var ar=document.getElementById("ar")
		 timer= setInterval( function(){
			 
			 var artop=ar.offsetTop
			 var arleft=ar.offsetLeft				
			 var arheight=ar.offsetHeight
			 var  arwidth=ar.offsetWidth
			  var itemtop=item.offsetTop
		      var itemleft=item.offsetLeft
			  if(Math.abs(artop-itemtop)<arheight && Math.abs(arleft-itemleft)< arwidth ){
			  		gameOver()
				
				}
			  
		  },30)   
	  }  
	  function gameOver(){
		   //得分的刷新
		   $("#score").html("得分:"+score)
		     //淡入
		     $("#score").fadeIn(500)
	  	  	alert("游戏结束!!!")
		    $("#start").fadeIn(500)
		    	//停止生产
					clearInterval(timer)
		 			//离开
				 $("#ar").animate({"bottom":"-100%"},200)
	   			}
		 		//开始按钮
		 		$("#start").click(function(){
					$("#score").fadeOut(500)
		 			$("#start").fadeOut(500)
					//加载页面
					document.location.reload()
				})
	</script>
	</body>
</html>

运行的游戏

?

?

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-05 17:40:59  更:2021-08-05 17:42:11 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/6 10:14:03-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码