JS实现简单拼图游戏
点击“打乱”按钮开始游戏;按下鼠标左键拖动一块到另一块上松开鼠标左键互换。游戏资源为一张图片
我这里名称为 2.jpg,将其与下面的网页文件放在同一目录中即可。
网页文件源码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<style type="text/css">
div{
width: 200px;
height: 200px;
}
.tu{
background-image:url(2.jpg); /* 游戏图片 */
}
//利用background-position给每个div添加不同的图片;即9个div组成一幅完成的图片;
#z-1{
background-position: 0px 0px;
}
#z-2{
background-position:-200px 0px;
}
#z-3{
background-position:-400px 0px;
}
#z-4{
background-position:0 -200px;
}
#z-5{
background-position: -200px -200px;
}
#z-6{
background-position: -400px -200px;
}
#z-7{
background-position: 0px -400px;
}
#z-8{
background-position: -200px -400px;
}
#z-9{
background-position: -400px -400px;
}
</style>
<script>
//完成两个图片拖拽后的互换;
function over(e){
e.preventDefault();//阻止默认
}
//抓起
function drag(e){
var id=e.target.id;
// console.log(e.target.id);
e.dataTransfer.setData("id",id);//设置传输的是被抓id;
}
//经过
function drop(e){
var beizhuaId=e.dataTransfer.getData("id");//接受被抓id;
// console.log(beizhuaId);//被抓ID
var fangID=e.target.id;//所放位置的id;
var beizhua=document.getElementById(beizhuaId);//获取被抓对象;
var fang=document.getElementById(fangID);//获取放的对象;
var f_beizhua=beizhua.parentNode;//分别找到对应的父节点
var f_fang=fang.parentNode;
//互换儿子
f_beizhua.appendChild(fang);
f_fang.appendChild(beizhua);
win();
}
//判断赢得方法;每个父和子id名字序号相同,循环 ,累加count;
function win(){
var count=0;
var tus=document.getElementsByClassName('tu');
for(var i=0;i<tus.length;i++){
var tu=tus[i];
var fu=tu.parentNode;
var tu_id=tu.getAttribute("id");
var fu_id=fu.getAttribute("id");
if(tu_id.replace("z-","")==fu_id.replace("f-","")){
count++;
console.log(count);
}else{
return;
}
}
if(count==9){
document.getElementById("PromptText").innerHTML="你赢了!"
}
}
//打乱按钮;通过生成随机数;appenChild方法去多次互换,则为打乱;
function daluan(){
document.getElementById("PromptText").innerHTML=""
for(var i=0;i<100;i++){
var tus=document.getElementsByClassName('tu');
var m=parseInt(Math.random()*9);
var n=parseInt(Math.random()*9);
var tusmp=tus[m].parentNode;
var tusnp=tus[n].parentNode;
tusmp.appendChild(tus[n]);
tusnp.appendChild(tus[m]);
}
}
</script>
<body>
<table border="1">
<tr>
<td>
<div id="f-1" ondragover="over(event)" ondrop="drop(event)">
<div id="z-1" class="tu" draggable="true" ondragstart="drag(event)"></div>
</div>
</td>
<td>
<div id="f-2" ondragover="over(event)" ondrop="drop(event)">
<div id="z-2" class="tu" draggable="true" ondragstart="drag(event)"></div>
</div>
</td>
<td>
<div id="f-3" ondragover="over(event)" ondrop="drop(event)">
<div id="z-3" class="tu" draggable="true" ondragstart="drag(event)"></div>
</div>
</td>
</tr>
<tr>
<td>
<div id="f-4" ondragover="over(event)" ondrop="drop(event)">
<div id="z-4" class="tu" draggable="true" ondragstart="drag(event)"></div>
</div>
</td>
<td>
<div id="f-5" ondragover="over(event)" ondrop="drop(event)">
<div id="z-5" class="tu" draggable="true" ondragstart="drag(event)"></div>
</div>
</td>
<td>
<div id="f-6" ondragover="over(event)" ondrop="drop(event)">
<div id="z-6" class="tu" draggable="true" ondragstart="drag(event)"></div>
</div>
</td>
</tr>
<tr>
<td>
<div id="f-7" ondragover="over(event)" ondrop="drop(event)">
<div id="z-7" class="tu" draggable="true" ondragstart="drag(event)"></div>
</div>
</td>
<td>
<div id="f-8" ondragover="over(event)" ondrop="drop(event)">
<div id="z-8" class="tu" draggable="true" ondragstart="drag(event)"></div>
</div>
</td>
<td>
<div id="f-9" ondragover="over(event)" ondrop="drop(event)">
<div id="z-9" class="tu" draggable="true" ondragstart="drag(event)"></div>
</div>
</td>
</tr>
</table>
<input type="button" value="打乱" onclick="daluan()" />
点击“打乱”开始游戏;按下鼠标左键拖动一块到另一块上松开鼠标左键互换。
<p id ="PromptText" style="color: red;"></p>
</body>
</html>
用浏览器打开运行效果:
OK !