Skip to content

Commit

Permalink
i added the flower, the score is a bit buggy but need to fix - i also…
Browse files Browse the repository at this point in the history
… about to add music and then a ending and start screen
  • Loading branch information
johyunjihyunji committed Oct 15, 2024
1 parent 1a0913d commit 04470cb
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 3 deletions.
Binary file modified .DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# norainnoflower
Art 173 Game Design Midterm Project

run python3 -m http.server to play locally
play at http://localhost:8000/
75 changes: 75 additions & 0 deletions background/flower.js
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions background/rain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<body>
<main>
</main>
<script src="./background/flower.js"></script>
<script src="./background/cloud.js"></script>
<script src="./background/grass.js"></script>
<script src="./background/rain.js"></script>
Expand Down
10 changes: 9 additions & 1 deletion sketch.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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");
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
Binary file added static/flower.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 04470cb

Please sign in to comment.