Guest User

Untitled

a guest
Sep 22nd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. for(var ranOnce:int =0, paddle:Sprite = new Sprite(), xpos:int=0, introText:TextField = new TextField(),
  2.         anyKeyPressed:Boolean=false, blocksCreated=false, ball:Sprite = new Sprite(),
  3.         ballXSpeed=5, ballYSpeed=5, blocksHit=0, ballYPosition:int=stage.stageHeight-50,
  4.         colorArray = new Array(0xA2BAB0, 0x8FA89B, 0x678C8B), blockArray:Array = new Array(),
  5.         ballXPosition:int=stage.stageWidth/2, paddleXPosition, lives=3, i, j, k, index,
  6.         gameBackground:Sprite = new Sprite(), life:Array = new Array(), gameOver=false,
  7.         livesText:TextField = new TextField();
  8.     ranOnce<1;
  9.     ranOnce++, gameBackground.graphics.beginFill(0xDCFFC8), gameBackground.graphics.drawRect(0,0,stage.stageWidth, stage.stageHeight),
  10.       addChild(gameBackground), introText.text = "Press any key to start!", introText.width = 150, introText.selectable = false,
  11.       addChild(introText),
  12.       stage.addEventListener(KeyboardEvent.KEY_DOWN,
  13.         function keyHandling(keyEvent:KeyboardEvent) {
  14.             if(gameOver==true){
  15.                 stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyHandling);
  16.             }else{
  17.                 if(keyEvent.keyCode==39 && xpos < 25){
  18.                     xpos++;
  19.                 }
  20.                 else if (keyEvent.keyCode==37 && xpos > -25){
  21.                     xpos--;
  22.                 }
  23.                 if(anyKeyPressed==false){
  24.                     var textFormat:TextFormat = new TextFormat();
  25.                     textFormat.size = 16;
  26.                    
  27.                     introText.defaultTextFormat = textFormat;
  28.                     introText.text="Score: 0";
  29.                     livesText.width = 150;
  30.                     livesText.selectable = false;
  31.                     livesText.y = 20;
  32.                     livesText.defaultTextFormat = textFormat;
  33.                     livesText.text = "Lives: ";
  34.                     addChild(livesText);
  35.                    
  36.                     for(i=0; i<4; i++){
  37.                         for(j=0; j<10; j++){
  38.                             index = i*10+j;
  39.                             blockArray[index]= new Sprite();
  40.                             blockArray[index].graphics.beginFill(colorArray[Math.round(Math.random()*2)]);
  41.                             blockArray[index].graphics.drawRect(25+(j*50), 50+(i*25), 49, 24);
  42.                             addChild(blockArray[index]);
  43.                         }
  44.                     }
  45.                     for(k=0; k<lives; k++){
  46.                         life[k] = new Sprite();
  47.                         life[k].graphics.beginFill(0x6DA69D);
  48.                         life[k].graphics.drawCircle(50+15*k,33,5);
  49.                         addChild(life[k]);
  50.                     }
  51.                     anyKeyPressed=true;
  52.                 }
  53.                 paddle.graphics.clear();
  54.                 paddle.graphics.beginFill(0x716EB2);
  55.                 paddleXPosition=stage.stageWidth/2 -25 +xpos*10;
  56.                 paddle.graphics.drawRect(paddleXPosition,stage.stageHeight-30,50,10);
  57.                 addChild(paddle);
  58.             }
  59.         }),
  60.     stage.addEventListener(Event.ENTER_FRAME,
  61.     function main(e:Event){
  62.         if(anyKeyPressed==true){
  63.             if(ballXPosition >= 550 || ballXPosition <= 0){ //if ball exits the stage
  64.                 ballXSpeed = -ballXSpeed;
  65.             }
  66.             if(ballYPosition <= 0){
  67.                 ballYSpeed = -ballYSpeed;
  68.             }else if(ballYPosition >= 500){
  69.                 lives--;
  70.                 removeChild(life[lives]);
  71.                 if(lives >0){
  72.                     ballXPosition=paddleXPosition+20;
  73.                     ballYPosition=stage.stageHeight-35;
  74.                 }else{
  75.                     introText.text = "YOU LOSE!"
  76.                     gameOver=true;
  77.                 }
  78.             }
  79.             ball.graphics.clear();
  80.             ball.graphics.beginFill(0x6DA69D);
  81.             ball.graphics.drawCircle(ballXPosition+=ballXSpeed, ballYPosition+=ballYSpeed, 5);
  82.             addChild(ball);
  83.             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
  84.                 ballYSpeed = -ballYSpeed;
  85.             }
  86.             var xQuadrant = (ballXPosition-20)/50;
  87.             var yQuadrant = (ballYPosition-45)/25;
  88.             var index = Math.floor(yQuadrant)*10+Math.floor(xQuadrant);
  89.             var xPosition = Number("."+String(xQuadrant).split(".")[1]);
  90.             var yPosition = Number("."+String(yQuadrant).split(".")[1]);
  91.             trace(ballXPosition + ": " + xQuadrant + ": " + index);
  92.             if(index <40 && index>=0 && ballXPosition >=25 && ballXPosition <= 525){
  93.                 if(stage.contains(blockArray[index])){
  94.                     removeChild(blockArray[index]);
  95.                     blocksHit++;
  96.                     introText.text = "Score: " + (blocksHit*100).toString();
  97.                     if(xPosition == 0.1 || xPosition == 0.9){
  98.                         ballXSpeed = -ballXSpeed;
  99.                     }else if(yPosition == 0.1 || yPosition == 0.9){
  100.                         ballYSpeed = -ballYSpeed;
  101.                     }
  102.                 }
  103.             }
  104.            
  105.             if(blocksHit==40){
  106.                 introText.text = "YOU WIN!";
  107.                 gameOver=true;
  108.             }
  109.             if(gameOver==true){
  110.                 removeChild(paddle);
  111.                 removeChild(ball);
  112.                 removeChild(livesText);
  113.                 for(k=0; k<lives; k++){
  114.                     removeChild(life[k]);
  115.                 }
  116.                 if(introText.text == "YOU LOSE!"){
  117.                     for(var i=0; i<4; i++){
  118.                         for(j=0; j<10; j++){
  119.                             index = i*10+j;
  120.                             if(stage.contains(blockArray[index])){
  121.                                 removeChild(blockArray[index]);
  122.                             }
  123.                         }
  124.                     }
  125.                 }
  126.                 stage.removeEventListener(Event.ENTER_FRAME, main);
  127.             }
  128.         }
  129.     })
  130. );
Add Comment
Please, Sign In to add comment