Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!