jquery插件實(shí)現(xiàn)掃雷游戲(2)
本文實(shí)例為大家分享了jquery插件實(shí)現(xiàn)掃雷游戲的第2篇,供大家參考,具體內(nèi)容如下
完善了必要的
效果如下
代碼部分
* { margin: 0px; padding: 0px; font-size: 12px; } #div { position: fixed; top: 10px; bottom: 10px; left: 10px; right: 10px; border: 1px solid lightgray; border-radius: 5px; display: flex; justify-content: center; align-items: center; overflow: hidden; } #box { border: 1px solid lightgray; border-radius: 5px; } .row { white-space: nowrap; height: 30px; } .item { display: inline-flex; justify-content: center; align-items: center; height: 30px; width: 30px; border-right: 1px solid lightgray; border-bottom: 1px solid lightgray; cursor: pointer; position: relative; } .item.num1::after{ content: '1'; color: #1abc9c; position: absolute; top: 0; bottom: 0; left: 0; right:0; display: flex; justify-content: center; align-items: center; z-index: 2; } .item.num2::after{ content: '2'; color: #2ecc71; position: absolute; top: 0; bottom: 0; left: 0; right:0; display: flex; justify-content: center; align-items: center; z-index: 2; } .item.num3::after{ content: '3'; color: #3498db; position: absolute; top: 0; bottom: 0; left: 0; right:0; display: flex; justify-content: center; align-items: center; z-index: 2; } .item.num4::after{ content: '4'; color: #9b59b6; position: absolute; top: 0; bottom: 0; left: 0; right:0; display: flex; justify-content: center; align-items: center; z-index: 2; } .item.num5::after{ content: '5'; color: #f1c40f; position: absolute; top: 0; bottom: 0; left: 0; right:0; display: flex; justify-content: center; align-items: center; z-index: 2; } .item.num6::after{ content: '6'; color: #e67e22; position: absolute; top: 0; bottom: 0; left: 0; right:0; display: flex; justify-content: center; align-items: center; z-index: 2; } .item.num7::after{ content: '7'; color: #e74c3c; position: absolute; top: 0; bottom: 0; left: 0; right:0; display: flex; justify-content: center; align-items: center; z-index: 2; } .item.num8::after{ content: '8'; color: #34495e; position: absolute; top: 0; bottom: 0; left: 0; right:0; display: flex; justify-content: center; align-items: center; z-index: 2; } .item.boom{ background-image: url(../img/boom.png); background-size: 60% 60%; background-repeat: no-repeat; background-position: center center; } .item::before{ position: absolute; content: ''; top: 0.5px; left:0.5px; bottom: 0.5px; right: 0.5px; background-color: gray; z-index: 3; } .item.click::before{ content: none; } .item:hover{ outline: 1px solid #2c3e50; } #menu { border-bottom: 1px solid lightgray; position: absolute; top: 0; left: 0; right: 0; height: 30px; display: flex; background-color: white; z-index: 10; } .mitem{ flex: 1; display: flex; justify-content: center; align-items: center; } .sl{ border: none; border-bottom: 1px solid lightgray; outline: none; width: 60%; height: 80%; } .btn{ border: none; border: 1px solid lightgray; outline: none; width: 60%; height: 80%; background-color: transparent; cursor: pointer; } .mitem *:hover{ background-color: lightgray; } ```javascript $(document).ready(function() { var x = 10; //x軸 var y = 10; //y軸 var c = 10; //雷數(shù) var boom = []; //產(chǎn)生炸彈的坐標(biāo) var $menu = $("#menu"); var $box = $("#box"); //同步參數(shù) $("#x").change(function() { x = parseInt($(this).val()); console.log(x); }) $("#y").change(function() { y = parseInt($(this).val()); }) $("#c").change(function() { c = parseInt($(this).val()); }) $(document).on('click', '#box .item', function() { $(this).addClass("click"); }) $("#pro").click(function() { console.log(x,y,c) draw(); draw();//繪制 booms();//產(chǎn)生炸彈參數(shù) drawbooms();//繪制炸彈 drawnum();//遍歷所有塊,生成提示 }) draw();//繪制 booms();//產(chǎn)生炸彈參數(shù) drawbooms();//繪制炸彈 drawnum();//遍歷所有塊,生成提示 function draw() { //繪制圖片 $box.html(''); for (var a = 0; a < x; a++) { var $row = $("<div class='row'></div>"); for (var b = 0; b < y; b++) { var $item = $("<div class='item' data-x='"+a+"' data-y='"+b+"'></div>"); $item.appendTo($row); } $row.appendTo($box); } } function drawnum(){ for(var a = 0;a<x;a++){ for(var b = 0;b<y;b++){ var pos = {x:a,y:b}; //遍歷這個(gè)坐標(biāo)四周的情況,看看有多少個(gè)炸彈 var s = getscore(pos); if(s!=0&&!$dom(pos).hasClass('boom')){ $dom(pos).addClass('num'+s); } } } } function getscore(p){ var index = 0; var s1 = boom.find(n=>n.x==(p.x-1)&&n.y==(p.y-1)) var s2 = boom.find(n=>n.x==(p.x)&&n.y==(p.y-1)) var s3 = boom.find(n=>n.x==(p.x+1)&&n.y==(p.y-1)) var s4 = boom.find(n=>n.x==(p.x-1)&&n.y==(p.y+1)) var s5 = boom.find(n=>n.x==(p.x)&&n.y==(p.y+1)) var s6 = boom.find(n=>n.x==(p.x+1)&&n.y==(p.y+1)) var s7 = boom.find(n=>n.x==(p.x-1)&&n.y==(p.y)) var s8 = boom.find(n=>n.x==(p.x+1)&&n.y==(p.y)) if(s1){index++;} if(s2){index++;} if(s3){index++;} if(s4){index++;} if(s5){index++;} if(s6){index++;} if(s7){index++;} if(s8){index++;} return index; } function drawbooms(){ boom.forEach(item=>{ $dom(item).addClass('boom'); }) } function booms(){//隨機(jī)產(chǎn)生炸彈參數(shù) var arr = []; while(arr.length<c){ var pos = {x:Math.floor(Math.random()*x),y:Math.floor(Math.random()*y)}; var temp = arr.find(n=>n.x==pos.x&&n.y==pos.y); if(!temp){ arr.push(pos); } } boom = arr; } function $dom(pos){ return $("[data-x='"+pos.x+"'][data-y='"+pos.y+"']"); } })
**思路解釋**
- 因?yàn)橛螒虻膮?shù)都已經(jīng)數(shù)據(jù)化了,所以后續(xù)的功能做起來(lái)更簡(jiǎn)單了
- 然后找了個(gè)圖片來(lái)作為炸彈,接著通過(guò)偽類做了對(duì)應(yīng)數(shù)字的提示效果
- 關(guān)于數(shù)字的產(chǎn)生就是掃雷附近的東西有多少個(gè)類的統(tǒng)計(jì),我都已經(jīng)化成坐標(biāo)系了,那就把附近的坐標(biāo)系統(tǒng)一查一遍就行了
- 接著就是關(guān)聯(lián)的效果我看看怎么來(lái)做
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jquery+CSS3實(shí)現(xiàn)3D拖拽相冊(cè)效果
這篇文章主要為大家詳細(xì)介紹了jquery+CSS3實(shí)現(xiàn)3D拖拽相冊(cè)效果的具體代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07jquery批量設(shè)置屬性readonly和disabled的方法
批量設(shè)置屬性的方法有很多,在本文為大家介紹下使用jquery批量設(shè)置readonly和disabled屬性2014-01-01struts2+jquery+json實(shí)現(xiàn)異步加載數(shù)據(jù)(自寫)
異步加載數(shù)據(jù)利用struts2+jquery+json實(shí)現(xiàn),具體代碼如下,感興趣的各位可以參考下哈,希望對(duì)大家有所幫助2013-06-06jQuery基于圖層模仿五星星評(píng)價(jià)功能的方法
這篇文章主要介紹了jQuery基于圖層模仿五星星評(píng)價(jià)功能的方法,使用jQuery動(dòng)態(tài)修改元素背景色的方法實(shí)現(xiàn)星評(píng)功能,需要的朋友可以參考下2015-05-05WEB前端開(kāi)發(fā)都應(yīng)知道的jquery小技巧及jquery三個(gè)簡(jiǎn)寫
一個(gè)簡(jiǎn)單技巧的集合,幫你提升 jQuery 技能,下面腳本之家小編給大家收集整理了web前端開(kāi)發(fā)都應(yīng)知道的jquery小技巧,對(duì)jquery小技巧感興趣的朋友一起學(xué)習(xí)吧2015-11-11jQuery獲取頁(yè)面元素絕對(duì)與相對(duì)位置的方法
這篇文章主要介紹了jQuery獲取頁(yè)面元素絕對(duì)與相對(duì)位置的方法,涉及jQuery中offset、position等方法的使用技巧,需要的朋友可以參考下2015-06-06jQuery使用動(dòng)畫隊(duì)列自定義動(dòng)畫操作示例
這篇文章主要介紹了jQuery使用動(dòng)畫隊(duì)列自定義動(dòng)畫操作,涉及jQuery使用queue()方法和dequeue()方法進(jìn)行函數(shù)隊(duì)列操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-06-06