Skip to content

Commit

Permalink
fixed platform spawning
Browse files Browse the repository at this point in the history
  • Loading branch information
jellinki committed Oct 18, 2023
1 parent 3551ab5 commit a191a0c
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions _posts/2023-10-12-teamwork.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,41 @@ courses: { compsci: {week: 7} }
// Create an array to hold platform information
var platforms = [];

// Constants for jump behavior
const gravity = 0.5;
const jumpStrength = -10;

// Function to generate random platforms
function generateRandomPlatform() {
// Define the platform properties
var platform = {
width: 150, // Increased width
height: 20, // Increased height
x: Math.random() * (canvas.width - 150), // Adjusted width
y: -50 // Start platforms off-screen
width: 150,
height: 20,
x: Math.random() * (canvas.width - 150),
y: canvas.height - Math.random() * (canvas.height / 3) // Platforms appear at various vertical positions
};
platforms.push(platform);
}

// Call the platform generation function at regular intervals
setInterval(generateRandomPlatform, 3000); // Adjust the interval as needed
// Function to generate platforms at the top of the screen
function generateTopPlatform() {
var platform = {
width: 150,
height: 20,
x: Math.random() * (canvas.width - 150),
y: -20 // Platforms appear at the top of the screen
};
platforms.push(platform);
}

// Call the platform generation function initially to ensure a platform is within jumping distance
generateRandomPlatform();

// Call the platform generation function more frequently to have more platforms
setInterval(generateRandomPlatform, 500); // Decreased interval for more platforms

// Call the top platform generation function at regular intervals
setInterval(generateTopPlatform, 2000); // Platforms at the top every 2 seconds

// Main game loop
var interval = setInterval(function () {
Expand Down Expand Up @@ -83,14 +104,9 @@ courses: { compsci: {week: 7} }

// Move and draw the platforms
platforms.forEach(function (platform) {
platform.y += 5; // Adjust the platform's speed
c.fillStyle = "yellow"; // Changed platform color to yellow
platform.y += 5;
c.fillStyle = "yellow";
c.fillRect(platform.x, platform.y, platform.width, platform.height);

// Remove platforms that are off-screen
if (platform.y > canvas.height) {
platforms.splice(platforms.indexOf(platform), 1);
}
});
}, 50);
};
Expand Down

0 comments on commit a191a0c

Please sign in to comment.