Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

166
Visualizações
Rotate Image on Keypress

I'm working on an asteroids remake in javascript. I'm looking for a way to rotate my ship on a keypress. I've looked everywhere, but I can't find a way to do it. Any Ideas? This is the code I have so far.

var starship = {
  x: 180,
  y: 180,
  angle: 0,
  velocity: {x: 0, y:0}
}
function draw(){
    c.drawImage(img, starship.x, starship.y);  
}
function update(){
  starship.x = starship.x + starship.velocity.x;
  starship.y = starship.y + starship.velocity.y;
  if(starship.x + 20 == 0){ 
  starship.x = 400;
  } else if(starship.x - 20 == 400){ 
  starship.x = 0;
  } else if(starship.y + 20 == 0){ 
  starship.y = 400;
  }  else if(starship.y - 20 == 400){ 
  starship.y = 0;
  }     
}
function animate(){
  requestAnimationFrame(animate)
  c.clearRect(0, 0, canvas.width, canvas.height);
  update();
  draw();
}
animate();
draw();
addEventListener("keydown", ({keyCode}) => {
    switch(keyCode){
        case 87: 
            starship.velocity.y = -2;
        break;
        case 65: 
            starship.velocity.x = -2;
        break;
        case 83: 
            starship.velocity.y = 2;
        break;
        case 68: 
        starship.velocity.x = 2;
        break;
    }
})

Thanks, Oliver

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda