本文实例为大家分享了javascript制作经典传统的拼图游戏的关键代码,供大家参考,具体内容如下
效果图:
拼出你喜欢的白雪公主和七个小矮人
实现代码:
<!DOCTYPE html> <html> <head> <title>pingtu.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=gb2312"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <style type="text/css"> *{ margin-bottom:0px; margin-top: 0px; margin-left: 0px; margin-right: 0px; padding-right: 0px; padding-left: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; } #box{ position:absolute; top:0px; left:0px; width: 300px; height: 300px; border: 1px solid red; } #box img{ float:left; width: 100px; height: 100px; } #box .pj,#box .p2,#box .p3,#box .p4,#box .p5,#box .p6,#box .p7,#box .p8,#box .p9{ position: absolute; } #box .pj,#box .p2,#box .p3{ top:0px; } #box .p4,#box .p5,#box .p6{ top:100px; } #box .p7,#box .p8,#box .p9{ top:200px; } #box .pj,#box .p4,#box .p7{ left:0px; } #box .p2,#box .p5,#box .p8{ left:100px; } #box .p3,#box .p6,#box .p9{ left:200px; } #button{ font-size:25px; font-weight:20px; float: left; position: absolute; top:400px; left:100px; } #output{ position:absolute; width: 270px; height: 170px; top:130px; left: 350px; } #output img{ height: 170px; width:170px; float: right; } #notice{ position: absolute; left: 650px; top:150px; width: 150px; height: 120px; border: 1px solid blue; font-size: 15px; } </style> </head> <body> <div id="box"> <img class="pj lazy" src="/static/imghwm/default1.png" data-src="img1/pj.png" alt="基于javascript制作经典传统的拼图游戏_javascript技巧" > <img class="p2 lazy" src="/static/imghwm/default1.png" data-src="img1/p2.png" alt="基于javascript制作经典传统的拼图游戏_javascript技巧" > <img class="p3 lazy" src="/static/imghwm/default1.png" data-src="img1/p3.png" alt="基于javascript制作经典传统的拼图游戏_javascript技巧" > <img class="p4 lazy" src="/static/imghwm/default1.png" data-src="img1/p4.png" alt="基于javascript制作经典传统的拼图游戏_javascript技巧" > <img class="p5 lazy" src="/static/imghwm/default1.png" data-src="img1/p5.png" alt="基于javascript制作经典传统的拼图游戏_javascript技巧" > <img class="p6 lazy" src="/static/imghwm/default1.png" data-src="img1/p6.png" alt="基于javascript制作经典传统的拼图游戏_javascript技巧" > <img class="p7 lazy" src="/static/imghwm/default1.png" data-src="img1/p7.png" alt="基于javascript制作经典传统的拼图游戏_javascript技巧" > <img class="p8 lazy" src="/static/imghwm/default1.png" data-src="img1/p8.png" alt="基于javascript制作经典传统的拼图游戏_javascript技巧" > <img class="p9 lazy" src="/static/imghwm/default1.png" data-src="img1/p9.png" alt="基于javascript制作经典传统的拼图游戏_javascript技巧" > </div> <div id="output"> 目标图形: <img src="/static/imghwm/default1.png" data-src="img1/output.jpg" class="lazy" alt=""> </div> <div id="notice"> 游戏提示:<br> <br> 点击“开始新游戏”开始游戏。鼠标点击黑色方块周围的方块时,即可移动方块。。 </div> <input type="button" id="button" value="开始新游戏"> <script type="text/javascript"> var times=0; var src= Array(); src.push("img1/pj.png"); src.push("img1/p2.png"); src.push("img1/p3.png"); src.push("img1/p4.png"); src.push("img1/p5.png"); src.push("img1/p6.png"); src.push("img1/p7.png"); src.push("img1/p8.png"); src.push("img1/p9.png"); function addLoadEvent(func) { //为window添加新事件函数 var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { oldonload(); func(); }; } } function getInfor(){ var ps=document.getElementById("box"); var Arrps=ps.getElementsByTagName("img"); for(var i=0;i<Arrps.length;i++){ Arrps[i].onclick=function(){ if (this.getAttribute("src")=="img1/pj.png"); changeP(this,Arrps); }; } } function tostar(){ var butt=document.getElementById("button"); butt.onclick=function(){ toST(); times=0; getInfor(); }; } function changeP(ob,Arrps){ var Ni=0; var Nj=0; for(var i=0;i<Arrps.length;i++){ if(Arrps[i]==ob) Ni=i; if(Arrps[i].getAttribute("src")=="img1/pj.png") Nj=i; } if(Math.abs(Ni-Nj)==3) { var temperOb=ob.getAttribute("src"); ob.setAttribute("src","img1/pj.png"); Arrps[Nj].setAttribute("src",temperOb); times++; ifright(); }else if((Ni-Nj)==1&&(Ni%3)!=0){ var temperOb=ob.getAttribute("src"); ob.setAttribute("src","img1/pj.png"); Arrps[Nj].setAttribute("src",temperOb); times++; ifright(); }else if((Ni-Nj)==-1&&(Ni%3)!=2){ var temperOb=ob.getAttribute("src"); ob.setAttribute("src","img1/pj.png"); Arrps[Nj].setAttribute("src",temperOb); times++; ifright(); } } function ifright(){ var ps=document.getElementById("box"); var Arrps=ps.getElementsByTagName("img"); for(var i=0;i<src.length;i++){ if(src[i]!=Arrps[i].getAttribute("src")) return; } if(times<50) alert("恭喜,你成功了。。"+"\n"+"仅用了"+times+"步哦"); else alert("恭喜,你成功了。。"+"\n"+"用了"+times+"步"); } function toST(){ var srcUsing= new Array(); for(var p=0; p<src.length;p++){ srcUsing[p]=src[p]; } var ps=document.getElementById("box"); var Arrps=ps.getElementsByTagName("img"); var newArry= new Array(); for(var i=0;i<Arrps.length;i++){ newArry.push(srcUsing.splice(Math.floor(Math.random()*srcUsing.length), 1)); } for(var j=0;j<newArry.length;j++) { //var newarrValue=newArry[j]; Arrps[j].setAttribute("src",String(newArry[j])); } } addLoadEvent(tostar); </script> </body> </html>
希望本文所述对大家学习javascript程序设计有所帮助,让大家动手亲自实现自己最喜爱的游戏——拼图。

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。

JavaScript框架的强大之处在于简化开发、提升用户体验和应用性能。选择框架时应考虑:1.项目规模和复杂度,2.团队经验,3.生态系统和社区支持。

引言我知道你可能会觉得奇怪,JavaScript、C 和浏览器之间到底有什么关系?它们之间看似毫无关联,但实际上,它们在现代网络开发中扮演着非常重要的角色。今天我们就来深入探讨一下这三者之间的紧密联系。通过这篇文章,你将了解到JavaScript如何在浏览器中运行,C 在浏览器引擎中的作用,以及它们如何共同推动网页的渲染和交互。JavaScript与浏览器的关系我们都知道,JavaScript是前端开发的核心语言,它直接在浏览器中运行,让网页变得生动有趣。你是否曾经想过,为什么JavaScr


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

禅工作室 13.0.1
功能强大的PHP集成开发环境