Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- for(var ranOnce:int =0, paddle:Sprite = new Sprite(), xpos:int=0, introText:TextField = new TextField(),
- anyKeyPressed:Boolean=false, blocksCreated=false, ball:Sprite = new Sprite(),
- ballXSpeed=5, ballYSpeed=5, blocksHit=0, ballYPosition:int=stage.stageHeight-50,
- colorArray = new Array(0xA2BAB0, 0x8FA89B, 0x678C8B), blockArray:Array = new Array(),
- ballXPosition:int=stage.stageWidth/2, paddleXPosition, lives=3, i, j, k, index,
- gameBackground:Sprite = new Sprite(), life:Array = new Array(), gameOver=false,
- livesText:TextField = new TextField();
- ranOnce<1;
- ranOnce++, gameBackground.graphics.beginFill(0xDCFFC8), gameBackground.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight),
- addChild(gameBackground), introText.text = "Press any key to start!", introText.width = 150, introText.selectable = false,
- addChild(introText),
- stage.addEventListener(KeyboardEvent.KEY_DOWN,
- function keyHandling(keyEvent:KeyboardEvent) {
- if(gameOver==true){
- stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyHandling);
- }else{
- if(keyEvent.keyCode==39 && xpos < 25){
- xpos++;
- }
- else if (keyEvent.keyCode==37 && xpos > -25){
- xpos--;
- }
- if(anyKeyPressed==false){
- var textFormat:TextFormat = new TextFormat();
- textFormat.size = 16;
- introText.defaultTextFormat = textFormat;
- introText.text="Score: 0";
- livesText.width = 150;
- livesText.selectable = false;
- livesText.y = 20;
- livesText.defaultTextFormat = textFormat;
- livesText.text = "Lives: ";
- addChild(livesText);
- for(i=0; i<4; i++){
- for(j=0; j<10; j++){
- index = i*10+j;
- blockArray[index]= new Sprite();
- blockArray[index].graphics.beginFill(colorArray[Math.round(Math.random()*2)]);
- blockArray[index].graphics.drawRect(25+(j*50), 50+(i*25), 49, 24);
- addChild(blockArray[index]);
- }
- }
- for(k=0; k<lives; k++){
- life[k] = new Sprite();
- life[k].graphics.beginFill(0x6DA69D);
- life[k].graphics.drawCircle(50+15*k,33,5);
- addChild(life[k]);
- }
- anyKeyPressed=true;
- }
- paddle.graphics.clear();
- paddle.graphics.beginFill(0x716EB2);
- paddleXPosition=stage.stageWidth/2 -25 +xpos*10;
- paddle.graphics.drawRect(paddleXPosition,stage.stageHeight-30,50,10);
- addChild(paddle);
- }
- }),
- stage.addEventListener(Event.ENTER_FRAME,
- function main(e:Event){
- if(anyKeyPressed==true){
- if(ballXPosition >= 550 || ballXPosition <= 0){ //if ball exits the stage
- ballXSpeed = -ballXSpeed;
- }
- if(ballYPosition <= 0){
- ballYSpeed = -ballYSpeed;
- }else if(ballYPosition >= 500){
- lives--;
- removeChild(life[lives]);
- if(lives >0){
- ballXPosition=paddleXPosition+20;
- ballYPosition=stage.stageHeight-35;
- }else{
- introText.text = "YOU LOSE!"
- gameOver=true;
- }
- }
- ball.graphics.clear();
- ball.graphics.beginFill(0x6DA69D);
- ball.graphics.drawCircle(ballXPosition+=ballXSpeed, ballYPosition+=ballYSpeed, 5);
- addChild(ball);
- if(Math.round(Math.sqrt(Math.pow(ballXPosition-paddleXPosition-25,2))) <= 30 && ballYPosition >=470 && ballYPosition <= 472){ //if ball close to paddle in X and in Y
- ballYSpeed = -ballYSpeed;
- }
- var xQuadrant = (ballXPosition-20)/50;
- var yQuadrant = (ballYPosition-45)/25;
- var index = Math.floor(yQuadrant)*10+Math.floor(xQuadrant);
- var xPosition = Number("."+String(xQuadrant).split(".")[1]);
- var yPosition = Number("."+String(yQuadrant).split(".")[1]);
- trace(ballXPosition + ": " + xQuadrant + ": " + index);
- if(index <40 && index>=0 && ballXPosition >=25 && ballXPosition <= 525){
- if(stage.contains(blockArray[index])){
- removeChild(blockArray[index]);
- blocksHit++;
- introText.text = "Score: " + (blocksHit*100).toString();
- if(xPosition == 0.1 || xPosition == 0.9){
- ballXSpeed = -ballXSpeed;
- }else if(yPosition == 0.1 || yPosition == 0.9){
- ballYSpeed = -ballYSpeed;
- }
- }
- }
- if(blocksHit==40){
- introText.text = "YOU WIN!";
- gameOver=true;
- }
- if(gameOver==true){
- removeChild(paddle);
- removeChild(ball);
- removeChild(livesText);
- for(k=0; k<lives; k++){
- removeChild(life[k]);
- }
- if(introText.text == "YOU LOSE!"){
- for(var i=0; i<4; i++){
- for(j=0; j<10; j++){
- index = i*10+j;
- if(stage.contains(blockArray[index])){
- removeChild(blockArray[index]);
- }
- }
- }
- }
- stage.removeEventListener(Event.ENTER_FRAME, main);
- }
- }
- })
- );
Add Comment
Please, Sign In to add comment