ren811

PONG

Nov 19th, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import flash.events.TimerEvent;
  2. import flash.events.KeyboardEvent;
  3. import flash.events.Event;
  4.  
  5. stage.addEventListener(Event.ENTER_FRAME,direcao);
  6. stage.addEventListener(Event.ENTER_FRAME,cpu);
  7.  
  8. var posAcerto:Number;
  9. var percentCenter:Number;
  10. var ajusteBola:Number;
  11.  
  12. var bolaX:int = 5;
  13. var bolaY:int = 5;
  14. var maxBolaX:int = 10;
  15. var maxBolaY:int = 10;
  16.  
  17. var maxVelBola = 10;
  18.  
  19. var pontosPlayer:int = 0;
  20. var pontosCPU:int = 0;
  21.  
  22. // TESTA COLISÃO E ALTERA DIREÇÃO DA BOLA
  23. function direcao(evt:Event){
  24.     if (player.hitTestObject(bola)){
  25.         bolaX *= (-1);
  26.         player.gotoAndPlay(2);
  27.         /*
  28.         posAcerto = bola.y - (player.y+(player.height/2)-bola.height);
  29.         percentCenter = posAcerto/player.height/2;
  30.         ajusteBola = percentCenter * 10;
  31.         */
  32.        
  33.         /*
  34.         É necessário dividir a barra em pelo menos 3 seções diferentes para
  35.         alterar o ângulo no qual a bola reflete (alterando Y da bola).
  36.         /\
  37.         ||
  38.         || _______
  39.         || _______________CENTRO da barra
  40.         || _______
  41.         ||
  42.         ||
  43.         \/
  44.         Dividindo a barra em três seções:
  45.         player.height/3 (125.4/3) (41.8)
  46.        
  47.         as divisões da barra então são:
  48.         A  =  do topo até: player.y - (player.height/2);
  49.         B  =  meio, pegando 20.9 pra baixo e pra cima, a partir do centro da barra
  50.         C  =  de player.y + (player.height/2) até a parte inferior da barra;
  51.        
  52.         if (bola.y < (player.y - (player.height/2))){
  53.             // bolaY tem que diminuir
  54.         }
  55.         if (bola.y >= (player.y - (player.height/2)) && bola.y <= (player.y - (player.height/2))){
  56.             // bolaY mantem igual
  57.         }
  58.         if (bola.y > (player.y + (player.height/2))){
  59.             // bolaY tem que aumentar
  60.         }
  61.        
  62.         Porcentagem do centro:
  63.         Suponto 125.4 como a altura da barra:
  64.         125.4 / 100 = [1.254 para 1%]
  65.        
  66.         percentCenter
  67.         ajusteBola
  68.         posAcerto
  69.        
  70.         */
  71.         if (bola.y > player.y){
  72.             percentCenter = (bola.y - player.y) * (player.height/100);
  73.             ajusteBola = (percentCenter/10);
  74.             if (bolaY >= maxVelBola){
  75.                 bolaY = maxVelBola;
  76.             }
  77.             else
  78.                 bolaY += ajusteBola;
  79.         }
  80.         if (bola.y < player.y){
  81.             percentCenter = (bola.y - player.y) * (player.height/100);
  82.             ajusteBola = (percentCenter/10);
  83.             if (bolaY >= maxVelBola){
  84.                 bolaY = maxVelBola;
  85.             }
  86.             else
  87.                 bolaY += ajusteBola;
  88.         }
  89.         if (bola.y == player.y){
  90.             ajusteBola = 0;
  91.             if (bolaY >= maxVelBola){
  92.                 bolaY = maxVelBola;
  93.             }
  94.             else
  95.                 bolaY += ajusteBola;
  96.         }
  97.         /*
  98.        
  99.         if (bola.y > player.y){
  100.             bolaY tem que aumentar
  101.             bola.y - player.y
  102.             200    - 190       = 10
  103.             10     * 1.254     = 12.54 (12.54 graus, porque foi perto do centro);
  104.            
  105.             200 - 130
  106.             70  * 1.254        = 87.78 (quase 90 graus, porque bateu na ponta);
  107.         }
  108.         if (bola.y < player.y){
  109.             bolaY tem que diminuir
  110.             player.y - bola.y
  111.             190    - 200       = -10
  112.             10     * 1.254     = -12.54;
  113.            
  114.             130    - 200       = -70
  115.             -70    * 1.254     = -87.78 (quase 90 graus, porque bateu na ponta);
  116.         }
  117.         if (bola.y == player.y){
  118.             // nao altera
  119.             (porcentagem do centro = 0)
  120.         }
  121.        
  122.         if (bolaY >= maxVelBola){
  123.             bolaY = maxVelBola;
  124.         }
  125.         // se bolaY < que 15
  126.         else
  127.             bolaY += ajusteBola;
  128.         */
  129.     }
  130.     if (inimigo.hitTestObject(bola)){
  131.         inimigo.gotoAndPlay(2);
  132.         //trace(bola.height);
  133.         bolaX *= (-1);
  134.         /*
  135.         posAcerto = bola.y - (inimigo.y+(player.height/2)-bola.height);
  136.         percentCenter = posAcerto/inimigo.height/2;
  137.         ajusteBola = percentCenter * 10;
  138.         if (bolaY >= maxVelBola){
  139.             bolaY = maxVelBola;
  140.         }
  141.         else
  142.             bolaY += ajusteBola;
  143.         */
  144.         if (bola.y > inimigo.y){
  145.             percentCenter = (bola.y - inimigo.y) * (inimigo.height/100);
  146.             ajusteBola = (percentCenter/10);
  147.             if (bolaY >= maxVelBola){
  148.                 bolaY = maxVelBola;
  149.             }
  150.             else
  151.                 bolaY += ajusteBola;
  152.         }
  153.         if (bola.y < inimigo.y){
  154.             percentCenter = (bola.y - inimigo.y) * (inimigo.height/100);
  155.             ajusteBola = (percentCenter/10);
  156.             if (bolaY >= maxVelBola){
  157.                 bolaY = maxVelBola;
  158.             }
  159.             else
  160.                 bolaY += ajusteBola;
  161.         }
  162.         if (bola.y == inimigo.y){
  163.             ajusteBola = 0;
  164.             if (bolaY >= maxVelBola){
  165.                 bolaY = maxVelBola;
  166.             }
  167.             else
  168.                 bolaY += ajusteBola;
  169.         }
  170.     }
  171.     // BOLA SAINDO //
  172.     if (bola.x < 5){
  173.         bola.x = 225;
  174.         bola.y = 200;
  175.         bolaY = 5;
  176.         //trace("PONTO DA CPU!");
  177.         pontosCPU++;
  178.     }
  179.     if (bola.x > 545){
  180.         bola.x = 225;
  181.         bola.y = 200;
  182.         bolaY = 5;
  183.         //trace("PONTO DO JOGADOR!");
  184.         pontosPlayer++;
  185.     }
  186.     if (bola.y <= 0){
  187.         bolaY *= (-1);
  188.     }
  189.     if (bola.y >= 400){
  190.         bolaY *= (-1);
  191.     }
  192.     // MOVIMENTA BOLA //
  193.     if (bolaX > maxBolaX){
  194.         bolaX = maxBolaX;
  195.     }
  196.     bola.x += bolaX;
  197.     if (bolaY > maxBolaY){
  198.         bolaY = maxBolaY;
  199.     }
  200.     bola.y += bolaY;
  201.    
  202.     // movimenta player de acordo com a posição do mouse
  203.     var velMax:Number = 7;
  204.     var playerDelta:Number = player.y - mouseY;
  205.     var movePlayer:Number = Math.min(Math.abs(playerDelta),velMax);
  206.     if (playerDelta < 0){
  207.         player.y += movePlayer;
  208.     }
  209.     else {
  210.         player.y -= movePlayer;
  211.     }
  212.     // placares
  213.     placarPlayer.text = pontosPlayer.toString();
  214.     placarCPU.text = pontosCPU.toString();
  215. }
  216.  
  217. function cpu(evt:Event):void{
  218.     var pontoDeToque:Number = Math.random() % inimigo.height;
  219.     var velMaxima:Number = 6;
  220.  
  221.     // Obtem a distancia que a barra se move para atingir a bola
  222.     var bolaDelta:Number = inimigo.y + pontoDeToque - bola.y;
  223.    
  224.     // Computa a quantidade para mover a bola, ou o maximo que pode-se
  225.     // tentar para atingí-la.
  226.     var moveInimigo:Number = Math.min(Math.abs(bolaDelta), velMaxima);
  227.  
  228.     if (bolaDelta < 0){
  229.         inimigo.y += moveInimigo;
  230.     }
  231.     else {
  232.         inimigo.y -= moveInimigo;
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment