code.club

標題: 類別加參數及陣列的用法 [打印本頁]

作者: enter    時間: 2017-1-17 23:30
標題: 類別加參數及陣列的用法
class Ball {

  int x=150;
  int y = 250;
  int r = 50;
  float speedx = random(-2, 2);
  float speedy = random(-2, 2);
  Ball() {
  }

  Ball(int a, int b, int c) {
    x = a;
    y = b;
    r = c;
  }

  void draw() {
    ellipse(x, y, r, r);
    x+=speedx;
    y+=speedy;
  }
};



Ball ball1;
Ball ball2;
Ball[] ball3 = new Ball[5];
Ball[] ball4 = new Ball[5];

void setup() {
  size(800, 600);
  ball1 = new Ball();
  ball2 = new Ball(400, 400, 100);
  for (int a=0; a<5; a++)
    ball3[a] = new Ball();
  for (int a =0; a<5; a++)
    ball4[a] = new Ball(int(random(width)), int(random(height)), int(random(10, 20)));
}

void draw() {
  background(255);
  ball1.draw();
  ball2.draw();
  for (int a=0; a<5; a++) ball3[a].draw();
  for (int a=0; a<5; a++) ball4[a].draw();
}





歡迎光臨 code.club (https://code.club/) Powered by Discuz! X3.2