วันอาทิตย์ที่ 6 กันยายน พ.ศ. 2558

Lab 3 : Battery charged

int scale;
int energy=1;
int colour=0;
void setup() {
  size(300, 300);
}
void draw() {
  background(255);
  frameRate(10);
  //call function
  draw_battery(65, 80);
  draw_positive(245, 95);
  draw_minus(5, 110);
  draw_energy();
  percent_battery(100, 250);
}
void draw_battery(int x, int y) {
  int batW = 160;
  int batH = batW/2;
  //battery
  fill(255);
  rect(x, y, batW, batH);
  //battery's polar
  fill(0);
  rect(batW+x, (batH/3)+y, 10, batH/3);
  //energy
  fill(colour);
  rect(x, y, scale, batH);
}
void draw_positive(int x, int y) {
  float sizePosi = 50;
  //positive
  fill(0);
  //vertical
  rect(x+(sizePosi/3), y, sizePosi/3, sizePosi);
  //horizon
  rect(x, y+(sizePosi/3), sizePosi, sizePosi/3);
}
void draw_minus(int x, int y ) {
  int sizeMinus = 50;
  fill(0);
  rect(x, y, sizeMinus, sizeMinus/3);
}
void text_full(int x, int y) {
  fill(#00FF00);
  textSize(30);
  text("FULL BATTERY", x, y);
}
void text_low(int x, int y) {
  fill(#DD0000);
  textSize(30);
  text("LOW BATTERY", x, y);
}
void draw_energy () {
  //energy
  scale = scale+energy;
  if (scale==160) {
    energy = 0;
    energy = energy - 1;
    text_full(60, 40);
  }
  if (scale == 0 && energy < 0) {
    energy = 0;
    energy = energy + 1;
    text_low(60, 40);
  }
}
void percent_battery(int x, int y) {
  //percent battery
  int perBat = (scale*100)/160;
  if (perBat >= 15) {
    colour = #00FF00;
  } else if (perBat <= 5) {
    colour = #DD0000;
  }
  fill(0);
  textSize(50);
  text((int)(perBat)+"%", x, y);
}



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

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