惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
雷峰网
雷峰网
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Spread Privacy
Spread Privacy
H
Hacker News: Front Page
PCI Perspectives
PCI Perspectives
Webroot Blog
Webroot Blog
罗磊的独立博客
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
大猫的无限游戏
大猫的无限游戏
月光博客
月光博客
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
Last Week in AI
Last Week in AI
美团技术团队
Help Net Security
Help Net Security
The Hacker News
The Hacker News
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
J
Java Code Geeks
The Register - Security
The Register - Security
IT之家
IT之家
WordPress大学
WordPress大学
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
H
Help Net Security
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
NISL@THU
NISL@THU
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Scott Helme
Scott Helme
V
Vulnerabilities – Threatpost
B
Blog
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
T
The Exploit Database - CXSecurity.com
S
Security Affairs
小众软件
小众软件
Hacker News: Ask HN
Hacker News: Ask HN
Security Latest
Security Latest
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
W
WeLiveSecurity
A
Arctic Wolf
L
LINUX DO - 热门话题
Google DeepMind News
Google DeepMind News
M
MIT News - Artificial intelligence

博客园 - 里沃特

XR806开发板环境搭建记录 谈谈把网站迁移到阿里云的一些感想和其中遇到的一些问题 HTML5+JS 《五子飞》游戏实现(八)人机对战 HTML5+JS 《五子飞》游戏实现(七)游戏试玩 HTML5+JS 《五子飞》游戏实现(五)移动棋子 HTML5+JS 《五子飞》游戏实现(四)夹一个和挑一对 HTML5+JS 《五子飞》游戏实现(三)页面和棋盘棋子 HTML5+JS 《五子飞》游戏实现(二)路线分析和资源准备 HTML5+JS 《五子飞》游戏实现(一)规则 cocos2dx-2.2.1 免 Cygwin 环境搭建(Win8+VS2013+ADT Bundle+android-ndk-r9c) 深入理解 cocos2d-x 坐标系 在VS2010 下编译 cocos2d-x-2.1.4 FFmpeg 1.2 for Android 生成一个动态库 FFmpeg 1.2 for Android 编译动态库 Linq 批量更新数据 C# 统一对 try...catch 的调用,方便保存错误日志。 手机:由全触摸屏失效所想到的 愚人节奉献给大家的礼物,敬请收下 C# 让控件全屏显示(WinForm)
HTML5+JS 《五子飞》游戏实现(六)鼠标响应与多重选择
里沃特 · 2015-01-16 · via 博客园 - 里沃特

上一章我们提到了如果有多条线上的棋子可以被吃掉,那么游戏需要提示用户,让用户选择吃哪条线上的。另外因为是网页游戏,所以一定要实现鼠标单击棋子可以进行操作。

当鼠标移动棋子上面后,切换鼠标指针为手形,移开棋子后再切换回默认的状态:

el.mousemove(function (e) {
    var o = el.offset();
    var p = { x: e.clientX - o.left, y: e.clientY - o.top };
    el.css("cursor", "default");
    for (var i = 0; i < t.chesses.length; i++) {
        if (Canvas.inRegion([p.x, p.y], t.chesses[i].bounds.toArrayXY())) {
            el.css("cursor", "pointer");
            break;
        }
    }
});

同时,还要根据鼠标位置来判断当前是哪颗棋子,是选中棋子还是移动棋子。

如果只是选中棋子,只需要在点击棋子后,在棋子的外面画一个框用来区别其他棋子,表示是当前棋子:

var b = this.chesses[this.currentIndex].bounds;
Canvas.drawRect(this.panel, "rgba(255,0,0,0.4)", 2, b.x - 2, b.y - 2, b.x + b.w + 2, b.y + b.h + 2);

如果是移动棋子,还要区别只是单纯的移动棋子还是移动后可以吃对方的棋子。单纯的移动棋子也就只需要更新目标位置为当前棋子就行了。

if (t.currentPlayer == t.chesses[i].player && t.chesses[i].player != Player.None) {
    t.currentIndex = i;
}

要是可以吃掉对方的棋子,就需要把对方的棋子吃掉或有多条路线可以吃棋时提示用户选择吃哪条路线的棋子。吃了棋子后还要判断对方还可不可以继续走棋,如果不能继续走棋,那么还需要提示用户游戏结束,我方赢了。

if (t.currentPlayer == t.chesses[t.currentIndex].player && t.chesses[i].player == Player.None) {
    if (t.moveChess(i, t.currentIndex)) {
        t.currentIndex = i;
        if (!t.chessarray) {
            player = t.currentPlayer;
            t.currentPlayer = t.getAnotherPlayer(player);
            t.changePlayer();
            if (t.isGameOver(t.currentPlayer)) { t.winner = player; t.isover = true; }
        }
    }
}

判断游戏是否结束:

// 游戏结束
this.isGameOver = function (player) {
    var i, j, k, pos;
    // 检查是否有可移动的棋子
    for (i = 0; i < this.chesses.length; i++) {
        if (this.chesses[i].player == player) {
            for (j = 0; j < this.lines.length; j++) {
                pos = $.inArray(this.chesses[i].point.index, this.lines[j]);
                if (pos != -1) {
                    for (k = 0; k < pos - 1; k++) {
                        if (this.canMove(k, pos)) return false;
                    }
                    for (k = pos + 1; k < this.lines[j].length; k++) {
                        if (this.canMove(k, pos)) return false;
                    }
                }
            }
        }
    }
    return true;
};

有多条路线选择的时候,我们暂时这样处理:在每条路线的左边棋子左边写上数字1,2,3...,表示路线编号,这样用户只需要点击有编号旁边的棋子就可以选择吃哪条路线的棋子:

// 多重选择
if (this.chessarray) {
    // 遮挡层
    Canvas.drawFillRect(this.panel, "#000000", 1, 20, 20, cw - 20, cw - 20, "rgba(0,0,0,0.4)");
    // 多重棋子
    for (i = 0; i < this.chessarray.length; i++) {
        b = this.chessarray[i][0].bounds;
        Canvas.drawRect(this.panel, "rgba(255,255,0,0.4)", 2, b.x - 2, b.y - 2, b.x + b.w + 2, b.y + b.h + 2);
// 写上路线编号 Canvas.drawText(
this.panel, i + 1, b.x + b.w + 4, b.y + b.h + 2, "#FFFFFF"); } }

最后每次我方下完棋子后,还需要切换给对方下棋:

player = t.currentPlayer;
t.currentPlayer = t.getAnotherPlayer(player);
t.changePlayer();

好了,这一章就里沃特先分析到这里。

HTML5+JS 《五子飞》游戏实现(一)规则

HTML5+JS 《五子飞》游戏实现(二)路线分析和资源准备

HTML5+JS 《五子飞》游戏实现(三)页面和棋盘棋子

HTML5+JS 《五子飞》游戏实现(四)夹一个和挑一对

HTML5+JS 《五子飞》游戏实现(五)移动棋子

HTML5+JS 《五子飞》游戏实现(七)游戏试玩