|
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; } } } }
|