🔬 Gravity is always adding to vertical velocity. What if you change spiderman.vy += gravity to spiderman.vy += gravity * 2? How does Spider-Man fall differently?
// Apply gravity
spiderman.vy += gravity;
if (spiderman.isSwinging) {
🔬 This line calculates how hard the web pulls based on how far Spider-Man stretches. What happens if you multiply swingForce by 10 instead (change * swingForce to * swingForce * 10)? The web becomes a catapult!
// Web tension force (pulls Spiderman towards the swingPoint if he stretches the web)
if (currentDist > spiderman.webLength) {
let forceMagnitude = (currentDist - spiderman.webLength) * swingForce;
function draw() {
background(135, 206, 235); // Sky blue background
if (!gameOver) {
// === Update and Draw Buildings ===
for (let i = buildings.length - 1; i >= 0; i--) {
let b = buildings[i];
b.x -= gameSpeed; // Move building left
fill(100); // Gray buildings
noStroke();
rect(b.x, b.y, b.width, b.height);
// Remove building if it goes off-screen
if (b.x + b.width < 0) {
buildings.splice(i, 1);
}
}
// Add new buildings when space opens up
if (buildings.length === 0 || width - buildings[buildings.length - 1].x > random(100, 300)) {
addBuilding();
}
// === Update Spiderman ===
// Apply gravity
spiderman.vy += gravity;
if (spiderman.isSwinging) {
// Calculate vector from Spiderman to the swing point
let dx = spiderman.swingPoint.x - spiderman.x;
let dy = spiderman.swingPoint.y - spiderman.y;
let currentDist = sqrt(dx * dx + dy * dy);
// Web tension force (pulls Spiderman towards the swingPoint if he stretches the web)
if (currentDist > spiderman.webLength) {
let forceMagnitude = (currentDist - spiderman.webLength) * swingForce;
spiderman.vx += (dx / currentDist) * forceMagnitude;
spiderman.vy += (dy / currentDist) * forceMagnitude;
}
// Damping to simulate air resistance and prevent endless swinging
spiderman.vx *= damping;
spiderman.vy *= damping;
}
// Update Spiderman's position based on velocity
spiderman.x += spiderman.vx;
spiderman.y += spiderman.vy;
// Boundary check: If Spiderman falls off the bottom of the screen, it's game over
if (spiderman.y - spiderman.size / 2 >= height) {
gameOver = true;
}
// Boundary check: Keep Spiderman within horizontal bounds (or let him scroll with the game)
if (spiderman.x < spiderman.size / 2) {
spiderman.x = spiderman.size / 2;
spiderman.vx = 0;
}
// If Spidey goes too far right, scroll the city faster to catch up
if (spiderman.x > width * 0.75) {
// This is a simple way to keep Spidey roughly in frame
spiderman.x = width * 0.75;
gameSpeed = 5; // Temporarily speed up city scroll
} else {
gameSpeed = 3; // Reset to normal scroll speed
}
// === Draw Spiderman ===
// Draw web if swinging
if (spiderman.isSwinging && spiderman.swingPoint) {
stroke(200); // White web
strokeWeight(2);
line(spiderman.x, spiderman.y, spiderman.swingPoint.x, spiderman.swingPoint.y);
}
// Draw Spiderman body (red)
fill(spiderman.color);
noStroke();
ellipse(spiderman.x, spiderman.y, spiderman.size, spiderman.size);
// Draw Spiderman "suit" details (blue)
fill(spiderman.suitColor);
ellipse(spiderman.x, spiderman.y, spiderman.size * 0.7, spiderman.size * 0.7);
fill(0);
textSize(12);
textAlign(CENTER, CENTER);
text("Spidey", spiderman.x, spiderman.y + spiderman.size / 2 + 10); // Label
// === Score ===
// Score increases with horizontal distance traveled
score++;
fill(0);
textSize(24);
textAlign(LEFT, TOP);
text("Score: " + score, 10, 10);
} else {
// === Game Over Screen ===
fill(0);
textSize(48);
textAlign(CENTER, CENTER);
text("GAME OVER!", width / 2, height / 2 - 50);
textSize(24);
text("You swung " + score + " units through the city!", width / 2, height / 2);
text("Press SPACE to Restart", width / 2, height / 2 + 50);
}
}