In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: forked from: forked from: Warp Smash (Pong variation)

Get Adobe Flash player
by dannnn 12 May 2013
    Embed
/**
 * Copyright dannnn ( http://wonderfl.net/user/dannnn )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/rr9x
 */

// forked from dannnn's forked from: forked from: Warp Smash (Pong variation)
// forked from hacker_sj1gxfcl's forked from: Warp Smash (Pong variation)
// forked from k0rin's Warp Smash (Pong variation)
package {
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.text.*;
    
    [SWF(width = "465", height = "465", frameRate = "50", backgroundColor="0x000000")]
    public class WarpSmash extends Sprite {
        private var isGameOver:Boolean;
        private var gameOverCount:int;
        // スコアを見ないまま再スタートしてしまうのを防止するウェイト
        private const GAME_OVER_WAIT:int = 50;
        
        private const SCREEN_W:int = 465;
        private const SCREEN_H:int = 465;
        
        private const PADDLE_OFFSET:Number = 25;
        private var PADDLE_XS:Array = [ PADDLE_OFFSET, SCREEN_W - PADDLE_OFFSET ];
        private var PADDLE_YS_2:Array = [ PADDLE_OFFSET, SCREEN_H - PADDLE_OFFSET ];

        private const PADDLE_HX:int = 7;
        private const PADDLE_HY:int = 50;
        private const PADDLE_HX_2:int = 50;
        private const PADDLE_HY_2:int = 7;
        
        // パドルの中央座標
        private var paddleX:Number = PADDLE_XS[side];
        private var paddleY:Number;
        private var paddleX_2:Number;
        private var paddleY_2:Number = PADDLE_YS_2[side_2];
        
        // パドル左右
        private var side:int = 8;
        // パドル上下
        private var side_2:int = 8;
        // テレポート中か?
        private var teleporting:Boolean;
        private var ctrl:Boolean;
        
        private var ballX:Number;
        private var ballY:Number;
        private var ballVX:Number;
        private var ballVY:Number;
        private var ballSpeed:Number;
        private const BALL_RADIUS:Number = 7;
        
        private var paddle:Shape = new Shape();
        private var paddle_2:Shape = new Shape();
        private var ball:Shape = new Shape();
        
        private var score:int;
        
        private var gameOverText:TextField;
        private var scoreText:TextField;
        private var instructionText:TextField;
        
        // モーションブラーが暗くて見えないので明るくする
        private var colorMatrixFilter:ColorMatrixFilter = new ColorMatrixFilter(
            [
                1, 0, 0, 0, 0,
                0, 1, 0, 3, 0,
                0, 0, 1, 1, 0,
                0, 0, 0, 1, 3
            ]
        );
        private var glowFilter:GlowFilter = new GlowFilter(0x00FF00, 1, 8, 8);
        private var paddleFilters:Array = [ new BlurFilter(0, 0, 2), colorMatrixFilter, glowFilter ];
        private var glowFilter_2:GlowFilter = new GlowFilter( 0x0000FF, 1, 8, 8);
        private var paddleFilters_2:Array = [ new BlurFilter(0, 0, 2), colorMatrixFilter, glowFilter_2 ];
        ////
        public var background:Sprite;
        ////
        public function WarpSmash() {
            ///
            background = new Sprite();
            background.graphics.beginFill(0x0,0.2);
            background.graphics.drawRect(0,0,465,465);
            background.graphics.endFill();
            addChild(background);
            ////
            function createText(x:Number, y:Number, fontSize:Number):TextField {
                var tf:TextField = new TextField();
                tf.defaultTextFormat = new TextFormat("Helvetica", fontSize, 0xFFFFFFFF);
                tf.selectable = false;
                tf.x = x;
                tf.y = y;
                return tf;
            }
            gameOverText = createText(225, 150, 23);
            gameOverText.text = "GAME OVER";
            gameOverText.autoSize = TextFieldAutoSize.CENTER;
            gameOverText.visible = isGameOver;
            addChild(gameOverText);
            
            scoreText = createText(252, 300, 160);
            scoreText.autoSize = TextFieldAutoSize.RIGHT;
            addChild(scoreText);
            
            instructionText = createText(278, 180, 23);
            instructionText.text = "CLICK and ctrl-key TO TELEPORT!";
            instructionText.autoSize = TextFieldAutoSize.RIGHT;
            addChild(instructionText);
            
            var g:Graphics = paddle.graphics;
            g.beginFill(0xFFFFFF);
            g.drawRoundRect( -PADDLE_HX, -PADDLE_HY, PADDLE_HX * 2, PADDLE_HY * 2, 4, 4);
            g.endFill();
            addChild(paddle);
            
            g = paddle_2.graphics;
            g.beginFill(0xFFFFFF);
            g.drawRoundRect( -PADDLE_HX_2, -PADDLE_HY_2, PADDLE_HX_2 * 2, PADDLE_HY_2 * 2, 4, 4);
            g.endFill();
            addChild(paddle_2);
            
            g = ball.graphics;
            g.beginFill(0xFFFFFF);
            g.drawCircle(0, 0, BALL_RADIUS);
            g.endFill();
            ball.filters = [ glowFilter ];
            addChild(ball);
            
            initGame();
            
            addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);      
        }
        
        public function initGame():void {
            ballSpeed = 5;
            score = 0;
            scoreText.text = "0";
            
            side = 0;
            paddleX = PADDLE_XS[side];
            side_2 = 0;
            paddleY_2 = PADDLE_YS_2[side_2];
            teleporting = false;
            ctrl = false;
            setPaddleBlur(0);
            setPaddleBlur_2(0);
            
            ballX = SCREEN_W;
            ballY = random(SCREEN_H * 0.1, SCREEN_H * 0.9);
            ballVX = -ballSpeed;
            ballVY = 0;
            
            isGameOver = false;
        }
        
        private function setPaddleBlur(blur:Number):void {
            paddleFilters[0].blurX = blur;
            paddle.filters = paddleFilters;
        }
        private function setPaddleBlurY(blur:Number):void 
        {
            paddleFilters[0].blurY = blur;
            paddle.filters = paddleFilters;
        }
        
        private function setPaddleBlur_2(blur:Number):void {
            paddleFilters_2[0].blurX = blur;
            paddle_2.filters = paddleFilters_2;
        }
        
        private function setPaddleBlurY_2(blur:Number):void 
        {
            paddleFilters_2[0].blurY = blur;
            paddle_2.filters = paddleFilters_2;
        }
        
        private function enterFrameHandler(event:Event):void {
            if (teleporting) {
                var d:Number = (PADDLE_XS[side] - paddleX) * 0.2;
                d += sign(d) * 4;
                if (Math.abs(PADDLE_XS[side] - paddleX) < d) {
                    paddleX = PADDLE_XS[side];
                    setPaddleBlur(0);
                    teleporting = false;
                } else {
                    paddleX += d;
                    setPaddleBlur(Math.abs(d * 0.65));
                }
            }
            if (ctrl) {    // ctrlkey downが行われた
                var d_2:Number = (PADDLE_YS_2[side_2] - paddleY_2) * 0.2;
                d_2 += sign(d_2) * 4;
                if (Math.abs(PADDLE_YS_2[side_2] - paddleY_2) < d_2) {
                    paddleY_2 = PADDLE_YS_2[side_2];
                    setPaddleBlurY_2(0);
                    ctrl = false;
                } else {
                    paddleY_2 += d_2;
                    setPaddleBlurY_2(Math.abs(d_2 * 0.65));
                }
            }
            paddleY = clamp(mouseY, PADDLE_HY, SCREEN_H - PADDLE_HY);
            paddleX_2 = clamp(mouseX,PADDLE_HX_2,SCREEN_W - PADDLE_HX_2);
            //paddleY = clamp(ballY, PADDLE_HY, SCREEN_H - PADDLE_HY);
            //paddleX_2 = clamp(ballX, PADDLE_HX_2, SCREEN_W - PADDLE_HX_2);
                     
            /*VX:right, -VX:left
             * side0:left, side1:right
             */
            //paddleX
            /*
            if (ballVX < 0) {
                    if (side == 0) {
                            side = 0;
                    }
                    if (side == 1) {
                            side = 0;
                            teleporting = true;
                    }
            }
                
            if (ballVX > 0) {
                    if (side == 0) {
                            side = 1;
                            teleporting = true;
                    }
                    if (side == 1) {
                            side = 1;
                    }
            }
            */
            //paddleY
            /*
            if (ballVY < 0) {
                    if (side_2 == 0) {
                            side_2 = 0;
                    }
                    if (side_2 == 1) {
                            side_2 = 0;
                            ctrl = true;
                    }
            }
                
            if (ballVY > 0) {
                    if (side_2 == 0) {
                            side_2 = 1;
                            ctrl = true;
                    }
                    if (side_2 == 1) {
                            side_2 = 1;
                    }
            }
            */

            if (!isGameOver) {
                if (!teleporting) {
                    if ((ballVX < 0 && side == 0 && ballX + ballVX < PADDLE_XS[0] + PADDLE_HX) || 
                        (ballVX > 0 && side == 1 && ballX + ballVX > PADDLE_XS[1] - PADDLE_HX)) {
                        if (Math.abs(ballY - paddleY) > PADDLE_HY + BALL_RADIUS * 0.8) {
                            isGameOver = true;
                            gameOverCount = 0;
                        } else {
                            ballSpeed = clamp(ballSpeed + 0.2, 0, 15);
                            
                            ballVX = -sign(ballVX) * ballSpeed;
                            ballVY += random( -ballSpeed, ballSpeed);
                            ballVY = clamp(ballVY, -ballSpeed, ballSpeed);
                            
                            score = clamp(score + 1, 0, 9999);
                            scoreText.text = String(score);
                        }
                    }
                }
                if (!ctrl) {
                    if ((ballVY < 0 && side_2 == 0 && ballY + ballVY < PADDLE_YS_2[0] + PADDLE_HY_2) ||
                        (ballVY > 0 && side_2 == 1 && ballY + ballVY > PADDLE_YS_2[1] - PADDLE_HY_2)) {
                            
                        if(Math.abs(ballX - paddleX_2) > PADDLE_HX_2 + BALL_RADIUS * 0.8) {    // パドルの長さの限界があった
                            isGameOver = true;
                            gameOverCount = 0;
                        } else {    // ゲームオーバーで無い、プレイ中の場合
                            ballSpeed = clamp(ballSpeed + 0.2, 0, 15);
                            
                            ballVY = -sign(ballVY) * ballSpeed;
                            ballVX += random( -ballSpeed, ballSpeed);
                            ballVX = clamp(ballVX, -ballSpeed, ballSpeed);
                            
                            score = clamp(score + 1, 0, 9999);
                            scoreText.text = String(score);
                        }
                    }
                }
                if (!inRange(ballX, -BALL_RADIUS, SCREEN_W + BALL_RADIUS)
                ||!inRange(ballY, -BALL_RADIUS, SCREEN_H + BALL_RADIUS)) {
                    isGameOver = true;
                    gameOverCount = 0;
                }
            } else {
                gameOverCount++;
            }
            //if (!inRange(ballY + ballVY, BALL_RADIUS, SCREEN_H - BALL_RADIUS)) {
            //    ballVY = -ballVY;
            //}
            ballX += ballVX;
            ballY += ballVY;
            
            paddle.x = paddleX;
            paddle.y = paddleY;
            paddle_2.x = paddleX_2;
            paddle_2.y = paddleY_2;
            
            ball.x = ballX;
            ball.y = ballY;
            
            gameOverText.visible = isGameOver && (gameOverCount > GAME_OVER_WAIT);
        }
        
        private function mouseDownHandler(MouseEvent:Event):void {
            side = 1 - side;
            teleporting = true;
            
            if (gameOverText.visible) {
                initGame();
            }
            instructionText.visible = false;
        }
        
        private function keyDownHandler(event:KeyboardEvent):void
        {
            side_2 = 1 - side_2;
            ctrl = event.ctrlKey;
            
            if (gameOverText.visible) {
                initGame();
            }
            instructionText.visible = false;
        }
        
        private function random(n:Number, m:Number):Number {
            return n + Math.random() * (m - n);
        }
        private function sign(n:Number):Number {
            if (n > 0) { return 1; }
            if (n < 0) { return -1; }
            return 0;
        }
        private function clamp(n:Number, min:Number, max:Number):Number {
            if (n < min) { n = min; }
            if (n > max) { n = max; }
            return n;
        }
        private function inRange(n:Number, min:Number, max:Number):Boolean {
            return (n >= min && n <= max);
        }
    }
}