diff --git a/.DS_Store b/.DS_Store index 306cbd0..81338d9 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/README.md b/README.md index 869bfaf..726cd1a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # norainnoflower Art 173 Game Design Midterm Project + +run python3 -m http.server to play locally +play at http://localhost:8000/ \ No newline at end of file diff --git a/background/flower.js b/background/flower.js new file mode 100644 index 0000000..89a8592 --- /dev/null +++ b/background/flower.js @@ -0,0 +1,75 @@ +class Flower { + constructor(flowerGif) { + this.x = width + 50; + this.y = windowHeight/2 + this.flowerGif = flowerGif; + this.collected = false; + this.speed = 3; + } + + collidesWith(charX, charWidth) { + let flowerWidth = this.flowerGif.width * 0.5; + return ( + this.x < charX + charWidth && + this.x + flowerWidth > charX + ); + } + + move(direction) { + if (direction < 0) { + this.x += this.speed; + } else { + this.x -= this.speed; + } + } + + render() { + if (!this.collected) { + image(this.flowerGif, this.x, this.y, this.flowerGif.width * 0.1, this.flowerGif.height * 0.1); + } + } + + isOffScreen() { + return this.x < -50; // Check if flower has moved off-screen to the left + } +} + +let timeSinceLastFlower = 0; +let flowerInterval = 450; + +function updateAndRenderFlowers(rain, flowers, flowerGif, direction) { + if (!rain.isRaining) { + timeSinceLastFlower++ + + if (flowers.length === 0 && timeSinceLastFlower >= flowerInterval) { + flowers.push(new Flower(flowerGif)); + timeSinceLastFlower = 0 + flowerInterval = random(100, 240) + } + } + + for (let i = flowers.length - 1; i >= 0; i--) { + let flower = flowers[i]; + flower.move(direction); + flower.render(); + + // Remove flowers that have gone off-screen + if (flower.isOffScreen()) { + flowers.splice(i, 1); + } + } +} + +function updateScoreFlower(charX, charWidth) { + for (let i = flowers.length - 1; i >= 0; i--) { + if (flowers[i].collidesWith(charX, charWidth)) { + flowers.splice(i, 1); + flowers.collected = true + return score += 2; + } + else { + return score + } + } + return score +} \ No newline at end of file diff --git a/background/rain.js b/background/rain.js index f866971..b9dc231 100644 --- a/background/rain.js +++ b/background/rain.js @@ -57,12 +57,12 @@ function updateScoreRain(rain, charX, charWidth, showUmbrella, score, currentTim let collided = collidesWith(rain, charX, charWidth) if (showUmbrella) { - return score; // No score decrease if umbrella is shown + return score; } if (collided && rain.isRaining && !showUmbrella && currentTime - lastScoreDecrease >= scoreDecreaseInterval) { lastScoreDecrease = currentTime; - return Math.max(0, score - 1); // Ensure score doesn't go below 0 + return Math.max(0, score - 1); } else { return score diff --git a/index.html b/index.html index 4388800..be0e23f 100644 --- a/index.html +++ b/index.html @@ -10,6 +10,7 @@
+ diff --git a/sketch.js b/sketch.js index dd43550..5b3092e 100644 --- a/sketch.js +++ b/sketch.js @@ -1,4 +1,4 @@ -let stillFrame, runningGif, rainGif, grainImage, umbrellaImage;; +let stillFrame, runningGif, rainGif, grainImage, umbrellaImage, flowerImg; let speed = 5; let direction = 0; // -1 for left, 0 for still, 1 for right let charX; @@ -23,6 +23,9 @@ let showUmbrella = false; // Score System let score = 10; +// Flowers +let flowers = []; + function preload() { stillFrame = loadImage('./static/stillFrame.png'); runningGif = loadImage("./static/runFrame.gif"); @@ -32,6 +35,7 @@ function preload() { cloudGif = loadImage("./static/cloud.gif"); grainImage = loadImage('./static/grainAttempt2.jpeg'); umbrellaGif = loadImage('./static/umbrella.gif'); + flowerGif = loadImage('./static/flower.gif'); } function setup() { @@ -120,9 +124,13 @@ function draw() { // Update and display clouds updateAndRenderClouds(clouds, direction !== 0, direction) + // Update and display flowers + updateAndRenderFlowers(rain, flowers, flowerGif, direction); + // Update and display Score displayScore() score = updateScoreRain(rain, charX, stillFrame.width * 0.5, showUmbrella, score, millis()) + score = updateScoreFlower(charX, stillFrame.width * 0.2); // Display character push(); diff --git a/static/flower.gif b/static/flower.gif new file mode 100644 index 0000000..42bc441 Binary files /dev/null and b/static/flower.gif differ