วันพุธที่ 7 พฤษภาคม พ.ศ. 2557

Animation piano

Animation piano
คำสั่ง

float x, xa, xb, xc;
float r,g,b;

void setup() {
  size(500, 500);
  background(255, 255, 255, 255);
  frameRate(5);
}

void draw_piano() {
  fill(0); //color black
  rect(90, 60, 330, 105); //draw body piano color black
  fill(255); //color white
  rect(100, 100, 10, 60); //draw key piano color white
  rect(110, 100, 10, 60);
  rect(120, 100, 10, 60);
  rect(130, 100, 10, 60);
  rect(140, 100, 10, 60);
  rect(150, 100, 10, 60);
  rect(160, 100, 10, 60);
  rect(170, 100, 10, 60);
  rect(180, 100, 10, 60);
  rect(190, 100, 10, 60);
  rect(200, 100, 10, 60);
  rect(210, 100, 10, 60);
  rect(220, 100, 10, 60);
  rect(230, 100, 10, 60);
  rect(240, 100, 10, 60);
  rect(250, 100, 10, 60);
  rect(260, 100, 10, 60);
  rect(270, 100, 10, 60);
  rect(280, 100, 10, 60);
  rect(290, 100, 10, 60);
  rect(300, 100, 10, 60);
  rect(310, 100, 10, 60);
  rect(320, 100, 10, 60);
  rect(330, 100, 10, 60);
  rect(340, 100, 10, 60);
  rect(350, 100, 10, 60);
  rect(360, 100, 10, 60);
  rect(370, 100, 10, 60);
  rect(380, 100, 10, 60);
  rect(390, 100, 10, 60);
  rect(400, 100, 10, 60);
}

void random_keyPiano(int newX, int newxa, int newxb, int newxc) {
  //random color
  r = random(255); 
  g = random(255);
  b = random(255);
  //draw random key piano and color
  fill(r,g,b);
  rect(newX, 100, 10, 60); 
  fill(b,g,r);
  rect(newxa, 100, 10, 60);
  fill(g,r,b);
  rect(newxb, 100, 10, 60);
  fill(r,b,g);
  rect(newxc, 100, 10, 60);

//draw key piano color black
  fill(0);
  rect(107, 100, 6, 35);
  rect(117, 100, 6, 35);
  rect(137, 100, 6, 35);
  rect(147, 100, 6, 35);
  rect(157, 100, 6, 35);
  rect(177, 100, 6, 35);
  rect(187, 100, 6, 35);
  rect(207, 100, 6, 35);
  rect(217, 100, 6, 35);
  rect(227, 100, 6, 35);
  rect(247, 100, 6, 35);
  rect(257, 100, 6, 35);
  rect(277, 100, 6, 35);
  rect(287, 100, 6, 35);
  rect(297, 100, 6, 35);
  rect(317, 100, 6, 35);
  rect(327, 100, 6, 35);
  rect(347, 100, 6, 35);
  rect(357, 100, 6, 35);
  rect(367, 100, 6, 35);
  rect(387, 100, 6, 35);
  rect(397, 100, 6, 35);
}

void draw() {

  x = random(100, 400);
  xa = random(100, 400);
  xb = random(100, 400);
  xc = random(100, 400);

  int newX = int(x);
  int newxa = int(xa);
  int newxb = int(xb);
  int newxc = int(xc);

  int modx = newX % 10;
  int modxa = newxa % 10;
  int modxb = newxb % 10;
  int modxc = newxc % 10;

  draw_piano();
  random_keyPiano(newX-modx, newxa-modxa, newxb-modxb, newxc-modxc);
}

จากคำสั่ง เมื่อรันโปรแกรมจะได้รูปเปียโน และที่คีย์จะมีการเปลี่ยนสีไปเรื่อยๆโดยการสุ่มสี ในโปรแกรมจะมีการจำลองเหมือนการกดคีย์ที่ละ 4 คีย์โดยการสุ่มเลข 100-400 เพราะรูปที่วาดไว้เริ่มที่ 100 - 400 และเมื่อสุ่มค่าได้แล้วนำค่ามา mod กับ 10 และเรียกใช้ฟังก์ชั่น random_piano โดยส่งค่าที่สุ่มได้ ลบกับค่าที่ mod ได้ก็ได้ค่าที่อยู่ในช่วง 100-400 โดยไม่มีเศษในหลักหน่วย






ไม่มีความคิดเห็น:

แสดงความคิดเห็น