GPN8:Processing-Workshop

aus dem Wiki des Entropia e.V., CCC Karlsruhe
Version vom 16. Juni 2009, 11:19 Uhr von Ralf (Diskussion | Beiträge) (Kategorie hinzugefuegt)

CAR

int anzahl_autos = 50;
Car[] autos = new Car[anzahl_autos];
void setup(){
  size(300,400);
  for(int i = 0; i < anzahl_autos; i++) {
    autos[i] = new Car(color(0,1,1),random(100),random(height),random(1,5));
  }
}
void draw() {
  background(128);
  for(int i = 0; i < anzahl_autos; i++) {
    autos[i].move();
    autos[i].display();
  }
}

--- TEIL2 ---

class Car { 
  color c;
  float xpos;
  float ypos;
  float xspeed;
  // The Constructor is defined with arguments.
  Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
    c = tempC;
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }
  void display() {
    stroke(0);
    fill(c);
    rectMode(CENTER);
    rect(xpos,ypos,20,10);
  }
  void move() {
    xpos = xpos + xspeed;
    if (xpos > width) {
      xpos = 0;
    }
  }
}