精彩专题推荐:建站之入门课 建站之必修课 建站之关键课 网站价值所在 流量提高专题 css+div 标准 个人网站打造全过程
返回建站学首页
导航:
建站首页 | 网站设计 | 网站开发 | 网站运营 | 网页软件 | 建站指南 | 搜索优化 | 图像处理 | 视频教程 | 书籍教程 | 建站专题
当前位置:首页>网页软件>FLASH教程>正文

Flash MX 编程深层次应用-网络连线游戏(7)


来源:站长站 时间:06-09-21 点击: 点击这里收藏本文
7.5 实时下棋(1)

    

7.5.1  实时下棋的子程序

让我们先想想两个人实时下棋需要哪些子程序:

初始化棋盘与棋子
清空棋盘子函数

走棋子程序

检查胜负子程序

1.初始化棋盘与棋子

首先让我们看看前面没有讲的(begin_play)那一帧的内容吧,这一帧实际上就是初始化棋盘与棋子。其程序代码如下:

//棋盘的初始位置

begin_x = 30;

begin_y = 40;

distance = 36;

chessman_number = 1;

//处理当前下棋位置闪烁功能

attachMovie("point", "obj_flash_point", 1000);

_root.obj_flash_point._visible = false;

//和棋、认输、悔棋窗口

attachMovie("windows", "DrawDefeatRepent", 2000);

_root.DrawDefeatRepent._visible = false;

_root.DrawDefeatRepent._x = 150;

_root.DrawDefeatRepent._y = 150;

win_defeat.duplicateMovieClip("obj_win_defeat", 10000);

win_defeat.removeMovieClip();

//表示结果是否已经出来

result = false;

//表示当时在哪个台玩游戏,0表示没有玩

_root.createEmptyMovieClip("chessboard", 1000);

// 产生一个背景方格图

for (i=1; i<=7; i++) {

    // 画模线

    chessboard.lineStyle(1);

    chessboard.moveTo(begin_x, begin_y+(i-1)*distance);

    chessboard.lineTo(begin_x+7*distance, begin_y+(i-1)*distance);

}

for (j=1; j<=8; j++) {

    // 画竖线

    chessboard.moveTo(begin_x+(j-1)*distance, begin_y);

    chessboard.lineTo(begin_x+(j-1)*distance, begin_y+6*distance);

}

for (i=0; i<=6; i++) {

    // 画箭头按钮

    name = "arrow_"+i;

    attachMovie("arrow", name, i+j);

    this[name]._x = begin_x+(i+0.5)*distance;

    this[name]._y = begin_y-distance/2;

    this[name].place = i;

}

//5×7的二维数据来存放棋盘,0表示无棋,1表示我走的棋,2表示对手走的棋

chess = [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]];

stop();

//上面代码完成了下棋前的初始化工作

2.清空棋盘函数

清空棋盘函数程序如下:

// 清空程序,在退出下棋时有用

function clear() {

    var i;

    // 清线段

    removeMovieClip(chessboard);

    for (i=0; i<=50; i++) {

             // 清棋子

             name = "chessman_"+i;

             removeMovieClip(name);

    }

    for (i=0; i<=6; i++) {

             // 清箭头

             name = "arrow_"+i;

             removeMovieClip(name);

    }

}

3.走棋子程序

走棋子程序如下:

function runchess(place, name) {

    var i = 5;

    _root.i_run = not _root.i_run;

    _root.obj_flash_point._visible = true;

    if (name == "my") {

             // 我走的棋就放我的棋子

             while (_root.chess[i][place] != 0 and i>=0) {

                      i--;

             }

             if (i>=0) {

                      // 说明棋子下面有子可动

                      _root.chess[i][place] = 1;

                      _root.last_x = i;

                      _root.last_y = place;

                      name = "chessman_"+_root.chessman_number;

                      attachMovie("chessman", name, 100+_root.chessman_number);

                      _root.obj_flash_point._x = this[name]._x=begin_x+(place+0.5)*distance;

                      _root.obj_flash_point._y = this[name]._y=begin_y+(i+0.5)*distance;

                      this[name].gotoAndStop(_root.my_logo);

                      _root.chessman_number++;

                      // 检查是否胜利

                      if (check_win(i, place, 1)) {

                //如果你胜利就跳到你胜利的显示帧

                                _root.obj_win_defeat.gotoAndPlay("you_win");

                                _root.result = true;

                                _root.replay = false;

                                _root.i_can_play = false;

                      } else if (_root.chessman_number>=41) {

                                // 说明已经无棋可走了,成和棋

                                _root.obj_win_defeat.gotoAndPlay("you_draw");

                                _root.result = true;

                                _root.replay = false;

                                _root.i_can_play = false;

                      }

             }

    }

    if (name == "your") {

             // 对方走的棋,就放对方的棋子

             while (_root.chess[i][place] != 0 and i>=0) {

                      i--;

             }

             if (i>=0) {

                      // 说明棋子下面有子可动

                      _root.chess[i][place] = 2;

                      _root.last_x = i;

                      _root.last_y = place;

                      name = "chessman_"+_root.chessman_number;

                      attachMovie("chessman", name, 100+_root.chessman_number);

                      _root.obj_flash_point._x = this[name]._x=begin_x+(place+0.5)*distance;

                      _root.obj_flash_point._y = this[name]._y=begin_y+(i+0.5)*distance;

                      this[name].gotoAndStop(_root.your_logo);

                      _root.chessman_number++;

                      // 检查是否胜利

                      if (check_win(i, place, 2)) {

                //如果对手胜利就等于你输了,跳到你输的显示帧

                                _root.obj_win_defeat.gotoAndPlay("you_defeat");

                                _root.result = true;

                                _root.replay = false;

                                _root.i_can_play = false;

                      } else if (_root.chessman_number>=41) {

                                // 说明已经无棋可走了,成和棋

                 _root.obj_win_defeat.gotoAndPlay("you_draw");

                               _root.result = true;

                                _root.replay = false;

                                _root.i_can_play = false;

                      }

             }

    }

}




  把此文章收藏到:          
相关教程
广而告之
文章搜索
  • Google JZxue.Com

关于我们 | 联系我们 | 友情链接 | 网站地图
Copyright © 2005 - 2006 建站学 All rights reserved.