SinWave

by rso
how to play
* Mouse_Down : Accelerated upward
* Mouse_Up : Accelerated downward 
♥2 | Line 203 | Modified 2009-09-20 18:42:31 | MIT License
play

ActionScript3 source code

/**
 * Copyright rso ( http://wonderfl.net/user/rso )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/o9uj
 */

/**
 * how to play
 * Mouse_Down : Accelerated upward
 * Mouse_Up : Accelerated downward 
 */

package {
    import flash.display.Sprite;
    [SWF(width="500", height="500", backgroundColor="0x000000", frameRate="50")]
    public class SinWave extends Sprite { public function SinWave() { main = this; initialize(); } }
}
import flash.display.*;
import flash.geom.*;
import flash.text.*;
import flash.events.*;


const PLAYER_LOC : int = 20
const SCREEN_WIDTH:int = 500, SCREEN_HEIGHT : int = 500;
const PLAYER_POSX : int = SCREEN_WIDTH / WALL_NUM * PLAYER_LOC;
const ARRAY_LENGTH : int = 25;//SCREEN_WIDTH / WALL_NUM * PLAYER_LOC / 2;
const WALL_NUM : int = 100;
const DIRCHANGECNT : int = 100;
var main : Sprite;
var screen : BitmapData = new BitmapData(SCREEN_WIDTH, SCREEN_HEIGHT, false, 0);

var isMouseClicked : Boolean = false;
var scoreField : TextField;
var testField : TextField;
var messageField : TextField;
var bestScoreField : TextField;

const ACC : Number = 0.5; 
var loc : int;
var vec : Number;
var locHistory : Array;
var wall : Array;
var spaceHeight : int;
var beforeSpaceHeight : int = spaceHeight;
var wallHeight : int = 100;
var wallDiff : int = 1;
var score : int = 0;
var bestScore : int = 0;
var life : int;    

var upDirection : Boolean = true;
var dirChangeCnt : int = DIRCHANGECNT;
var rand:Function = Math.random;

var isGameOver : Boolean = false;
var isHit : Boolean = false;
var gameOverWaitTick : int = 200;

function initialize() : void {
    main.addChild(new Bitmap(screen));
    main.stage.addEventListener(MouseEvent.MOUSE_DOWN, function(e:Event) : void {isMouseClicked = true;})
    main.stage.addEventListener(MouseEvent.MOUSE_UP, function(e:Event) : void{isMouseClicked = false;})    
    main.stage.addEventListener(MouseEvent.CLICK, click);
    scoreField = createTextField(SCREEN_WIDTH - 100, 0, 100, 32, 0xFF6666,TextFormatAlign.RIGHT);
    testField = createTextField(0, 0, 100, 24, 0x8888FF)
    messageField = createTextField(SCREEN_WIDTH - 300, 0, 200, 36, 0xff6666);
    bestScoreField = createTextField(SCREEN_WIDTH - 100, SCREEN_HEIGHT - 16, 100, 16, 0xFF6666,TextFormatAlign.RIGHT);
     
    testField.text = life.toString();
    main.addChild(scoreField);
    main.addChild(testField);
    main.addChild(messageField);
    main.addChild(bestScoreField);
    main.addEventListener(Event.ENTER_FRAME, update);
    gameInit();
}

function click(e:Event) : void{
    if(isGameOver && gameOverWaitTick <= 0){
        gameInit();    
    }
}

function gameInit() : void{
    locHistory = new Array(21);
    wall = new Array(WALL_NUM + 1);
    score = 0;
    wallDiff = 1;
    loc = 200;
    vec = -5;
    beforeSpaceHeight = spaceHeight = 300;
    gameOverWaitTick = 100;
    isGameOver = false;
    life = 10;
}

function update(event:Event): void{
    if(isGameOver){
        if(gameOverWaitTick > 0)
            gameOverWaitTick--;
        return;    
    }
    screen.lock();
    screen.fillRect(screen.rect, 0x333333)
    Player.update();
    loc += vec;
 
    if(loc < 0){
        loc = 0;
    }else if(loc > SCREEN_HEIGHT){
        loc = SCREEN_HEIGHT;
    }
    
    locHistory.push(loc);
    
    if(upDirection == true){
        wallHeight -= wallDiff;    
    }else{
        wallHeight += wallDiff;    
    }
    wall.push(wallHeight);
    
    if(locHistory.length > ARRAY_LENGTH){
        locHistory.shift();
    }
    if(wall.length > WALL_NUM){
        wall.shift();    
    }
    
 
    
    if(wallHeight < 10){
        upDirection = false;    
    }else if(wallHeight > SCREEN_HEIGHT - spaceHeight - 10){ 
        upDirection = true;    
    }
    
    if(isMouseClicked){
       vec -= (ACC + vec/100);     
    }else{
       vec += (ACC - vec/100);        
    }    
    
    checkCollision();
    score++;
    scoreField.text = String(score);
    testField.text = life.toString();
    
    screen.unlock();
    
    
    dirChangeCnt--;
    if(dirChangeCnt == 0){
        dirChangeCnt = DIRCHANGECNT;
        wallDiff = 1 + rand() * (score / 500);
        upDirection = dirChange();
        beforeSpaceHeight = spaceHeight;
        spaceHeight = changeSpaceHeight();
    }
    
    
    
}

    function changeSpaceHeight() : int {
        if(score < 2000){
            return 300 - (score / 20);    
        }else{
            return 200 - (score - 2000) / 40;    
        }    
    }

    function checkCollision() : void{
        if(wall.length > PLAYER_LOC){
            if(loc < wall[PLAYER_LOC] || loc + life/2 > wall[PLAYER_LOC] + spaceHeight){
                if(life > 0){
                    life--;
                    isHit = true;    
                }else{
                    messageField.text = "GameOver"; 
                    isGameOver = true;
                    if(score > bestScore){
                        bestScore = score;
                        bestScoreField.text = "Best:"+bestScore.toString();    
                    }    
                }
                   
            }else{       
                messageField.text = "";         
                isHit = false;
            } 
        }
    }
    
    function dirChange() : Boolean{
        if(rand() > 0.5){
            return true;    
        }    
        return false
    }

// Player.
class Player {
    public static var pos:Vector3D = new Vector3D, prevPos:Vector3D = new Vector3D;
    public static function update():void {
        prevPos.x = pos.x; prevPos.y = pos.y;
        pos.x = main.stage.mouseX; pos.y = main.stage.mouseY;
        draw();
        var cx:Number = pos.x, cy:Number = pos.y, ox:Number = prevPos.x - pos.x, oy:Number = prevPos.y - pos.y;
        ox /= 9; oy /= 9;
        //for (var i:int = 0; i < 10; i++, cx += ox, cy += oy)
            //for each (var w:Wall in walls) if (w.rect.contains(cx, cy)) {
                //add Collision event
            //}
    }
    /*
    public static function draw2():void {
        var rect : Rectangle = new Rectangle;
 //       testField.text = pos.x.toString() + "," + pos.y.toString();
    
        for(var i:int = locHistory.length; i > 0;i--){
            rect.x = 100; rect.y = loc - 5; rect.width = rect.height = 8;
            screen.fillRect(rect, 0xffffff);
        }
     }
     */

    public static function draw():void {
        var rect : Rectangle = new Rectangle;
       var posX : int = PLAYER_POSX;
        for(var i:int = locHistory.length - 1; i > 0;i--){
            rect.x = posX ; rect.y = locHistory[i]; rect.width = rect.height = life/2 + 5;
            var colorG : int = 0xff / (locHistory.length - 1) * i; 
            if(isHit){
               screen.fillRect(rect, colorG * 0x10000);
            }else{
                screen.fillRect(rect, colorG * 0x100 + colorG );
            }
            posX -= (PLAYER_POSX / ARRAY_LENGTH);
        }
        var wallRectTop : Rectangle = new Rectangle;
        var wallRectBtn : Rectangle = new Rectangle;
        var wallPosX : int = SCREEN_WIDTH - (SCREEN_WIDTH / WALL_NUM);
        for(var j:int = wall.length -1;j > 0; j--){
            wallRectTop.x = wallPosX; wallRectTop.y = 0; wallRectTop.width = SCREEN_WIDTH / WALL_NUM; wallRectTop.height = wall[j];    
            var height : int = spaceHeight;
            if(j < dirChangeCnt){
                height = beforeSpaceHeight;    
            }
            wallRectBtn.x = wallPosX; wallRectBtn.y = wall[j] + height; wallRectBtn.width = SCREEN_WIDTH / WALL_NUM; wallRectBtn.height = SCREEN_HEIGHT - wall[j] - spaceHeight;
            screen.fillRect(wallRectTop, 0xffffff);
            screen.fillRect(wallRectBtn,0xffffff);
            //if(j == 20){
            //    screen.fillRect(wallRectTop, 0xffff00);    
            //}
            wallPosX -= SCREEN_WIDTH / WALL_NUM;
        } 

     }
}

function createTextField(x:int, y:int, width:int, size:int, color:int, align:String = TextFormatAlign.LEFT):TextField {
    var fm:TextFormat = new TextFormat, fi:TextField = new TextField;
    fm.font = "_typewriter"; fm.bold = true; fm.size = size; fm.color = color; fm.align = align;
    fi.defaultTextFormat = fm; fi.x = x; fi.y = y; fi.width = width; fi.selectable = false;
    return fi;
}