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