庞大资源库的计算机教程网站!
设为首页
加入收藏
总编信箱
投稿或申请专栏请先 [登 陆]
首页 操作系统 程序设计 图形图像 媒体动画 机械电子 WEB开发 数 据 库 办公系列 路由技术 网络原理 网络应用
认证考试 安全技术
首页>安全技术>软件破解>算法研究>正文
资料搜索
Google搜索
Google
返回上级列表

推荐文章

快速保存网页中所有图片的方法
Windows中让光驱巧妙“隐身”技
防范非法用户入侵Win 2000/XP系
两款比较典型的ASP木马防范方法
有关表格边框的css语法整理
Windows XP中可以被禁用的服务
SQL Server导出导入数据方法
Javascript所有对象的属性的获
网页(HTML)中的特殊字符
与篮球共舞,尽显模式本色
QQ病毒的手工清除方法
Photoshop为极品美女打造性感睫
天衣无缝:IIS与PHP水火也相容
SQL Server存储过程编写和优化

贪吃蛇的算法分析(2)

 作者:本站收集   日期:2005-5-28
字号选择〖 〗/ 双击滚屏 单击停止   
下面重点介绍下Worm类中的几个方法:

l         public void setDirection(byte direction)

这个方法用来改变贪吃蛇运动的方向,只能90度。看下面的实现代码:

if ((direction != currentDirection) && !needUpdate) {

        // 取出列表中的最后一个元素(蛇的头部)

        WormLink sl = (WormLink)worm.lastElement();

        int x = sl.getEndX();

        int y = sl.getEndY();

         // 不同的运动方向坐标的改变也不一样

        switch (direction) {

        case UP: // 当这段向上运动的时候

            if (currentDirection != DOWN) {

            y--; needUpdate = true;         }

            break;

        case DOWN: // 当这段向下运动的时候

            if (currentDirection != UP) {

            y++; needUpdate = true;         }

            break;

        case LEFT: // 当这段向左运动的时候

            if (currentDirection != RIGHT) {

            x--; needUpdate = true;         }

            break;

        case RIGHT: // 当这段向右运动的时候

            if (currentDirection != LEFT)  {

            x++; needUpdate = true;         }

            break;       }

        // 当更改方向后需要更新

        if (needUpdate == true) {

            worm.addElement(new WormLink(x, y, 0, direction));

            currentDirection = direction;        }       }

 

l         public void update(Graphics g)

这个函数是更新贪吃蛇状态。每次更新都把头部增加一节,尾部减少一节。如果它吃到食物尾部段就不减少一节。看起来就像整只蛇长了一节。

 

        // 把贪吃蛇头部增加一格

        head = (WormLink)worm.lastElement();

        head.increaseLength();

        // 如果没有吃到食物则尾部减少一格

        if (!hasEaten) {

           WormLink tail;

           tail = (WormLink)worm.firstElement();

           int tailX = tail.getX();

           int tailY = tail.getY();

           // 如果尾部块长度为0就删除

           tail.decreaseLength();

           if (tail.getLength() == 0) {

               worm.removeElement(tail);        }

           // 尾部减少一格

           g.setColor(WormPit.ERASE_COLOUR);

           drawLink(g, tailX, tailY, tailX, tailY, 1);

           } else {        

           // 如果吃到食物就不删除尾部

           hasEaten = false;        }

        needUpdate = false;

        // 确认是否在边界中

        if (!WormPit.isInBounds(head.getEndX(), head.getEndY())) {

           // 如果不在,就死了

           throw new WormException("over the edge");         }

        headX = (byte)head.getEndX();

        headY = (byte)head.getEndY();

        //贪吃蛇的头部增加一格

        g.setColor(WormPit.DRAW_COLOUR);

        drawLink(g, headX, headY, headX, headY, 1);

        // 判断是否吃到自己

        for (int i = 0; i < worm.size()-1; i++) {

        sl = (WormLink)worm.elementAt(i);

        if (sl.contains(headX, headY)) {

            throw new WormException("you ate yourself");     }       }

 

l         void drawLink(Graphics g, int x1, int y1, int x2, int y2, int len)

这个函数用来画蛇的一段,一只完整的蛇是一段一段组成的。

// 把长度转换成像素长度

    len *= WormPit.CELL_SIZE;

    // (x1 == x2)说明这一段是垂直的

    if (x1 == x2) {

        // 把x1转成像素长度

        x1 *= WormPit.CELL_SIZE;

        // (y2 < y1)说明是向上运动

        if (y2 < y1) {

// 就把头、尾左边交换并转成像素

        y1 = y2 * WormPit.CELL_SIZE;

        } else {    

        // 把y1转成像素

        y1 *= WormPit.CELL_SIZE;        }

        g.fillRect(x1, y1, WormPit.CELL_SIZE, len);

    } else {

        // 这是水平的一段

        y1 *= WormPit.CELL_SIZE;

        if (x2 < x1) {

        // 就把头、尾左边交换并转成像素

        x1 = x2 * WormPit.CELL_SIZE;

        } else {

        x1 *= WormPit.CELL_SIZE;        }

        g.fillRect(x1, y1, len, WormPit.CELL_SIZE);  }

 

l         public void paint(Graphics g)

画出一只完整的贪吃蛇

WormLink sl;

    int x1, x2, y1, y2;

    int len;

    for (int i = 0; i < worm.size(); i++) {

        // 取出每一段,然后画出这一段,连起来就是一只完整的蛇

        sl = (WormLink)worm.elementAt(i);

        x1 = sl.getX();    x2 = sl.getEndX();

        y1 = sl.getY();    y2 = sl.getEndY();

        len = sl.getLength();

        drawLink(g, x1, y1, x2, y2, len);    }

上一篇:贪吃蛇的算法分析(1)    下一篇:贪吃蛇的算法分析(3)  
[发送给好友]  [关闭窗口]  [返回顶部]   转载请注明来源:www.it00.com   
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
责任编辑: 原点 投稿作者: 本站收集
信息来源: 网络 录入时间: 2005-5-28
关于我们 - 广告服务 - 版权申明 - 网站地图 - 联系方式 - 总编信箱 - 会员投稿