牛逼的AlphaGo,怎样用DIV+CSS画棋盘

这两天柯洁对阵阿尔法狗,不得不说一句人工智能真TM的强大,我觉得把他电源拔了,我肯定能下过他

但是我只会下五子棋,不会下围棋啊

但是我也会画棋盘,你肯定会认为不就是画棋盘吗,给div一个border不就行了

非也非也,我们下过棋的都是知道的,棋子是要下在交叉线上的了

小学的时候用过田子格的那种本子吧

我们今天的目的就是要给实现这个效果,先上效果图帮助大家理解一下

相信大家已经看明白了,这就是在div容器里画两条相互垂直的线,这肯定是border不能办到的

那我们用什么呢

我在思考这个问题的时候也是想了很久,当时都想用canvas了

但是但是,我试了一下伪元素,没想到成功了

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要让棋子动起来,你需要使用JavaScript来添加交互性。以下是一些实现的步骤: 1. 为每个棋子创建一个JavaScript对象,其包含其位置、颜色和其他相关信息。 2. 使用JavaScript监听鼠标事件,例如mousedown和mouseup,以便在拖动棋子时更新其位置。 3. 当棋子移动时,使用CSS的transform属性来更新其位置。例如,你可以使用translateX和translateY来移动棋子。 4. 如果需要,可以使用CSS的transition属性来为棋子添加动效果。 下面是一个简单的示例代码,可以帮助你开始实现棋子的移动: ```html <!DOCTYPE html> <html> <head> <title>Chess Board</title> <style> .chessboard { display: flex; flex-wrap: wrap; width: 400px; height: 400px; margin: 0 auto; } .square { box-sizing: border-box; width: 50px; height: 50px; border: 1px solid black; background-color: #EFEFEF; } .piece { position: absolute; width: 50px; height: 50px; background-image: url("pieces.png"); background-size: 600px 200px; } </style> </head> <body> <div class="chessboard"> <div class="square"></div> <div class="square"></div> <!-- more squares... --> </div> <script> // Define chess pieces as JavaScript objects var pieces = [ { type: "rook", color: "white", position: "a1" }, { type: "knight", color: "white", position: "b1" }, // more pieces... ]; // Create HTML elements for each piece pieces.forEach(function(piece) { var element = document.createElement("div"); element.className = "piece " + piece.type + " " + piece.color; element.style.backgroundPosition = getBackgroundPosition(piece.type, piece.color); element.style.left = getLeftOffset(piece.position); element.style.top = getTopOffset(piece.position); document.body.appendChild(element); }); // Define event listeners for mouse events var draggedPiece = null; document.addEventListener("mousedown", function(event) { if (event.target.classList.contains("piece")) { draggedPiece = event.target; } }); document.addEventListener("mouseup", function(event) { if (draggedPiece) { draggedPiece.style.transform = "translate(" + event.clientX + "px, " + event.clientY + "px)"; draggedPiece = null; } }); // Helper functions to calculate background position and element offset function getBackgroundPosition(type, color) { var x = { "rook": 0, "knight": -50, // more positions... }[type]; var y = { "white": 0, "black": -50, }[color]; return x + "px " + y + "px"; } function getLeftOffset(position) { return (position.charCodeAt(0) - "a".charCodeAt(0)) * 50 + "px"; } function getTopOffset(position) { return (8 - parseInt(position.charAt(1))) * 50 + "px"; } </script> </body> </html> ``` 请注意,这只是一个简单的例子,你需要根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值