Livvy’s mini project 6

Raining

https://editor.p5js.org/wl3223/sketches/_GJirtgj5

 

Brief Description and Concept

In this project, the interactive sketch showcases a serene scene of ripples and rain. The canvas starts with a set of ripples that expand over time and raindrops that fall from the top of the canvas. Users can interact with the canvas in two ways:

  1. Clicking anywhere on the canvas creates a new ripple at the mouse’s location.
  2. Pressing the ‘R’ key intensifies the rain, adding more raindrops to the scene.

 

Demo/Visual Documentation

 

Coding

I have struggled with how to replace the ripples and they exceed a certain width. I wrote the code in the Ripple class but failed. Finally, I used the if condition in the for loop and put splice() and push() together. Here is the final code I achieved.

  for (let i = ripple.length - 1; i >= 0; i--) {// loop backward to avoid skipping elements after splicing

    ripple[i].update();

    ripple[i].display();

    if (ripple[i].w > 100) {

      ripple.splice(i, 1); // remove the ripple if its width is greater than 100

      ripple.push(new Ripple(random(width), random(200, height))); // add a new ripple

    }

  }

     

Reflection

  1. What is Object-Oriented Programming (OOP), a Class, and an Instance of the Class?
  • OOP is a programming paradigm based on the concept of “objects.” These objects can contain data, often known as properties, and code, often known as methods. OOP makes it easier to draw and structure complex code.
  • A class is a blueprint or template for creating objects. It defines a set of attributes (variables) and methods (functions) that the created objects can have. 
  • An instance is a specific realization of any object. When you create an object from a class, that object is an instance of that class. It’s like having a blueprint (class) for a house, and when you build a house using that blueprint, the house is the instance.

 

  1. Discuss the effectiveness of OOP. When is OOP useful? In what way can it be utilized?

To start with, with regard to modularity, OOP allows for the division of a complex problem into smaller, manageable modules or objects. What’s more, classes can be reused in different parts of an application or even in different applications thanks to its reusability. 

 

  1. Describe the objects you have created. What properties and methods were implemented? What kind of behaviors did you create by manipulating them?

I have created two classes: Ripple and Rain

1). Ripple Class:

  • Properties: x, y (position), w, h (width and height), sh (speed).
  • Methods: display and update (to increase the size of the ripple over time).
  • Behaviors: Ripples grow in size over time, and when they exceed a certain width, they are replaced by new smaller ripples.

2). Rain Class:

  • Properties: x, y (position), length, speed.
  • Methods: display and update (to move the raindrop downwards).
  • Behaviors: Raindrops fall from the top of the canvas, and when they reach the bottom, they reappear at the top, simulating continuous rainfall.
  1. Please share if there is any challenging and confusing aspect(s) in the OOP concepts and techniques.

The logic in OOP is challenging for me. I am struggling with the nested for loop and conditions. Also, it’s hard to tell which part should I write the code(should it be in draw function or in the class?)

Leave a Reply

Your email address will not be published. Required fields are marked *