From 8dfd9655a79baac1e9f57e52c624e84405df8f85 Mon Sep 17 00:00:00 2001 From: jellinki Date: Mon, 23 Oct 2023 14:30:12 -0700 Subject: [PATCH] added movement to link --- _posts/2023-10-12-teamwork.md | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/_posts/2023-10-12-teamwork.md b/_posts/2023-10-12-teamwork.md index c8cf5ba..2a00862 100644 --- a/_posts/2023-10-12-teamwork.md +++ b/_posts/2023-10-12-teamwork.md @@ -37,6 +37,7 @@ courses: { compsci: {week: 7} } const gravity = 0.5; const jumpStrength = -10; + const moveSpeed = 5; function updateSpriteAnimation() { if (frameX < maxFrame) { @@ -74,7 +75,7 @@ courses: { compsci: {week: 7} } } window.addEventListener('keydown', (event) => { - if (event.key === 'w') { + if (event.key === 'w' && !isJumping) { jump(); } else if (event.key === 'a') { moveLeft(); @@ -104,12 +105,14 @@ courses: { compsci: {week: 7} } x: 0, y: 0 } + var bg2 = { width: 500, height: 1000, x: 0, y: -1000 } + var bg3 = { width: 500, height: 1000, @@ -121,7 +124,6 @@ courses: { compsci: {week: 7} } var platforms = []; // Constants for jump behavior - const gravity = 0.5; const jumpStrength = -10; // Function to generate random platforms throughout the screen @@ -243,6 +245,24 @@ courses: { compsci: {week: 7} } platform.y += 5; }); + // Apply gravity and handle jumping + spriteVelocityY += gravity; + spriteY += spriteVelocityY; + + if (spriteY >= canvas.height - spriteHeight) { + spriteY = canvas.height - spriteHeight; + spriteVelocityY = 0; + isJumping = false; + } + + // Handle sprite movement + if (isMovingLeft && spriteX > 0) { + spriteX -= moveSpeed; + } + if (isMovingRight && spriteX + spriteWidth < canvas.width) { + spriteX += moveSpeed; + } + // Draw the sprite c.drawImage( spriteImage,