史上最简单的推箱子(AS3.0版)

史上最简单的推箱子(AS3.0版)_第1张图片

我最熟悉的语言是

AS3.0,现在主要用C++,还想学学Python,因此,最近一段时间先用AS3.0实现了最简版的推箱子、贪吃蛇和俄罗斯方块,然后换Python实现,算是熟悉了一下Python的基本用法,最后用C++实现,后续会陆续发上来。

先上AS3.0版代码:

package {
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.geom.Point;
    public class sokoban extends MovieClip {
        private var boxWidth: Number = 20;
        public var levelArr: Array = new Array();
        private var levelNO: int = 0;
        private var manMC: man;
        private var manDirect: Point;
        private var wallContiner: MovieClip = new MovieClip();
        private var boxContiner: MovieClip = new MovieClip();
        private var manContiner: MovieClip = new MovieClip();
        public function sokoban() {
            this.addChild(wallContiner);
            this.addChild(boxContiner);
            this.addChild(manContiner);
            manDirect = new Point(0, 0);
            initLevel();
            drawLevel(levelNO);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyEvent);
        }

        private function initLevel() {
            levelArr[0] = [
                [1, 1, 1, 1, 0, 0, 0, 0],
                [1, 0, 0, 1, 1, 1, 1, 1],
                [1, 0, 2, 0, 0, 3, 0, 1],
                [1, 0, 3, 0, 0, 2, 4, 1],
                [1, 1, 1, 0, 0, 1, 1, 1],
                [0, 0, 1, 1, 1, 1, 0, 0]
            ];
            levelArr[1] = [
                [0, 0, 1, 1, 1, 1, 0, 0],
                [0, 0, 1, 0, 0, 1, 0, 0],
                [1, 1, 1, 0, 0, 1, 1, 1],
                [1, 0, 3, 5, 2, 4, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 1],
                [1, 1, 1, 1, 1, 1, 1, 1]
            ];
            levelArr[2] = [
                [1, 1, 1, 1, 1, 1, 1, 1],
                [1, 0, 0, 1, 0, 0, 0, 1],
                [1, 0, 0, 0, 0, 0, 0, 1],
                [1, 3, 3, 3, 1, 1, 1, 1],
                [1, 6, 2, 0, 2, 1, 0, 0],
                [1, 1, 1, 1, 1, 1, 0, 0]
            ];
        }
        //画地图
        //1-墙壁 2-目标 3-箱子 4-人 5-箱子在目标上 6-人在目标上
        private function drawLevel(level: int) {

            for (var i: int = 0; i < levelArr[levelNO].length; i++) {
                for (var j: int = 0; j < levelArr[levelNO][i].length; j++) {
                    switch (levelArr[levelNO][i][j]) {
                        case 1: // wall
                            var wallMC = new wall();
                            wallMC.x = j * boxWidth;
                            wallMC.y = i * boxWidth;
                            wallContiner.addChild(wallMC);
                            break;
                        case 2: //goal
                            var goalMC = new goal();
                            goalMC.x = j * boxWidth;
                            goalMC.y = i * boxWidth;
                            wallContiner.addChild(goalMC);
                            break;
                        case 3: //box
                            var boxMC: box = new box();
                            boxMC.gotoAndStop(1);
                            boxMC.curRow = i;
                            boxMC.curCol = j
                            boxMC.x = j * boxWidth;
                            boxMC.y = i * boxWidth;
                            boxMC.name = i + "_" + j;
                            trace(boxMC.name);
                            boxContiner.addChild(boxMC);
                            break;
                        case 4: //man
                            manMC = new man();
                            manMC.curRow = i;
                            manMC.curCol = j;
                            manMC.x = j * boxWidth;
                            manMC.y = i * boxWidth;
                            manContiner.addChild(manMC);
                            break;
                        case 5: //box on goal
                            var goalMC = new goal();
                            goalMC.x = j * boxWidth;
                            goalMC.y = i * boxWidth;
                            wallContiner.addChild(goalMC);
                            var boxMC: box = new box();
                            boxMC.gotoAndStop(2);
                            boxMC.curRow = i;
                            boxMC.curCol = j
                            boxMC.x = j * boxWidth;
                            boxMC.y = i * boxWidth;
                            boxMC.name = i + "_" + j;
                            trace(boxMC.name);
                            boxContiner.addChild(boxMC);
                            break;
                        case 6: //man on goal
                            var goalMC = new goal();
                            goalMC.x = j * boxWidth;
                            goalMC.y = i * boxWidth;
                            wallContiner.addChild(goalMC);
                            manMC = new man();
                            manMC.curRow = i;
                            manMC.curCol = j;
                            manMC.x = j * boxWidth;
                            manMC.y = i * boxWidth;
                            manContiner.addChild(manMC);
                            break;


                    }
                }
            }
        }
        
        //left=col-1 right=col+1 up=row-1 down=row+1
        private function onKeyEvent(e: KeyboardEvent) {
            switch (e.keyCode) {
                case 37: //left
                    canMove(levelNO, 0, -1);
                    break;
                case 38: //up
                    canMove(levelNO, -1, 0);
                    break;
                case 39: //right
                    canMove(levelNO, 0, 1);
                    break;
                case 40: //down
                    canMove(levelNO, 1, 0);
                    break;
            }
        }

        
        //如果将要移动到的位置可以通过则移动小人        
        //如果将要移动到的位置上有箱子,如果箱子下一位置可移动则先移动箱子再移动人
        public function canMove(level: uint, row: int, col: int) {
            if (isWalkable(manMC.curRow + row, manMC.curCol + col)) {
                moveMan(row, col);
            } else {
                if (isBox(manMC.curRow + row, manMC.curCol + col)) {
                    if (isWalkable(manMC.curRow + 2 * row, manMC.curCol + 2 * col)) {
                        var boxName: String = (manMC.curRow + row) + "_" + (manMC.curCol + col);
                        var currentBox: box = boxContiner.getChildByName(boxName) as box;
                        trace(boxName);                        
                        moveBox(currentBox,row, col);
                        moveMan(row, col);
                    }
                }
            }

        }
        
        //偶数可以通行,如果没东西或是目标则可以通行
        public function isWalkable(row: int, col: int): Boolean {
            return (levelArr[levelNO][row][col] % 2 == 0);
        }
        
        //移动人后当前位置标志加4(4表示人)
        //原来位置标志减4
        //更新人当前行列坐标和实际位置
        private function moveMan(row: int, col: int) {
            levelArr[levelNO][manMC.curRow + row][manMC.curCol + col] += 4;
            levelArr[levelNO][manMC.curRow][manMC.curCol] -= 4;
            manMC.curRow += row;
            manMC.curCol += col;
            manMC.x += col * boxWidth;
            manMC.y += row * boxWidth;
        }
        
        //如果该行列位置被箱子(3,5)所占
        public function isBox(row: int, col: int): Boolean {
            var num: int = levelArr[levelNO][row][col];
            if(num % 2 == 1 && num > 1){
                return true;
            }else{
                return false;
            }
        }
        
        //移动箱子后当前位置标志加3(3表示箱子)
        //原来位置标志减3
        //更新箱子当前行列坐标和实际位置
        //如果箱子在目标上则箱子变绿    
        //更新箱子名字
        public function moveBox(currentBox: *, row: int, col: int) {
            trace("***" + currentBox.name);
            levelArr[levelNO][currentBox.curRow + row][currentBox.curCol + col] += 3;
            levelArr[levelNO][currentBox.curRow][currentBox.curCol] -= 3;
            currentBox.curRow += row;
            currentBox.curCol += col;
            if (levelArr[levelNO][currentBox.curRow][currentBox.curCol] == 5) {
                currentBox.gotoAndStop(2);
            } else {
                currentBox.gotoAndStop(1);
            }
            currentBox.x += col * boxWidth;
            currentBox.y += row * boxWidth;
            currentBox.name = currentBox.curRow + "_" + currentBox.curCol;
        }

    }
}

你可能感兴趣的:(as3,flash,flash)