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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
V
V2EX
G
Google Developers Blog
F
Full Disclosure
Martin Fowler
Martin Fowler
宝玉的分享
宝玉的分享
H
Hacker News: Front Page
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
NISL@THU
NISL@THU
G
GRAHAM CLULEY
V
Vulnerabilities – Threatpost
Hacker News - Newest:
Hacker News - Newest: "LLM"
A
About on SuperTechFans
The Cloudflare Blog
C
Cisco Blogs
D
DataBreaches.Net
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Vercel News
Vercel News
P
Privacy International News Feed
Microsoft Security Blog
Microsoft Security Blog
Help Net Security
Help Net Security
Recorded Future
Recorded Future
PCI Perspectives
PCI Perspectives
S
Schneier on Security
AI
AI
N
News | PayPal Newsroom
雷峰网
雷峰网
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
The Last Watchdog
The Last Watchdog
L
LINUX DO - 最新话题
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
S
Securelist
云风的 BLOG
云风的 BLOG
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
AWS News Blog
AWS News Blog
TaoSecurity Blog
TaoSecurity Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 三生石上(FineUI控件)
C
CXSECURITY Database RSS Feed - CXSecurity.com
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Cloudbric
Cloudbric
C
Cybersecurity and Infrastructure Security Agency CISA
Project Zero
Project Zero
C
Check Point Blog
S
Security Affairs

博客园 - 君之蘭

WPF打印票据 easyui plugin——etreegrid:CRUD Treegrid 【经验谈】XmlSerializer的坑 从抽象谈起(三):AOP编程和ASP.NET MVC 从抽象谈起(二):观察者模式与回调 从抽象谈起(一):工厂模式与策略模式 SQL SERVER BI 入门:(2) Analysis Service 应用 SQL SERVER BI 入门:(1)安装与基础概念 HTML5 Canvas编写五彩连珠(5):寻路 HTML5 Canvas编写五彩连珠(4):动画 HTML5 Canvas编写五彩连珠(3):设计 HTML5 Canvas编写五彩连珠(2):画图 HTML5 Canvas编写五彩连珠(1):预览 Trie树-脏词过滤应用 带你走进缓存世界(6):共享缓存 ASP.NET MVC3 Custom FormAuthorize ASP.NET MVC3 Custom ErrorPages 500/404 带你走进缓存世界(5):一显身手 带你走进缓存世界(4):缓存之缓
HTML5 Canvas编写五彩连珠(6):试玩
君之蘭 · 2012-03-19 · via 博客园 - 君之蘭

  上节中讲了如何寻路,在和朋友们讨论时都反应有时走的不太对,绕远路了,其实代码主要是大方向的判断  比如目标在右上,那应该是先右还是先上 这个并没有做处理,如果这个做了处理,效果会更好一些,但也难免会走弯路。  贪心就是这样,不是最优,接近最优。也希望其他的同学有意见的可以讨论下。我这也只是个人想法。

  既然可以走动了,那就可以判断是否可以消除同样颜色的行、列或斜线了。只要>=5个同样的色球,就清除他们,并且可以继续移动。如果不可以清除,那就再增加3个球。 

            clearLine: function (x1, y1, color, isClick) {
                if (this.isEmpty(x1, y1)) {
                    if (isClick) game.ready.flyin();
                    return;
                };
                //给定一个坐标,看是否有满足的line可以被消除
                //4根线 一  | / \
                
                var current = this.getBubble(x1, y1);
                if (!current.color) {
                    console.log(current);
                }
                var arr1, arr2, arr3, arr4;
                arr1 = this.bubbles[y1];//横线很简单,就是数组的一项,现成的

                arr2 = [];
                for (var y = 0; y < game.cellCount; y++)
                    arr2.push(this.getBubble(x1, y));//竖线就是一列。

                arr3 = [current];
                arr4 = [current];
                for (var i = 1; i < game.cellCount ; i++) {
                    if (x1 - i >= 0 && y1 - i >= 0)//\斜线的上半部分...
                        arr3.unshift(this.getBubble(x1 - i, y1 - i));
                    if (x1 + i < game.cellCount && y1 + i < game.cellCount)//\斜线的下半部分
                        arr3.push(this.getBubble(x1 + i, y1 + i));

                    if (x1 - i >= 0 && y1 + i < game.cellCount)// /斜线的下半部分
                        arr4.push(this.getBubble(x1 - i, y1 + i));
                    if (x1 + i < game.cellCount && y1 - i >= 0)// /斜线的上班部分
                        arr4.unshift(this.getBubble(x1 + i, y1 - i));
                }

                var line1 = getLine(arr1);
                var line2 = getLine(arr2);
                var line3 = getLine(arr3);
                var line4 = getLine(arr4);

                var line = line1.concat(line2).concat(line3).concat(line4);
                if (line.length < 5) {
                    if (isClick) game.ready.flyin();
                    return;
                }
                else {
                    var me = this;
                    var i = 0;

                    game.play("clearline", function () {
                        if (i == line.length) {
                            game.score.addScore(line.length);
                            game.stop("clearline");
                            me.isMoving = false;
                            //game.ready.flyin();
                            return;
                        }
                        me.isMoving = true;
                        var p = line[i];
                        me.setBubble(p.x, p.y, null);
                        i++;
                    }, 100);
                }


                function getLine(bubbles) {

                    var line = [];
                    for (var i = 0; i < bubbles.length; i++) {
                        var b = bubbles[i];
                        if (b.color == color) {
                            line.push({ "x": b.x, "y": b.y });
                        }
                        else {
                            if (line.length < 5)
                                line = [];
                            else
                                return line;
                        }
                    }
                    if (line.length < 5)
                        return [];
                    return line;
                }
            },

大家可以看看代码,主要有两点 1、或许需要处理的 横竖斜线 4个数组,然后看这4个数组是否有连续的。判断是否连续只需要一个状态数组来维持就可以了。
由于考虑到同时消除多行的情况,所以要把连续的line 连接在一起,并在结算得分时考虑奖励。 
isClick参数主要用于判断是否是用户点击进行消行的还是新泡泡飞入产生的。  如果达不到消行条件并且是飞入的,那就不能再调用飞入了,否则死循环了。

满足的消行代码也比较简单,就是播放一个消行动画,其实这个动画真心没劲,要想设计好些可以做个渐变偏移消除。  现在我发现起初没有设计一个数组来维护状态有些不太明智,因为还有奖励的星星和炸弹要绘制,挺麻烦的。。 那么,把积分显示出来吧。 这样就可以先试玩起来了:)

        game.score = {
            basic: 0,
            operate: 0,
            star1: 0,
            star2: 0,
            boom: 0,
            draw: function () {
                var startX = game.cellWidth * 10 + game.map.startX;
                var startY = game.map.startY;
                var ctx = game.ctx;
                ctx.save();
                ctx.translate(startX, startY);
                ctx.clearRect(0, 0, 150, 400);
                ctx.strokeStyle = "#456";
                //ctx.strokeRect(0, 0, 150, 200);
                ctx.font = "24px 微软雅黑";
                ctx.fillStyle = "#fefefe";
                ctx.fillText("score:" + (this.basic * 5 + this.star1 * 8 + this.star2 * 10 + this.boom * 20), 0, 30);
                ctx.stroke();
                ctx.restore();
            },
            addScore: function (length) {
                switch (length) {
                    case 5:
                        this.basic++;
                        break;
                    case 6:
                        this.star1++;
                        break;
                    case 7:
                        this.star2++;
                        break;
                    default:
                        this.boom++;
                        break;
                }
                this.draw();
                console.log(this.score);
            },
        };

第三节在设计里谈到了 百搭星和炸弹,我太懒了,不打算讲了,因为这些处理也没有什么复杂的,HTML5 Canvas的的基础学习也算可以了。后面应该学一些更深入的东西,比如如何处理性能、物体碰撞、运动学神马的。。。