拼图游戏

a{

color:#FF0000;

text-decoration:none;

}

a:hover{

text-decoration:underline;

}

body{

background-repeat:no-repeat;

font-family: Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;

height:100%;

width:100%;

margin:10px;

padding-left:0px;

text-align:left;

}

#puzzle_container{

line-height:500px;

text-align:center;

vertical-align:center;

border:10px outset #CCCCCC;

position:relative;

color: #FFFFFF;

background-color: #707070;

width: 520px;/* IE 5.x */

width/* */:/**/500px;

width: /**/500px;

height: 520px;

height/* */:/**/500px;

height: /**/500px;

}

#puzzle_container .square{

overflow:hidden;

border-left:1px solid #FFF;

border-top:1px solid #FFF;

position:absolute;

}

.activeImageIndicator{

border:1px solid #FF0000;

position:absolute;

z-index:10000;

}

.revealedImage{

position:absolute;

left:0px;

opacity:0;

filter:alpha(opacity=50);

top:0px;

z-index:50000;

}

var puzzleImages = ['http://p1.mb5u.com/texiao/wall1.jpg','http://p1.mb5u.com/texiao/wall2.jpg','http://p1.mb5u.com/texiao/wall3.jpg','http://p1.mb5u.com/texiao/wall4.jpg','http://p1.mb5u.com/texiao/wall5.jpg','http://p1.mb5u.com/texiao/wall6.jpg','http://p1.mb5u.com/texiao/wall7.jpg','http://p1.mb5u.com/texiao/wall8.jpg'];

var rows = 3;

var cols = 4;

var imageArray = new Array();

var imageInUse = false;

var puzzleContainer;

var activeImageIndicator = false;

var activeSquare = false;

var squareArray = new Array();

var emptySquare_x;

var emptySquare_y;

var colWidth;

var rowHeight;

var gameInProgress = false;

var revealedImage = false;

for(var no=0;no

imageArray[no] = new Image();

imageArray[no].src = puzzleImages[no];

}

function initPuzzle()

{

gameInProgress = false;

var tmpInUse = imageInUse;

imageInUse = imageArray[Math.floor(Math.random() * puzzleImages.length)];

if(imageInUse==tmpInUse && puzzleImages.length>1)

initPuzzle();

else{

puzzleContainer = document.getElementById('puzzle_container');

getImageWidth();

}

}

function getImageWidth()

{

if(imageInUse.width>0){

startPuzzle();

}else{

setTimeout('getImageWidth()',100);

}

}

function scramble()

{

gameInProgress = true;

var currentRow = cols-1;

var currentCol = rows-1;

document.getElementById('revealedImage').style.display='none';

for(var no=0;no

for(var no2=0;no2

if(no

var el = document.getElementById('square_' + no2 + '_' + no);

if(el){

el.style.left = (no2 * colWidth) + (no2) + 'px';

el.style.top = (no * rowHeight) + (no) + 'px';

}else{

initPuzzle();

return;

}

}

}

}

var lastPos=false;

var countMoves = 0;

while(countMoves

var dir = Math.floor(Math.random()*4);

var readyToMove = false;

if(dir==0 && currentRow>0 && lastPos!=1){

currentRow = currentRow-1;

readyToMove = true;

}

if(dir==1 && currentRow

currentRow = currentRow+1;

readyToMove = true;

}

if(dir==2 && currentCol>0 && lastPos!=3){

currentCol = currentCol -1;

readyToMove = true;

}

if(dir==3 && currentCol

currentCol = currentCol + 1;

readyToMove = true;

}

if(readyToMove){

activeSquare = document.getElementById('square_' + currentCol + '_' + currentRow);

moveImage(false,true);

lastPos = dir;

countMoves++;

}

}

return;

}

function gameFinished()

{

var string = "";

var squareWidth = colWidth + 1;

var squareHeight = rowHeight + 1;

var squareCounter = 0;

var errorsFound = false;

var correctSquares = 0;

for(var prop in squareArray){

var currentCol = squareCounter % cols;

var currentRow = Math.floor(squareCounter/cols);

var correctLeft = currentCol * squareWidth;

var correctTop = currentRow * squareHeight;

if(squareArray[prop].style.left.replace('px','') != correctLeft || squareArray[prop].style.top.replace('px','') != correctTop){

//return;

}else{

correctSquares++;

}

squareCounter++;

}

if(correctSquares == ((cols * rows) -1)){

document.getElementById('messageDiv').innerHTML = '

Fantastic! You did it!

';

gameInProgress = false;

revealImage();

}else{

document.getElementById('messageDiv').innerHTML = 'Currently, you have ' + correctSquares + ' out of ' + ((cols * rows) -1) + ' pieces placed correctly';

}

}

var currentOpacity = 0;

function revealImage()

{

if(currentOpacity==100)currentOpacity=0;

var obj = document.getElementById('revealedImage');

obj.style.display = 'block';

currentOpacity = currentOpacity +2;

if(document.all){

obj.style.filter = 'alpha(opacity='+currentOpacity+')';

}else{

obj.style.opacity = currentOpacity/100;

}

if(currentOpacity<100)setTimeout('revealImage()',10);

}

function displayActiveImage()

{

if(!gameInProgress)return;

if(!activeImageIndicator){

activeImageIndicator = document.createElement('DIV');

activeImageIndicator.className = 'activeImageIndicator';

puzzleContainer.appendChild(activeImageIndicator);

activeImageIndicator.onclick = moveImage;

}

activeImageIndicator.style.display='block';

activeImageIndicator.style.left = this.offsetLeft + 'px';

activeImageIndicator.style.top = this.offsetTop + 'px';

activeImageIndicator.style.width = this.style.width;

activeImageIndicator.style.height = this.style.height;

activeImageIndicator.innerHTML = '';

activeSquare = this;

}

function moveImage(e,fromShuffleFunction)

{

if(!activeSquare)return;

if(!gameInProgress && !fromShuffleFunction){

alert('You have to shuffle the cards first');

return;

}

var currentLeft = activeSquare.style.left.replace('px','') /1;

var currentTop = activeSquare.style.top.replace('px','') /1;

var diffLeft = Math.round((currentLeft - emptySquare_x) / colWidth);

var diffTop = Math.round((currentTop - emptySquare_y) / rowHeight);

if(((diffLeft==-1 || diffLeft==1) && diffTop==0) || ((diffTop==-1 || diffTop==1) && diffLeft==0)){

activeSquare.style.left = emptySquare_x + 'px';

activeSquare.style.top = emptySquare_y + 'px';

emptySquare_x = currentLeft;

emptySquare_y = currentTop;

activeSquare = false;

if(activeImageIndicator)activeImageIndicator.style.display = 'none';

if(!fromShuffleFunction)gameFinished();

}

}

function startPuzzle()

{

puzzleContainer.innerHTML = '';

var subDivs = puzzleContainer.getElementsByTagName('DIV');

for(var no=0;no

subDivs[no].parentNode.removeChild(subDivs[no]);

}

activeImageIndicator = false;

squareArray.length = 0;

if(document.getElementById('revealedImage')){

var obj = document.getElementById('revealedImage');

obj.parentNode.removeChild(obj);

}

var revealedImage = document.createElement('DIV');

revealedImage.style.display='none';

revealedImage.id='revealedImage';;

revealedImage.className='revealedImage';;

var img = document.createElement('IMG');

img.src = imageInUse.src;

revealedImage.appendChild(img);

puzzleContainer.appendChild(revealedImage);

colWidth = Math.round(imageInUse.width / cols)-1;

rowHeight = Math.round(imageInUse.height / rows)-1;

puzzleContainer.style.width = colWidth * cols + (cols * 1) + 'px';

puzzleContainer.style.height = rowHeight * rows + (rows * 1) + 'px';

if(navigator.appVersion.indexOf('5.')>=0 && navigator.userAgent.indexOf('MSIE')>=0){

puzzleContainer.style.width = colWidth * cols + (cols * 1) + 20 + 'px';

puzzleContainer.style.height = rowHeight * rows + (rows * 1) + 20 + 'px';

}

if(!revealedImage){

revealedImage = document.createElement('DIV');

revealedImage.style.display='none';

revealedImage.innerHTML = '';

}

for(var no=0;no

for(var no2=0;no2

if(no2==cols-1 && no==rows-1){

emptySquare_x = (no2 * colWidth) + (no2);

emptySquare_y = (no * rowHeight) + (no);

break;

}

var newDiv = document.createElement('DIV');

newDiv.id = 'square_' + no2 + '_' + no;

newDiv.onmouseover = displayActiveImage;

newDiv.onclick = moveImage;

newDiv.className = 'square';

newDiv.style.width = colWidth + 'px';

newDiv.style.height = rowHeight + 'px';

newDiv.style.left = (no2 * colWidth) + (no2) + 'px';

newDiv.style.top = (no * rowHeight) + (no) + 'px';

newDiv.setAttribute('initPosition',(no2 * colWidth) + (no2) + '_' + (no * rowHeight) + (no));

var img = new Image();

img.src = imageInUse.src;

img.style.position = 'absolute';

img.style.left = 0 - (no2 * colWidth) + 'px';

img.style.top = 0 - (no * rowHeight) + 'px';

newDiv.appendChild(img);

puzzleContainer.appendChild(newDiv);

squareArray.push(newDiv);

}

}

}

window.onload = initPuzzle;

打乱 | 换一张图

Columns/Rows: ×

Please wait - initializing script

var gaJsHost = (("https:" == document.location.protocol) ? "https://www.baidu.com" : "http://www.mb5u.com");

document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

try {

var pageTracker = _gat._getTracker("UA-1334907-4");

pageTracker._trackPageview();

}

catch(err) {}

打开后请等待加载,因背景图较大,需要一定时间加载,你可以刷新页面试一下。

网页拼图游戏html代码,JavaScript网页拼图游戏相关推荐

  1. html document.location转到另一个网页,常用跳转代码 打开网页转到其他网址

    打开立马跳转 其他收藏记录 收集了几种比较经典.比较全的跳转代码和加载代码,做自己个人网站的时候经常用到,下面分享给大家: window.location.href='http://www.webuc ...

  2. html五子棋游戏制作原理,JavaScript实现五子棋游戏的方法详解

    本文实例讲述了JavaScript实现五子棋游戏的方法.分享给大家供大家参考,具体如下: 最近半个月一直在看深入的学习JavaScript,里面有很多重点和难点,比如闭包.词法分析.面向对象等.今天给 ...

  3. 学生HTML游戏网页作业作品——HTML+CSS+JavaScript魔域私服游戏HTML(1个页面)

  4. 学生HTML游戏网页作业作品——html css javascript魔域私服游戏HTML(1个页面)

  5. 网页制作表单代码java_网页制作的40个经典代码

    1. οncοntextmenu="window.event.returnValue=false" 将彻底屏蔽鼠标右键 可用于Table 2. 取消选取.防止复制 3. οnpas ...

  6. java 模拟网页_使用Java代码模拟网页登录(有例子程序)

    最近股票很火,打算研究用代码下载股票数据,于是我做了一个Java代码的模拟IE网页登录的程序, 登录新浪的股票页面并下载一些数据来分析, 基本原理是利用Java自带的HttpURLConnection ...

  7. 网页设计框架布局代码_网页设计中的分屏切割布局法

    今天和大家聊的是一种关于网页设计中的视觉呈现方法,一般我们称之为"分屏布局",是一种常见的网页设计排版方式,将页面分割为均等或者不均等的两部分,这种方式的好处就是可以将文字信息和图 ...

  8. php俄罗斯方块代码,JavaScript实现俄罗斯方块游戏过程分析及源码分享_javascript技巧...

    2)旋转, 需要数理逻辑, 一个点相对另外一个点旋转90度的问题. 3)定时和监听键盘事件机制让游戏自动运行下去. //开始 function begin(e){ e.disabled = true; ...

  9. 网页计算器源php代码,计算器网页版源代码

    ?????? .panel { border: 4px solid #ddd; width: 192px; margin: 100px auto; /border-radius: 6px;/ } .p ...

最新文章

  1. jekyll静态博客提升访问速度:内嵌CSS,异步加载js,压缩HTML
  2. 远程网络读取服务器文件是否存在,远程读取服务器文件是否存在
  3. lunixs 退出mysql_MySQL的基本操作
  4. H264 编码+打包+解码相关知识
  5. 晨哥真有料丨你太容易被得到了!
  6. 牛客题霸-SQL篇——10~20题
  7. 线路由器常见故障和排除方法
  8. oracle学习资料大全
  9. android登录ins,在android中集成instagram身份验证
  10. 坚定Freyja2的发展方向
  11. Quartus II 安装
  12. HTML5期末大作业:京东商城网站设计——京东商城购物网站(4页) 基于Html+Css+javascript的网页制作(购物主题)
  13. codewars练习(javascript)-2021/2/24
  14. Python机器学习英文资源
  15. 游戏辅助 -- 走路call中ecx值分析
  16. 哈工大c语言ex6答案,哈工大C语言程序设计习题ex6.doc
  17. 高考数学计算机题,高考数学题型全归纳汇总
  18. 【解决方案】LaTeX插入svg图片
  19. C# Environment.StackTrace、StackFrame、Tirm的使用技巧
  20. 奇偶校验,异或校验,和校验,nios串口校验

热门文章

  1. [知乎]关于WindowsXPx64SP2系统的说明
  2. 收藏!5款超级好用的小工具推荐,各个都是同类软件中的翘楚
  3. HTML中placeholder用法
  4. 《30岁之前的每一天》阅读笔记(一)
  5. 机器人总动员片尾曲歌词_机器人总动员中的所有歌曲叫什么名?
  6. [332]sql快速创建一张相同表结构的表
  7. 寻找指定长度以内的最短连续子段和
  8. 微信投票活动文明好人评选怎么刷票
  9. Visual Studio Code PlatformIo IDE 新建项目下载慢的解决办法
  10. CCNP路由学习技巧