Skip to content

Commit

Permalink
added collisions
Browse files Browse the repository at this point in the history
  • Loading branch information
jellinki committed Oct 23, 2023
1 parent 42e8741 commit 09f1974
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions _posts/2023-10-12-teamwork.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -172,33 +178,27 @@ 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();
c.moveTo(platform.x, platform.y + 5);
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;

Expand All @@ -208,15 +208,15 @@ courses: { compsci: {week: 7} }
isJumping = false;
}

// Handle sprite movement
if (isMovingLeft && spriteX > 0) {
spriteX -= moveSpeed;
}
if (isMovingRight && spriteX + spriteWidth < canvas.width) {
spriteX += moveSpeed;
}

// Draw the sprite
checkCollisions();

c.drawImage(
spriteImage,
frameX * spriteWidth,
Expand Down

0 comments on commit 09f1974

Please sign in to comment.