diff --git a/_posts/2023-10-12-teamwork.md b/_posts/2023-10-12-teamwork.md index ca56647..cef9470 100644 --- a/_posts/2023-10-12-teamwork.md +++ b/_posts/2023-10-12-teamwork.md @@ -37,7 +37,7 @@ courses: { compsci: {week: 7} } const gravity = 0.5; const jumpStrength = -10; - const moveSpeed = 8; // Increase sprite movement speed + const moveSpeed = 8; function updateSpriteAnimation() { if (frameX < maxFrame) { @@ -94,11 +94,9 @@ courses: { compsci: {week: 7} } } }); - // Load background image var bgImage = new Image(); bgImage.src = "{{site.baseurl}}/images/Stone_Background.jpg"; bgImage.onload = function () { - // Set up backgrounds var bg1 = { width: 500, height: 1000, @@ -120,25 +118,20 @@ courses: { compsci: {week: 7} } y: -2000 } - // Create an array to hold platform information var platforms = []; - // Constants for jump behavior const jumpStrength = -10; - // Function to generate random platforms throughout the screen function generateRandomPlatform() { - // Define the platform properties var platform = { width: 150, height: 20, x: Math.random() * (canvas.width - 150), - y: canvas.height - Math.random() * (canvas.height) // Platforms appear at various vertical positions + y: canvas.height - Math.random() * (canvas.height) }; platforms.push(platform); - // Add a trampoline on some platforms randomly - if (Math.random() < 0.1) { // Reduce the probability for fewer platforms + if (Math.random() < 0.1) { var trampoline = { x: platform.x + platform.width / 2 - 10, y: platform.y - 10, @@ -149,15 +142,28 @@ courses: { compsci: {week: 7} } } } - // Call the platform generation function initially to ensure a platform is within jumping distance generateRandomPlatform(); - // Call the platform generation function more slowly for fewer platforms - setInterval(generateRandomPlatform, 1200); // Increase interval for fewer platforms + setInterval(generateRandomPlatform, 1200); + + function checkCollisions() { + // Check collisions between the sprite and platforms + platforms.forEach(function (platform) { + if ( + spriteX + spriteWidth > platform.x && + spriteX < platform.x + platform.width && + spriteY + spriteHeight > platform.y && + spriteY < platform.y + ) { + // Collision detected, set the sprite on the platform + spriteY = platform.y - spriteHeight; + isJumping = false; + spriteVelocityY = 0; + } + }); + } - // Main game loop var interval = setInterval(function () { - // Move the backgrounds bg1.y += 5; bg2.y += 5; bg3.y += 5; @@ -172,18 +178,14 @@ courses: { compsci: {week: 7} } bg3.y = -2000; } - // Clear the canvas c.clearRect(0, 0, canvas.width, canvas.height); - // Draw the background c.drawImage(bgImage, bg1.x, bg1.y); c.drawImage(bgImage, bg2.x, bg2.y); c.drawImage(bgImage, bg3.x, bg3.y); - // Move and draw the platforms, including trampolines platforms.forEach(function (platform) { if (platform.width === 20) { - // Draw a green line for the trampoline c.strokeStyle = "cyan"; c.lineWidth = 5; c.beginPath(); @@ -191,14 +193,12 @@ courses: { compsci: {week: 7} } c.lineTo(platform.x + platform.width, platform.y + 5); c.stroke(); } else { - // Draw a yellow platform c.fillStyle = "yellow"; c.fillRect(platform.x, platform.y, platform.width, platform.height); } platform.y += 5; }); - // Apply gravity and handle jumping spriteVelocityY += gravity; spriteY += spriteVelocityY; @@ -208,7 +208,6 @@ courses: { compsci: {week: 7} } isJumping = false; } - // Handle sprite movement if (isMovingLeft && spriteX > 0) { spriteX -= moveSpeed; } @@ -216,7 +215,8 @@ courses: { compsci: {week: 7} } spriteX += moveSpeed; } - // Draw the sprite + checkCollisions(); + c.drawImage( spriteImage, frameX * spriteWidth,