手机版
你好,游客 登录 注册
背景:
阅读新闻

使用HTML5的drag&drop做一个数独游戏

[日期:2012-03-19] 来源:Linux社区  作者:shyleoking [字体:大 中 小]
现在你可以开始玩啦,虽然颜色不怎么好看,但至少可以玩,对不,我们第一个html5的实用游戏。我后期的blog打算再做些类似新浪微博的“你画我猜”还有“接龙游戏”。

下面是完整的代码,你要懒的话,就直接copy吧

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.   
  6.     <script src="js/jquery-1.7.1.min.js" type="text/Javascript"></script>  
  7.   
  8.     <script type="text/javascript">  
  9.         $.log = function(msg) {  
  10.             console.log(msg);  
  11.         }  
  12.   
  13.         $(  
  14.        function() {  
  15.            [{ number: 1, bgcolor: "#C71585" }, { number: 2, bgcolor: "#800080" }, { number: 3, bgcolor: "#B8860B" },  
  16.            { number: 4, bgcolor: "rgb(0,0,128)" }, { number: 5, bgcolor: "rgb(30,144,255)" },  
  17.            { number: 6, bgcolor: "rgb(255,165,0)" },  
  18.             { number: 7, bgcolor: "hsl(0,75%,50%)" }, { number: 8, bgcolor: "hsl(30,50%,50%)" },  
  19.             { number: 9, bgcolor: "hsl(120,75%,38%)"}].forEach(  
  20.             function(key, index) {  
  21.                 //$.log(key);   
  22.                 var li = $("<li>").html(key.number).css("backgroundColor", key.bgcolor).attr("draggable", "true");  
  23.                 //$.log(li);   
  24.                 li[0].addEventListener("dragstart", function(e) {  
  25.                     e.dataTransfer.effectAllowed = "copyMove";  
  26.                     e.dataTransfer.setData("text/plain", this.innerHTML);  
  27.                     $.log(this.innerHTML);  
  28.                     [ ].forEach.call(document.querySelectorAll("#player .default"),  
  29.                 function(item) {  
  30.                     //$.log(item);   
  31.                     item.classList.remove("default");  
  32.                     item.classList.add("ation");  
  33.                 });  
  34.                 }, false);  
  35.   
  36.                 li[0].addEventListener("dragend", function() {  
  37.                     [ ].forEach.call(document.querySelectorAll("#player .ation"),  
  38.                 function(item) {  
  39.                     item.classList.remove("ation");  
  40.                     item.classList.add("default");  
  41.                 });  
  42.                 }, false);  
  43.   
  44.                 $("#numberBox").append(li);  
  45.             }  
  46.             );  
  47.        }  
  48.        );  
  49.   
  50.         $(  
  51.        function() {  
  52.            "500000300090500400004000700051037289302080604008052137035000900609000823080023006".split("").forEach(  
  53.            function(item, index) {  
  54.                $.log(item);  
  55.                var li = $("<li>")  
  56.                if (item != "0") {  
  57.                    li.addClass("fix");  
  58.                    li[0].innerHTML = item;  
  59.                }  
  60.                else {  
  61.                    li[0].classList.add("default");  
  62.                    li[0].addEventListener("dragenter",  
  63.                    function(e) {  
  64.                        $.log(e);  
  65.                    }, false);  
  66.   
  67.                    li[0].addEventListener("dragover",  
  68.                    function(e) {  
  69.                        if (e.preventDefault) {  
  70.                            e.preventDefault(); //不要执行与事件关联的默认动作   
  71.                        }  
  72.                        if (e.stopPropagation) {  
  73.                            e.stopPropagation(); //停止事件的传播   
  74.                        }  
  75.                        $.log(e);  
  76.                        return false;  
  77.                    }, false);  
  78.   
  79.                    li[0].addEventListener("dragleave",  
  80.                    function(e) {  
  81.                    }, false);  
  82.   
  83.                    li[0].addEventListener("drop",  
  84.                    function(e) {  
  85.                        if (e.preventDefault) {  
  86.                            e.preventDefault(); //不要执行与事件关联的默认动作   
  87.                        }  
  88.                        if (e.stopPropagation) {  
  89.                            e.stopPropagation(); //停止事件的传播   
  90.                        }  
  91.   
  92.                        var sendData = e.dataTransfer.getData("text/plain");  
  93.                        //获得#player>li矩阵数组   
  94.                        var matrix = Array.prototype.slice.call(document.querySelectorAll("#player>li"));   
  95.                        var currIndex = matrix.indexOf(this); //获得当前元素的位置   
  96.                        var rowIndex = currIndex - currIndex % 9; //行开始的位置   
  97.                        var colIndex = currIndex % 9//列开始的位置   
  98.                        for (var i = rowIndex; i < rowIndex + 9; i++) {  
  99.                            if (i != currIndex && matrix[i].innerHTML == sendData) {  
  100.                                alert("对不起行上有数据重复,请小心哦!亲");  
  101.                                return;  
  102.                            }  
  103.                        }  
  104.                        for (var i = colIndex; i < 81; i = i + 9) {  
  105.                            if (i != currIndex && matrix[i].innerHTML == sendData) {  
  106.                                alert("对不起列上有数据重复,请小心哦!亲");  
  107.                                return;  
  108.                            }  
  109.                        }  
  110.                        this.innerHTML = sendData;  
  111.   
  112.                    }, false);  
  113.                }  
  114.                $("#player").append(li);  
  115.            }  
  116.            );  
  117.        }  
  118.        );  
  119.          
  120.      
  121.      
  122.     </script>  
  123.   
  124.     <style type="text/css">  
  125.         #numberBox > li   
  126.         {  
  127.             width: 30px;  
  128.             height: 25px;  
  129.             text-align: center;  
  130.             font-size: 20px;  
  131.             padding-top: 5px;  
  132.             float: left;  
  133.             color: White;  
  134.         }  
  135.         #player .default   
  136.         {  
  137.             float: left;  
  138.             width: 48px;  
  139.             height: 33px;  
  140.             border: solid 1px rgb(0,0,0);  
  141.             font-size: 20px;  
  142.             padding-top: 15px;  
  143.             text-align: center;  
  144.             background-color: #B8860B;  
  145.         }  
  146.         #player .fix   
  147.         {  
  148.             float: left;  
  149.             width: 48px;  
  150.             height: 33px;  
  151.             border: solid 1px rgb(0,0,0);  
  152.             font-size: 20px;  
  153.             padding-top: 15px;  
  154.             text-align: center;  
  155.             background-color: #FFFABC;  
  156.         }  
  157.         #player .ation   
  158.         {  
  159.             float: left;  
  160.             width: 48px;  
  161.             height: 33px;  
  162.             border: solid 1px rgb(0,0,0);  
  163.             font-size: 20px;  
  164.             padding-top: 15px;  
  165.             text-align: center;  
  166.             background-color: #FFA07A;  
  167.         }  
  168.     </style>  
  169. </head>  
  170. <body>  
  171.     <ol id="numberBox" style="list-style-type: none; width: 90px; height: 90px;">  
  172.     </ol>  
  173.     <ol id="player" style="list-style-type: none; width: 450px; height: 450px;">  
  174.     </ol>  
  175. </body>  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款