Tengo como 3 días programando un PONG, y ya casi está listo el codigo base para despues comenzar a estilizarlo, el problema con el que me encuentro ahorita es que, al dejar apretada la tecla de arriba del jugador 1 su paleta comienza a moverse, pero si oprimo cualquier otra ésta se deja de mover, y lo que quiero saber es como puedo hacer que si aprieto 2 o más teclas, las dos manden sus respectivas funciones?
El codigo del pong que llevo hasta ahorita es:
Código javascript:
Se aceptan criticas y recomendaciones :p Ver original
<html> <head> </head> <body style="overflow:hidden" bgcolor="black"> <canvas id="a" width="100" height="100">Your browser does not support the Canvas element.</canvas> <script type="text/javascript"> var VELOCITY = 20; var PARTICLES = 1; var z = 1; var v1 = 0; var v2 = 0; var p1 = 0; var p2 = 0; var myImage = new Image(); myImage.src = "a.png"; var myImage2 = new Image(); myImage2.src = "a2.png"; var myImage3 = new Image(); myImage3.src = "a3.png"; var myImage4 = new Image(); myImage4.src = "a4.png"; var particles = []; var colors = [ "rgba(0,255,255,.50)","rgba(255,0,0,.50)","rgba(255,255,0,.50)","rgba(100,255,185,.50)" ]; var canvas = document.getElementById('a'); var context; if (canvas && canvas.getContext) { context = canvas.getContext('2d'); for( var i = 0; i < PARTICLES; i++ ) { particles.push( { x: Math.random()*window.innerWidth, y: Math.random()*window.innerHeight, vx: ((Math.random()*(VELOCITY*2))-VELOCITY), vy: ((Math.random()*(VELOCITY*2))-VELOCITY), size: 1+Math.random()*2.5, color: colors[ Math.floor( Math.random() * colors.length ) ] } ); } Initialize(); } function Initialize() { canvas.addEventListener('mousemove', MouseMove, false); window.addEventListener('resize', ResizeCanvas, false); setInterval( TimeUpdate, 1000/24 ); ResizeCanvas(); } function TimeUpdate(e) { context.clearRect(0, 0, window.innerWidth, window.innerHeight); var len = particles.length; var particle; var aux1; var aux2; var posy1 = v1; var posy2 = v2; context.fillStyle = 'rgb(200,200,0)'; context.beginPath(); context.rect(0,posy1,5,150); context.closePath(); context.fill(); context.fillStyle = 'rgb(200,200,0)'; context.beginPath(); context.rect(window.innerWidth-15,posy2,5,150); context.closePath(); context.fill(); context.fillStyle = 'rgba(255,255,255,1)'; context.font = '15px""'; context.fillText("Controles",50,30); context.fillText("P1: arriba=w abajo=s",50,45); context.fillText("P2: arriba=8 abajo=2",50,60); context.fillText("Nueva bola= n",50,75); context.font = '35px""'; context.fillText(p1+" || "+ p2 ,(window.innerWidth/2)-30,30); for( var i = 0; i < len; i++ ) { particle = particles[i]; particle.x += particle.vx; particle.y += particle.vy; if (particle.x > window.innerWidth-45 && (particle.y > (posy2) && particle.y < (posy2+150))) { VELOCITY += .35; particle.vx = -VELOCITY - Math.random(); } else if (particle.x < 35 && (particle.y > (posy1) && particle.y < (posy1+150))) { VELOCITY += .35; particle.vx = VELOCITY + Math.random(); } else { particle.vx *= (1 + (Math.random() * 0.005)); } if (particle.y > window.innerHeight-8) { particle.vy = -VELOCITY - Math.random(); } else if (particle.y < 0) { particle.vy = VELOCITY + Math.random(); } else { particle.vy *= (1 + (Math.random() * 0.005)); } context.strokeStyle = particle.color; context.beginPath(); context.arc(particle.x,particle.y,20,0,Math.PI*2,true); context.closePath(); context.stroke(); if(particle.vx<=0){ if ((particle.x > window.innerWidth-60 && (particle.y > (posy2) && particle.y < (posy2+150))) || (particle.x < 50 && (particle.y > (posy1) && particle.y < (posy1+150)))) { context.drawImage(myImage4, particle.x-20, particle.y-20, 40, 40); } else{ context.drawImage(myImage2, particle.x-20, particle.y-20, 40, 40); } } else{ if ((particle.x > window.innerWidth-60 && (particle.y > (posy2) && particle.y < (posy2+150))) || (particle.x < 50 && (particle.y > (posy1) && particle.y < (posy1+150)))) { context.drawImage(myImage3, particle.x-20, particle.y-20, 40, 40); } else{ context.drawImage(myImage, particle.x-20, particle.y-20, 40, 40); } } if(particle.x<0||particle.x>window.innerWidth){ aux1=-particle.vx; particle.vx=0; aux2=-particle.vy; particle.vy=0; particle.y=-30; if(particle.x < 0 && particle.x != -30){ p2++; particle.x=-30; }; if(particle.x>window.innerWidth && particle.x != -30){ p1++; particle.x=-30; }; }; } } function MouseMove(e) { mouse.x = e.layerX; mouse.y = e.layerY; } document.onkeydown = function(event) { var keyCode = (window.event||event).keyCode; switch(keyCode) { case 78: VELOCITY = 20; particles[0].x=(window.innerWidth/2); particles[0].y=(window.innerHeight/2); particles[0].vx=((Math.random()*(VELOCITY*2))-VELOCITY); particles[0].vy=((Math.random()*(VELOCITY*2))-VELOCITY); TimeUpdate(); break; case 104: if(v2>-1){ v2-=45; } else{ v2=0; } break; case 87: if(v1>-1){ v1-=45; } else{ v1=0; } break; case 98: if(v2<window.innerHeight-80){ v2+=45; } else{ v2=window.innerHeight-90; } break; case 83: if(v1<window.innerHeight-80){ v1+=45; } else{ v1=window.innerHeight-90; } break; default: break; } } function ResizeCanvas(e) { canvas.width = window.innerWidth; canvas.height = window.innerHeight; } </script> </body> </html>