Labels

Tuesday, 22 May 2012

DSDN 142 - ArrayList code after lecture. Added to my code.

Here is my 'ArrayList' code that I worked on during tutorial :

float A = 50;
float ellipsex = 0;
float ellipsey = 0;

ArrayList Circles;

void setup() {
  size(500, 500);
  smooth();

  Circles = new ArrayList();
 
  for(int i = 0; i < 150; i++){
  Circle myCircle = new Circle(random(0,width),random(0,200));
  Circles.add(myCircle);
 }
}

void draw() {
  background(0);


  if (mousePressed == true) {
    ellipse(ellipsex, ellipsey, A, A);
    ellipsex = ellipsex + ((mouseX-ellipsex)/20.0);
    ellipsey = ellipsey + ((mouseY-ellipsey)/20.0);
  }

  if (mousePressed == true) {
    fill(255, 0, 0);
  }
  else {
    fill(255);
  }

for(int i = 0; i < 150; i++){
Circle myCircle = (Circle) Circles.get(i);
myCircle.play();
 }
}

class Circle {

  float x = 0;
  float y = 0;
  float speedX = 3;
  float speedY = 0.2;


  Circle(float xx, float yy) {

    x = xx;
    y = yy;
  }

  void play() {
    Thecircle();
    movement();
    bouncing();
    force();
  }

  void force() {
    speedY = speedY + 0.5;

  }
  void bouncing() {
    if (x > width) {
      speedX = speedX * -1;
    }
    if (x < 0) {
      speedX = speedX * -1;
    }
    if (y > height) {
      speedY = speedY * -1;
    }
    if (y < 0) {
      speedY = speedY * -1;
    }
  }

  void movement() {
    x = x + speedX;
    y = y + speedY;
  }

  void Thecircle() {
    ellipse(x, y, 20, 20);
  }

}  

Nowz I can haz many circlezzzz!!!!

    Here are two images of my project so far with ArrayLists and the one on the left has 50 circles while the one on the left has 150. I can have as many as i want to have obvoiusly but just wanted to show you some images of it :).

No comments:

Post a Comment