Ver Mensaje Individual
  #15 (permalink)  
Antiguo 19/09/2012, 15:55
Avatar de hackjose
hackjose
 
Fecha de Ingreso: abril-2010
Ubicación: Edo Mexico
Mensajes: 1.178
Antigüedad: 14 años, 1 mes
Puntos: 131
Respuesta: desafios javascript 2012

En cuanto al juego de pong, este es mi avance.
No es un super avance pero lo posteo para que se vea actividad

Codigo

Código HTML:
Ver original
  1. <!DOCTYPE HTML>
  2. <html lang="en-US">
  3.     <meta charset="UTF-8">
  4.     <title>Juego Pong</title>
  5.     <script type="text/javascript">
  6.    
  7.     var X1 = 0, Y1 = 40, X2 = 110, Y2 = 0;
  8.     var canvas = null, ctx = null;
  9.     var PAUSE = true;
  10.     var lastKey = null;
  11.    
  12.     window.addEventListener('load',function(){canvas = document.getElementById('canvas');canvas.style.background = "#000";ctx = canvas.getContext('2d');
  13.     paint(ctx);ejecutar();},false);
  14.    
  15.     document.addEventListener('keydown',function(evt){lastKey = evt.keyCode;ejecutar();},false);
  16.    
  17.     function paint(ctx){
  18.     ctx.fillStyle = "#F00";
  19.     ctx.fillRect(0,0,10,60);
  20.     ctx.fillStyle = "#0F0";
  21.     ctx.fillRect(X1,Y1,10,20);
  22.    
  23.     ctx.fillStyle = "#F00";
  24.     ctx.fillRect(110,0,10,60);
  25.     ctx.fillStyle = "#0F0";
  26.     ctx.fillRect(X2,Y2,10,20);
  27.     }
  28.        
  29.     function ejecutar(){
  30.    
  31.         if(lastKey == 13)
  32.         PAUSE = !PAUSE;
  33.        
  34.         if(PAUSE)
  35.         {
  36.             document.getElementById("pause").innerHTML = "PAUSE";
  37.            
  38.         }
  39.         else
  40.         {
  41.             document.getElementById("pause").innerHTML = "";
  42.        
  43.             if(lastKey == 37)
  44.             {
  45.                 if(Y1 > 0)
  46.                 Y1 -= 2;
  47.                 ctx.fillStyle = "#F00";
  48.                 ctx.fillRect(0,0,10,60);
  49.                 ctx.fillStyle = "#0F0";
  50.                 ctx.fillRect(X1,Y1,10,20);
  51.             }
  52.                
  53.             if(lastKey == 39)
  54.             {
  55.                     if(Y1 < 40)
  56.                     Y1 += 2;
  57.                     ctx.fillStyle = "#F00";
  58.                     ctx.fillRect(0,0,10,60);
  59.                     ctx.fillStyle = "#0F0";
  60.                     ctx.fillRect(X1,Y1,10,20);
  61.             }
  62.                
  63.             if(lastKey == 38)
  64.             {
  65.                 if(Y2 > 0)
  66.                 Y2 -= 2;
  67.                 ctx.fillStyle = "#F00";
  68.                 ctx.fillRect(110,0,10,60);
  69.                 ctx.fillStyle = "#0F0";
  70.                 ctx.fillRect(X2,Y2,10,20);
  71.             }
  72.                
  73.             if(lastKey == 40)
  74.             {
  75.                 if(Y2 < 40)
  76.                 Y2 += 2;
  77.                 ctx.fillStyle = "#F00";
  78.                 ctx.fillRect(110,0,10,60);
  79.                 ctx.fillStyle = "#0F0";
  80.                 ctx.fillRect(X2,Y2,10,20);
  81.             }
  82.        
  83.         }
  84.     }
  85.     </script>
  86. </head>
  87.     <canvas id="canvas" width="120" height="60"></canvas>
  88.     <p id="pause"></p>
  89. </body>
  90. </html>

Saludos