Skip to content

Commit

Permalink
added movement to link
Browse files Browse the repository at this point in the history
  • Loading branch information
jellinki committed Oct 23, 2023
1 parent 356359d commit 8dfd965
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions _posts/2023-10-12-teamwork.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ courses: { compsci: {week: 7} }

const gravity = 0.5;
const jumpStrength = -10;
const moveSpeed = 5;

function updateSpriteAnimation() {
if (frameX < maxFrame) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8dfd965

Please sign in to comment.