Module 5 - Classes and Objects

Summary

  • Object Oriented Programming

Object Oriented Programming

  • Basic Classes and Objects

    • Creating Classes: Define custom objects with properties and methods.

      class Ball {
          constructor(x, y, r,col) {
              this.x = x;
              this.y = y;
              this.r = r;
              this.col = col;
          }
      
          display() {
              fill(this.col);
              ellipse(this.x, this.y, this.r * 2, this.r * 2);
          }
      }
      
      function setup() {
          createCanvas(400, 200);
          let ball = new Ball(200, 100, 50, color(255,0,0));
          ball.display();
      }
      

    • Adding Methods: Methods define behavior for objects

      class Ball {
          constructor(x, y, r) {
              this.x = x;
              this.y = y;
              this.r = r;
          }
      
          move() {
              this.x += random(-5, 5);
              this.y += random(-5, 5);
          }
      
          display() {
              ellipse(this.x, this.y, this.r * 2, this.r * 2);
          }
      }
      
      let ball;
      
      function setup() {
          createCanvas(400, 200);
          ball = new Ball(200, 100, 50);
      }
      
      function draw() {
          background(220);
          ball.move();
          ball.display();
      }