How to make a game with many different objects (ArrayList)

here is an example

using interface and implements


// demo for interface and implements
// Interface allows to bring different kinds of objects in one ArrayList

ArrayList<BasicItem3D> list = new ArrayList(); 

void setup() {
  size(950, 650, P3D);

  for (int x=0; x<7; x++) {
    for (int y=0; y<7; y++) {
      if (random(100) > 50) 
        list.add(new ClassBox( 300.0+x*74, 100.0+y*74, random(-150, 150)));
      else
        list.add(new ClassSphere( 300.0+x*74, 100.0+y*74, random(-150, 150)));
    }//for
  }//for
  //
}//func

void draw() {
  background(0);
  lights();

  for (BasicItem3D item : list) {
    item.display();
  }//for
}//func

//=============================================================================

interface BasicItem3D {
  void display();
  void move();
}//interface

//=============================================================================

class ClassSphere implements BasicItem3D {
  PVector pos   = new PVector(0, 0, 0); 

  // constr 
  ClassSphere(float x, float y, float z) {
    pos.set(x, y, z);
  }// constr 

  void display() {
    noStroke();
    fill(255, 0, 0);
    pushMatrix();
    translate(pos.x, pos.y, pos.z);
    sphere (39);
    popMatrix();
  } 

  void move() {
  }
  //
}//class

//=============================================================================

class ClassBox implements BasicItem3D {
  PVector pos   = new PVector(0, 0, 0); 

  // constr 
  ClassBox(float x, float y, float z) {
    pos.set(x, y, z);
  }// constr 

  void display() {
    stroke(0);
    fill(200);
    pushMatrix();
    translate(pos.x, pos.y, pos.z);
    box (39);
    popMatrix();
  } 

  void move() {
  }
  //
}//class
//
2 Likes