I have followed a Youtube guide (https://www.youtube.com/watch?v=D9LqQEPGoo8) on how to make a 2D tank game with JS. I feel like I have done/written the same things as he did, but when I test out the movement of the tanks, my wont move at all?
Im completely new to programming, and trying to get a hang on it. I have tried to find a solution myself but I cant figure out the problem.
The images are getting draw, and I have have tested that it takes “key-inputs” and I can console.log its values, but it still doesn’t work.
Can you figure out whats wrong?
I have tried to switch a bit up on the variables, to see how the game would react and it feels like it does the right thing. I just doesn’t wont to move. Maybe the problem is at controls()?
<body>
<canvas id="myCanvas" width="800" height="600"></canvas>
<script type="text/javascript">
let canvas = document.getElementById("myCanvas");
let ctx = canvas.getContext("2d");
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
// Tanks udseende
let tank1 = new Image();
tank1.src = "tank1.png";
let tank2 = new Image();
tank2.src = "tank2.png";
// Definering af variabler til controls
let upPressed = false;
let downPressed = false;
let leftPressed = false;
let rightPressed = false;
let zeroPressed = false;
let wPressed = false;
let sPressed = false;
let dPressed = false;
let aPressed = false;
let spacePressed = false;
// keyUp og Down kontrollere om knappen bliver trykket eller ej
// Tank 1 controls
function keyDownHandler(e) {
if (e.keyCode == 38)
{
upPressed = true;
}
if (e.keyCode == 40)
{
downPressed = true;
}
if (e.keyCode == 37)
{
leftPressed = true;
}
if (e.keyCode == 39)
{
rightPressed = true;
}
if (e.keyCode == 32)
{
spacePressed = true;
}
// Tank 2 controls
if (e.keyCode == 87)
{
wPressed = true;
}
if (e.keyCode == 83)
{
sPressed = true;
}
if (e.keyCode == 65)
{
aPressed = true;
}
if (e.keyCode == 68)
{
dPressed = true;
}
if (e.keyCode == 96)
{
zeroPressed = true;
}
}
function keyUpHandler(e) {
if (e.keyCode == 38)
{
upPressed = false;
}
if (e.keyCode == 40)
{
downPressed = false;
}
if (e.keyCode == 37)
{
leftPressed = false;
}
if (e.keyCode == 39)
{
rightPressed = false;
}
if (e.keyCode == 32)
{
spacePressed = false;
}
// Tank 2 controls
if (e.keyCode == 87)
{
wPressed = false;
}
if (e.keyCode == 83)
{
sPressed = false;
}
if (e.keyCode == 65)
{
aPressed = false;
}
if (e.keyCode == 68)
{
dPressed = false;
}
if (e.keyCode == 96)
{
zeroPressed = false;
}
}
// For rotere img
function drawImageRot(img, x, y, width, height, deg)
{
let rad = deg * Math.PI / 180;
ctx.translate( x + width / 2, y + height / 2);
ctx.rotate(rad);
ctx.drawImage(img, width / 2 * -1, height / 2 * -1, width, height);
ctx.rotate(rad * -1);
ctx.translate(x + width / 2 * -1, y + height / 2 * -1);
}
let player1 = new Player(50, 100, 0, 40, 30);
let player2 = new Player(300, 300, 0, 40, 30);
function controls()
{
// Tank1
if (leftPressed)
{
player1.rot -= 1;
}
if (rightPressed)
{
player1.rot += 1;
}
if (upPressed)
{
player1.x += Math.cos(player1.rot * Math.PI / 180);
player1.y += Math.sin(player1.rot * Math.PI / 180);
}
if (downPressed)
{
player1.x -= Math.cos(player1.rot * Math.PI / 180);
player1.y -= Math.sin(player1.rot * Math.PI / 180);
}
// Tank2
if (aPressed)
{
player2.rot -= 1;
}
if (dPressed)
{
player2.rot += 1;
}
if (wPressed)
{
player2.x += Math.cos(player2.rot * Math.PI / 180);
player2.y += Math.sin(player2.rot * Math.PI / 180);
}
if (sPressed)
{
player2.x -= Math.cos(player2.rot * Math.PI / 180);
player2.y -= Math.sin(player2.rot * Math.PI / 180);
}
}
// Playerframework
function Player(x, y, rot, w, h)
{
this.x = x;
this.y = y;
this.rot = rot;
this.w = w;
this.h = h;
this.canFire = true;
this.score = 0;
this.hasBeenHit = false;
}
function draw()
{
ctx.clearRect (0, 0, myCanvas.width, myCanvas.height);
controls();
if (!player1.hasBeenHit)
{
drawImageRot(tank1, player1.x, player1.y, player1.w, player1.h, player1.rot);
}
if (!player2.hasBeenHit)
{
drawImageRot(tank2, player2.x, player2.y, player2.w, player2.h, player2.rot);
}
}
setInterval (draw, 10);
</script>
</body>