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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 里沃特

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 《五子飞》游戏实现(七)游戏试玩