28 กันยายน 2558

Lab5 - convert a number from base 10 to base 2

def setup():
   base10 = 19
   base2 = ""
   while (base10 > 0 ):
      base2 = str(base10%2)+base2
      base10 = base10//2
      print(base2)
   print(base2)
setup()

21 กันยายน 2558

Lab4x Loan payment

def setup():
  monthly_loan_payment(5000, 12, 1);

def monthly_loan_payment(loan_amount, interest_rate, loan_term):
   ratepermonth = (interest_rate/100)/12
   paypermonth = loan_amount*(ratepermonth/(1-pow(1+ratepermonth, -(loan_term*12))))#M=P*(J/(1-(1+J)^-n)
   unpaid = loan_amount
   total_interest = 0
   month = 1
   print("Monthly Loan Payment");
   print("Payment No.  Interest   Principal   Unpaid Balance   Total Interest")
   while (month <= (loan_term*12)):
      interest = ratepermonth*unpaid
      total_interest+=interest
      principal = paypermonth-interest
      unpaid = abs(unpaid-principal)
      print("   ","%0.2d"%month,end="")
      print("        ","$","%5.2f"% interest,end="",sep="")
      print("     ","$","%6.2f"% principal,end="",sep="")
      print("       ","$","%7.2f"% unpaid,end="",sep="")
      print("         ","$","%6.2f"% total_interest,sep="")
      month+=1

setup()

Lab4x Multiplication table

def setup():
   multi_table = 25
   max_multiplier = 12
   multiplier=1
   print("Multiplication Table","'",multi_table,"'");
   while (multiplier <= max_multiplier):
      result = multi_table*multiplier
      print(multi_table,"x",multiplier,"=",result)
      multiplier+=1
 
setup()

Lab4x Sum of Prime Number

def setup():
   max_val = 20
   prime_num = 2
   sum_prime = 0
   print("Sum of Prime number from 1 to",max_val,"(",end="")
   while (prime_num <= max_val):
      if (check_prime_num(prime_num)):
         print(prime_num,",",sep="",end="")
         sum_prime += prime_num
      prime_num+=1
   print(")")
   print("'",sum_prime,"'",sep="")
 

def check_prime_num(p_num):
   divisor = 2
   while (divisor<p_num):
      if (p_num%divisor==0):
         return False
      divisor+=1
   return True

setup()

20 กันยายน 2558

Lab4x Sum of Integers

def setup():
   max_val = 10
   count=0
   result=0
   while (count <= max_val):
      result += count
      count+=1
   print("Sum of Integers from 1 to",max_val,"=",end="")
   print(" '",result,"'",sep="")
setup()

Lab4x Leap year

def setup():
  year = 2015
  if (year%400==0):
    print(year," is leap year")
  elif (year%100==0):
    print(year," isn't leap year")
  elif (year%4==0):
    print(year," is leap year")
  else:
    print(year," isn't "+"leap year")
             
setup()

Lab4x Grade

def setup():
   score = 49
   print("Your score =",score)
   if (score>=80):
      print("Grade A ")
   elif (score>=70):
      print("Grade B ")
   elif (score>=60):
      print("Grade C ")
   elif (score>=50):
      print("Grade D ")
   else:
      print("Grade F ")

setup()

14 กันยายน 2558

Lab4x BMI

def setup():
#//----- Define value -----//
   weight_ = 68
   height_ = 174

#//----- Calculate -----//
   bmi = format(cal_bmi(weight_,height_),'.2f')

#//----- Show Value ----//
   print('weight_ = ',weight_,'kg')
   print('height_ = ',height_,'cm')
   print('Body mass index(BMI) = ', bmi)

def cal_bmi(w,h):#//Create function BMI calculation
   val_bmi = w/((h/100)*(h/100))#//Formula of BMI
   return val_bmi

setup()

Lab4x Circumference

def setup():
   radius = 10
   circumference = format(cal_cir(radius),'.2f')
   area = format(cal_area(radius),'.2f')
   print ("Circumference = ",circumference,"cm")
   print ("Area =",area, "cm2")

#Create function circumference calculation  
def cal_cir(r):
   val_cir = (22/7)*2*r
   return val_cir

#Create function area calculation
def cal_area(r):
   val_area = (22/7)*r*r
   return val_area

setup()

Lab4 Doraemon book

void setup() {
  size(800, 800);
  background(#F6B0D8);
}

int move_X = 0;
int move_Y = 0;
void draw() {
  int X = 0;
  int Y = 0;
  int max_book = 4;
  int pre_book = 1;
  background(#F6B0D8);
  while (pre_book<=max_book) {
    draw_book(move_X+X, move_Y+Y);
    Y+= 364;
    if (pre_book%2==0) {
      X=304;
      Y=0;
    }
    pre_book++;
  }
  if (key=='w'||key=='W') {
    move_Y-=2;
    if (move_Y<=-442) {
      move_Y=-442;
    }
  }
  if (key=='s'||key=='S') {
    move_Y+=2;
    if (move_Y>=358) {
      move_Y=358;
    }
  }
  if (key=='a'||key=='A') {
    move_X-=2;
    if (move_X<=-402) {
      move_X=-402;
    }
  }
  if (key=='d'||key=='D') {
    move_X+=2;
    if (move_X>=397) {
      move_X=397;
    }
  }
}

void draw_book(int pos_x, int pos_y) {

  strokeJoin(ROUND);
  //----- front page -----//
  strokeWeight(2);
  fill(#323C5D);
  rect(pos_x+100, pos_y+100, 270, 340);

  //----- behind doraemon background -----//
  noStroke();
  fill(#FCE314);
  rect(pos_x+125, pos_y+220, 220, 205);

  //----- name book background -----//
  fill(255);
  rect(pos_x+125, pos_y+130, 220, 50);
  //----- name book -----//
  fill(0);
  textSize(35);
  text("DORAEMON", pos_x+133, pos_y+167);

  //----- book page -----//
  strokeWeight(2);
  stroke(0);
  fill(255);
  quad(pos_x+100, pos_y+100, pos_x+130, pos_y+80, pos_x+400, pos_y+80, pos_x+370, pos_y+100);
  quad(pos_x+370, pos_y+100, pos_x+400, pos_y+80, pos_x+400, pos_y+420, pos_x+370, pos_y+440);
  strokeWeight(1);
  line(pos_x+106, pos_y+96, pos_x+376, pos_y+96);
  line(pos_x+112, pos_y+92, pos_x+382, pos_y+92);
  line(pos_x+118, pos_y+88, pos_x+388, pos_y+88);
  line(pos_x+124, pos_y+84, pos_x+394, pos_y+84);
  line(pos_x+376, pos_y+96, pos_x+376, pos_y+436);
  line(pos_x+382, pos_y+92, pos_x+382, pos_y+432);
  line(pos_x+388, pos_y+88, pos_x+388, pos_y+428);
  line(pos_x+394, pos_y+84, pos_x+394, pos_y+424);

  //----- Doraemon -----//
  strokeWeight(3);
  stroke(#325D87);
  //----- face -----//
  fill(#4F81BC);
  ellipse(pos_x+235, pos_y+320, 200, 175);
  fill(255);
  ellipse(pos_x+235, pos_y+334, 140, 125);

  //----- eyes -----//
  fill(255);
  ellipse(pos_x+215, pos_y+274, 37, 50);
  ellipse(pos_x+255, pos_y+274, 37, 50);
  fill(#325D87);
  ellipse(pos_x+220, pos_y+279, 8, 8);
  strokeWeight(4);
  arc(pos_x+248, pos_y+281, 6, 6, PI, TWO_PI);
  strokeWeight(3);

  //----- nose -----//
  fill(#FE0000);
  ellipse(pos_x+235, pos_y+301, 20, 20);

  //----- mounth -----//
  noFill();
  arc(pos_x+235, pos_y+350, 100, 50, 0, PI);
  line(pos_x+235, pos_y+312, pos_x+235, pos_y+375);

  //----- Whiskers -----//
  line(pos_x+220, pos_y+320, pos_x+177, pos_y+315);
  line(pos_x+220, pos_y+325, pos_x+175, pos_y+325);
  line(pos_x+220, pos_y+330, pos_x+177, pos_y+335);

  line(pos_x+250, pos_y+320, pos_x+293, pos_y+315);
  line(pos_x+250, pos_y+325, pos_x+295, pos_y+325);
  line(pos_x+250, pos_y+330, pos_x+293, pos_y+335);

  //----- collar -----//
  fill(#FD0000);
  rect(pos_x+185, pos_y+395, 100, 13);

  //----- bell -----//
  fill(#FCBE0C);
  ellipse(pos_x+235, pos_y+394, 20, 20);

  //----- hands -----//
  fill(255);
  ellipse(pos_x+180, pos_y+395, 30, 28);
  ellipse(pos_x+290, pos_y+395, 30, 28);
}

Lab4 Capt

float move = 0;
float pos_x_shield = 370;
float speed = 2;
int max_shield = 4;
boolean start_fw = true;
boolean start_bw;

void setup() {
  size(600, 600);
  background(#0D0D0D);
}

void draw() {
  background(#0D0D0D);
  int Y = 0;
  int X = 0;
  int pre_shield =1;
  textSize(550);
  text("A", 100, 500);// A font
 
  while (pre_shield<=max_shield) {
    draw_capt(move, Y, X);
    X+=250;
    if (pre_shield%2==0) {
      Y=-200;
      X=0;
    }
    if (pre_shield%4==0) {
      Y=200;
      X=0;
    }
    pre_shield++;
  }
 
  if (start_fw == true) {
    move +=speed;
    pos_x_shield +=speed;
    if (pos_x_shield >= 485) {
      start_fw = false;
      start_bw = true;
    }
  }

  if (start_bw == true) {
    move -=speed;
    pos_x_shield -=speed;
    if (pos_x_shield <= -140) {
      start_fw = true;
      start_bw = false;
    }
  }
 
  fill(255);
  textSize(15);
  text("speed "+speed, 20, 20);

  //----- title -----//
  textSize(30);
  text("CAPTAIN AMERICA", 162, 570);
}

void draw_capt(float pos_x, float pos_y, float x) {
  noStroke();
  fill(255);

  //----- Shield -----//
  fill(#EE0000);
  ellipse(pos_x_shield+x, pos_y+300, 250, 250);
  fill(255);
  ellipse(pos_x+370+x, pos_y+300, 210, 210);
  fill(#EE0000);
  ellipse(pos_x+370+x, pos_y+300, 170, 170);
  fill(#000080);
  ellipse(pos_x+370+x, pos_y+300, 130, 130);

  //----- Star -----//
  beginShape();
  fill(255);
  //fill((random(0, 255)), (random(0, 255)), (random(0, 255)));
  vertex(pos_x+370+x, pos_y+243);
  vertex(pos_x+382.5+x, pos_y+280.5);
  vertex(pos_x+420+x, pos_y+280.5);
  vertex(pos_x+395+x, pos_y+305.5);
  vertex(pos_x+407.5+x, pos_y+343);
  vertex(pos_x+370+x, pos_y+318);
  vertex(pos_x+332.5+x, pos_y+343);
  vertex(pos_x+345+x, pos_y+305.5);
  vertex(pos_x+320+x, pos_y+280.5);
  vertex(pos_x+357.5+x, pos_y+280.5);
  endShape(CLOSE);
}
void keyPressed() {
  if (keyCode==UP)speed+=0.5;
  if (keyCode==DOWN)speed-=0.5;
  if (speed<=2)speed=2;
  if (speed>=30)speed=30;
}
void mousePressed() {
  if (mouseButton == LEFT) {
    max_shield++;
    {
      if (max_shield>=6)max_shield=6;
    }
  } else if (mouseButton == RIGHT) {
    max_shield--;
    {
      if (max_shield<=1)max_shield=1;
    }
  }
}

Lab4 Flock birds

float wing_y;
float bird_y;
int size_draw = 40;
boolean wing_up;
boolean wing_down;
int max_bird = 3;

void setup() {
  size(600, 600);
  background(255);
  mouseX=width/2;
  mouseY=height/2;
  wing_up = true;
}

void draw() {
  int present_bird = 1;
  int X = 0;
  int Y = 0;
  background(255);

  if (mouseY<size_draw*2) {
    mouseY=size_draw*2;
  }
  if (mouseX<size_draw) {
    mouseX = size_draw;
  }

  while (present_bird <= max_bird) {
    draw_bird(mouseX+X, mouseY+Y, size_draw, bird_y);
    X += size_draw*3;
    if (present_bird%4==0) {
      X=0;
      Y-=size_draw*1.1;
    }
    present_bird++;
  }

  if (wing_up == true) {
    wing_y-= size_draw*0.15;
    bird_y-= size_draw*0.01;
    if (wing_y <= -(size_draw*0.7)) {
      wing_up = false;
      wing_down = true;
    }
  }

  if (wing_down == true) {
    wing_y+= size_draw*0.15;
    bird_y+= size_draw*0.01;
    if (wing_y >= (size_draw*0.7)) {
      wing_up = true;
      wing_down = false;
    }
  }
}

void draw_bird(float mouse_x, float mouse_y, float s_draw, float bird_y) {
  fill(#D30327);
  strokeWeight(s_draw*0.2);
  line(mouse_x, mouse_y-(s_draw*0.5), mouse_x-(s_draw*1.3), mouse_y+wing_y-(s_draw*0.5));//left wing
  line(mouse_x, mouse_y-(s_draw*0.5), mouse_x+(s_draw*1.3), mouse_y+wing_y-(s_draw*0.5));//right wing
  strokeWeight(s_draw*0.05);
  ellipse(mouse_x, mouse_y-(s_draw*0.5)+bird_y, s_draw, s_draw);//body
  strokeWeight(1);
  fill(255);
  ellipse(mouse_x-(s_draw*0.22), mouse_y-(s_draw*0.6)+bird_y, s_draw*0.3, s_draw*0.35);//left white eye
  ellipse(mouse_x+(s_draw*0.22), mouse_y-(s_draw*0.6)+bird_y, s_draw*0.3, s_draw*0.35);//right white eye
  fill(0);
  ellipse(mouse_x-(s_draw*0.22), mouse_y-(s_draw*0.6)+bird_y, s_draw*0.1, s_draw*0.1);//left black eye
  ellipse(mouse_x+(s_draw*0.22), mouse_y-(s_draw*0.6)+bird_y, s_draw*0.1, s_draw*0.1);//right black eye
  fill(#F9BC09);
  triangle(mouse_x-(s_draw*0.17), mouse_y-(s_draw*0.35)+bird_y, mouse_x+(s_draw*0.17), mouse_y-(s_draw*0.35)+bird_y, mouse_x, mouse_y-(s_draw*0.05)+bird_y); //mounth
}

void keyPressed() {
  if (keyCode==UP) {
    size_draw++;
    if (size_draw>=90)size_draw=90;
  }
  if (keyCode==DOWN) {
    size_draw--;
    if (size_draw<=20)size_draw=20;
  }
}
void mousePressed() {
  if (mouseButton == LEFT) {
    max_bird++;
  } else if (mouseButton == RIGHT) {
    max_bird--;
    {
      if (max_bird<=1)max_bird=1;
    }
  }
}

Lab4 Balloon

int max_balloon = 4;
int size_draw = 50;
float Y = height;
void setup() {
  size(600, 600);
}


void draw() {
  int present_balloon = 1;
  float X = 0;
  background(0);
  if (Y < height*0.16) {
    fill(#00FD00);
  } else if (Y < height*0.33) {
    fill(#A0FA00);
  } else if (Y < height*0.5) {
    fill(#F2FB01);
  } else if (Y < height*0.66) {
    fill(#EFA200);
  } else if (Y < height*0.83) {
    fill(#EB6100);
  } else {
    fill(#D00022);
  }

  while (present_balloon <= max_balloon) {
    draw_balloon(X, Y);
    X += size_draw*1.2;
    present_balloon++;
  }

  Y-=2;
  if (Y<=(-size_draw*2))Y=height;
}

void draw_balloon(float pos_x, float pos_y) {
  stroke(#009CDA);
  strokeWeight(size_draw*0.05);
  float radius = size_draw;
  float string_length = size_draw*2;
  line(pos_x+(size_draw*0.6), pos_y, pos_x+(size_draw*0.6), pos_y+string_length);
  ellipse(pos_x+(size_draw*0.6), pos_y, radius, radius);
}

void keyPressed() {
  if (keyCode==UP) {
    size_draw++;
    if (size_draw>=90)size_draw=90;
  }
  if (keyCode==DOWN) {
    size_draw--;
    if (size_draw<=20)size_draw=20;
  }
}
void mousePressed() {
  if (mouseButton == LEFT) {
    max_balloon++;
  } else if (mouseButton == RIGHT) {
    max_balloon--;
    {
      if (max_balloon<=1)max_balloon=1;
    }
  }
}

Lab4 Loan Payment

void setup() {
  size(685, 500);
  background(0);
  fill(255);
  monthly_loan_payment(5000, 12, 1);
}
void monthly_loan_payment(float loan_amount, float interest_rate, int loan_term) {
  float ratepermonth = (interest_rate/100)/12;
  float paypermonth = loan_amount*(ratepermonth/(1-pow(1+ratepermonth, -(loan_term*12))));//M=P*(J/(1-(1+J)^-n))
  float unpaid = loan_amount;
  float total_interest = 0;
  int month = 1;
  textSize(25);
  text("Monthly Loan Payment", 205, 30);
  textSize(17);
  text("Payment No.        Interest        Principal        Unpaid Balance        Total Interest", 20, 70);
  int Y =110;
  while (month <= (loan_term*12)) {
    float interest = ratepermonth*unpaid;
    total_interest+=interest;
    float principal = paypermonth-interest;
    unpaid = abs(unpaid-principal);
    text(nf(month, 2), 60, Y);
    text("$"+nf(interest, 1, 2), 168, Y);
    text("$"+nf(principal, 1, 2), 272, Y);
    text("$"+nf(unpaid, 1, 2), 408, Y);
    text("$"+nf(total_interest, 3, 2), 576, Y);
    Y+=30;
    month++;
  }
}

Lab4 Sum of Prime number

void setup() {
  size(350, 100);
  background(0);
  int max_val = 10;
  int p_num =2;
  int sum=0;
  while (p_num <= max_val) {
    if (prime_number(p_num)) {
      sum += p_num;
      println(sum);
    }
    p_num++;
  }
  textSize(18);
  textAlign(CENTER);
  text("Sum of Prime number from 1 to "+max_val, width/2, height/2-20);
  text("'"+sum+"'", width/2, height/2+20);
}

boolean prime_number(int p_num) {
  boolean result = true;
  int divisor = 2;
  while (divisor<p_num) {
    if (p_num%divisor==0) {
      result = false;
      break;
    }
    result = true;
    divisor++;
  }
  return result;
}

Lab4 Multiplication Table

void setup() {
  size(250, 300);
  background(0);
  int multi_table = 25;
  int max_multiplier_value = 12;
  int x=1;
  textSize(18);
  text("Multiplication Table "+"'"+multi_table+"'", 10, 20);
  while (x <= max_multiplier_value) {
    int result = multi_table*x;
    text(multi_table+" x "+x+" = "+result, 10, 30+(20*x));
    x++;
  }
}

Lab4 Sum of Integers

void setup() {
  size(300, 100);
  background(0);
  int max_val = 99;
  int x=0;
  int sum=0;
  while (x <= max_val) {
    sum += x;
    x++;
  }
  textSize(18);
  textAlign(CENTER);
  text("Sum of Integers from 1 to "+max_val, width/2, height/2-20);
  text("'"+sum+"'", width/2, height/2+20);
}

9 กันยายน 2558

Lab3 Grade from Score

void setup() {
  size(300, 150);
  background(0);
  int score = 49;
  fill(255);
  textAlign(CENTER);
  textSize(25);
  text("Your score = "+score, width/2, height/2-20);
  if (score>=80) {
    text("Grade A ", width/2, height/2+20);
  } else if (score>=70) {
    text("Grade B ", width/2, height/2+20);
  } else if (score>=60) {
    text("Grade C ", width/2, height/2+20);
  } else if (score>=50) {
    text("Grade D ", width/2, height/2+20);
  } else {
    text("Grade F ", width/2, height/2+20);
  }
}


Lab3 Power of 10


void setup() {
size(300, 100);
background(0);
fill(255);
textAlign(CENTER);
textSize(25);
int number=6;
power_of_ten(number);
}

void power_of_ten(int num) {
if (num == 6) {
text("10^"+num+" = "+"Million", width/2, (height/2)+5);
} else if (num == 9) {
text("10^"+num+" = "+"'Billion'", width/2, (height/2)+5);
} else if (num == 12) {
text("10^"+num+" = "+"Trillion", width/2, (height/2)+5);
} else if (num == 15) {
text("10^"+num+" = "+"'Quadrillion'", width/2, (height/2)+5);
} else if (num == 18) {
text("10^"+num+" = "+"'Quintillion'", width/2, (height/2)+5);
} else if (num == 21) {
text("10^"+num+" = "+"'Sextillion'", width/2, (height/2)+5);
} else if (num == 30) {
text("10^"+num+" = "+"'Nonillion'", width/2, (height/2)+5);
} else if (num == 100) {
text("10^"+num+" = "+"'Googol'", width/2, (height/2)+5);
} else {
text("10^"+num+" = "+"No Word", width/2, (height/2)+5);
}
}




Lab3 Leap Year

void setup() {
  int year = 2015;
  size(270, 80);
  background(0);
  fill(255);
  textAlign(CENTER);
  textSize(25);

  if (year%400==0) {
    text(year+" is "+"leap year", width/2, height/2 );
  } else if (year%100==0) {
    text(year+" isn't "+"leap year", width/2, height/2 );
  } else if (year%4==0) {
    text(year+" is "+"leap year", width/2, height/2 );
  } else {
    text(year+" isn't "+"leap year", width/2, height/2 );
  }
}

Lab3 Flying bird

float wing_y;
int size_draw = 50;
boolean w_up;
boolean w_down;
void setup() {
  size(400, 500);
  background(255);
  mouseX=width/2;
  mouseY=height/2;
  w_up = true;
}

void draw() {
  background(255);
  draw_bird(mouseX, mouseY, size_draw);
  if (w_up == true) {
    if (mouseY<=100)wing_y-= size_draw*0.30;
    if (mouseY>100&&mouseY<=200)wing_y-= size_draw*0.20;
    if (mouseY>200&&mouseY<=300)wing_y-= size_draw*0.15;
    if (mouseY>300&&mouseY<=400)wing_y-= size_draw*0.10;
    if (mouseY>400&&mouseY<=500)wing_y-= size_draw*0.07;
    if (wing_y <= -(size_draw*0.5)) {
      w_up = false;
      w_down = true;
    }
  }
  if (w_down == true) {
    if (mouseY<=100)wing_y+= size_draw*0.30;
    if (mouseY>100&&mouseY<=200)wing_y+= size_draw*0.20;
    if (mouseY>200&&mouseY<=300)wing_y+= size_draw*0.15;
    if (mouseY>300&&mouseY<=400)wing_y+= size_draw*0.10;
    if (mouseY>400&&mouseY<=500)wing_y+= size_draw*0.07;
    if (wing_y >= (size_draw*0.5)) {
      w_up = true;
      w_down = false;
    }
  }
  if (keyCode==UP) {
    size_draw++;
    if (size_draw>=110)size_draw=110;
  }
  if (keyCode==DOWN) {
    size_draw--;
    if (size_draw<=50)size_draw=50;
  }
}

void draw_bird(int mouse_x, int mouse_y, int s_draw) {
  fill(#D30327);
  strokeWeight(s_draw*0.2);
  line(mouse_x, mouse_y-(s_draw*0.5), mouse_x-(s_draw*1.3), mouse_y+wing_y-(s_draw*0.5));
  line(mouse_x, mouse_y-(s_draw*0.5), mouse_x+(s_draw*1.3), mouse_y+wing_y-(s_draw*0.5));
  strokeWeight(s_draw*0.05);
  ellipse(mouse_x, mouse_y-(s_draw*0.5), s_draw, s_draw);
  strokeWeight(1);
  fill(255);
  ellipse(mouse_x-(s_draw*0.22), mouse_y-(s_draw*0.6), s_draw*0.3, s_draw*0.35);
  ellipse(mouse_x+(s_draw*0.22), mouse_y-(s_draw*0.6), s_draw*0.3, s_draw*0.35);
  fill(0);
  ellipse(mouse_x-(s_draw*0.22), mouse_y-(s_draw*0.6), s_draw*0.1, s_draw*0.1);
  ellipse(mouse_x+(s_draw*0.22), mouse_y-(s_draw*0.6), s_draw*0.1, s_draw*0.1);
  fill(#F9BC09);
  triangle(mouse_x-(s_draw*0.17), mouse_y-(s_draw*0.35), mouse_x+(s_draw*0.17),
    mouse_y-(s_draw*0.35), mouse_x, mouse_y-(s_draw*0.05));
}

Lab3 Martin (interaction)

int pos_x;
int pos_y;
color clr_x_sign;
color clr_positive_sign;
void setup(){
size (600,600);
background(0);
frameRate(30);
clr_x_sign = color(255);
clr_positive_sign = color(255);
}

void draw(){
draw_martin(pos_x,pos_y);
if(key=='w'||key=='W'){
pos_y-=2;
if(pos_y<=-33){ pos_y=-33; } } if(key=='s'||key=='S'){ pos_y+=2; if(pos_y>=230){
pos_y=230;
}
}
if(key=='a'||key=='A'){
pos_x-=2;
if(pos_x<=-56){ pos_x=-56; } } if(key=='d'||key=='D'){ pos_x+=2; if(pos_x>=55){
pos_x=55;
}
}
}

void draw_martin(int x , int y){
background(0);
//----- Frame -----//
noStroke();
rect(x+60,y+35,480,280,2); // Frame outside
fill(0);
rect(x+100,y+75,400,200,2);// Frame inside

//----- Positive Sign -----//
fill(clr_positive_sign);
rect(x+140,y+155,140,40,2);
rect(x+190,y+105,40,140,2);

//----- X Sign -----//
fill(clr_x_sign);
quad(x+349,y+105,x+460,y+216,x+431,y+245,x+320,y+134);
quad(x+431,y+105,x+460,y+134,x+349,y+245,x+320,y+216);

//----- Tiltle -----//
fill(255);
PFont font;
font = loadFont("Arial-Black-52.vlw");
textFont(font);
textSize(52);
text("MAR", x+65 , y+365);
text("IN", x+232 , y+365);
text("GARRI", x+313 , y+365);

//----- Positive Sign Tiltle -----//
rect(x+195,y+341,36,10);
rect(x+208,y+328,10,36);
//----- X Sign Tiltle -----//
quad(x+503,y+328,x+533,y+358,x+526,y+365,x+496,y+335);
quad(x+526,y+328,x+533,y+335,x+503,y+365,x+496,y+358);
}

void keyPressed(){
if(key=='z'||key=='Z'){
clr_x_sign = color(random(0,255),random(0,255),random(0,255));
}
if(key=='x'||key=='X'){
clr_positive_sign = color(random(0,255),random(0,255),random(0,255));
}
if(key=='r'||key=='R'){
clr_x_sign = color(255);
clr_positive_sign = color(255);
}
}




Lab3 Doraemon book (interaction)


int pos_x;
int pos_y;
color clr_bg_dora;

void setup() {
size(500, 500);
background(#F6B0D8);
frameRate(25);
clr_bg_dora = color(255);
}

void draw(){
draw_book(pos_x , pos_y);
if(key=='w'||key=='W'){
pos_y-=1;
if(pos_y<=-78){ pos_y=-78; } } if(key=='s'||key=='S'){ pos_y+=1; if(pos_y>=57){
pos_y=57;
}
}
if(key=='a'||key=='A'){
pos_x-=1;
if(pos_x<=-98){ pos_x=-98; } } if(key=='d'||key=='D'){ pos_x+=1; if(pos_x>=98){
pos_x=98;
}
}

}

void draw_book(int pos_x, int pos_y){
background(#F6B0D8);
strokeJoin(ROUND);
//----- front page -----//
strokeWeight(2);
fill(#323C5D);
rect(pos_x+100,pos_y+100,270,340);

//----- behind doraemon background -----//
noStroke();
fill(#FCE314);
rect(pos_x+125,pos_y+220,220,205);

//----- name book background -----//
fill(clr_bg_dora);
rect(pos_x+125,pos_y+130,220,50);
//----- name book -----//
fill(0);
textSize(35);
text("DORAEMON", pos_x+133, pos_y+167);

//----- book page -----//
strokeWeight(2);
stroke(0);
fill(255);
quad(pos_x+100,pos_y+100,pos_x+130,pos_y+80,pos_x+400,pos_y+80,pos_x+370,pos_y+100);
quad(pos_x+370,pos_y+100,pos_x+400,pos_y+80,pos_x+400,pos_y+420,pos_x+370,pos_y+440);
strokeWeight(1);
line(pos_x+106,pos_y+96,pos_x+376,pos_y+96);
line(pos_x+112,pos_y+92,pos_x+382,pos_y+92);
line(pos_x+118,pos_y+88,pos_x+388,pos_y+88);
line(pos_x+124,pos_y+84,pos_x+394,pos_y+84);
line(pos_x+376,pos_y+96,pos_x+376,pos_y+436);
line(pos_x+382,pos_y+92,pos_x+382,pos_y+432);
line(pos_x+388,pos_y+88,pos_x+388,pos_y+428);
line(pos_x+394,pos_y+84,pos_x+394,pos_y+424);

//----- Doraemon -----//
strokeWeight(3);
stroke(#325D87);
//----- face -----//
fill(#4F81BC);
ellipse(pos_x+235,pos_y+320,200,175);
fill(255);
ellipse(pos_x+235,pos_y+334,140,125);

//----- eyes -----//
fill(255);
ellipse(pos_x+215,pos_y+274,37,50);
ellipse(pos_x+255,pos_y+274,37,50);
fill(#325D87);
ellipse(pos_x+220,pos_y+279,8,8);
strokeWeight(4);
arc(pos_x+248,pos_y+281,6,6,PI,TWO_PI);
strokeWeight(3);

//----- nose -----//
fill(#FE0000);
ellipse(pos_x+235,pos_y+301,20,20);

//----- mounth -----//
noFill();
arc(pos_x+235,pos_y+350,100,50,0,PI);
line(pos_x+235,pos_y+312,pos_x+235,pos_y+375);

//----- Whiskers -----//
line(pos_x+220,pos_y+320,pos_x+177,pos_y+315);
line(pos_x+220,pos_y+325,pos_x+175,pos_y+325);
line(pos_x+220,pos_y+330,pos_x+177,pos_y+335);

line(pos_x+250,pos_y+320,pos_x+293,pos_y+315);
line(pos_x+250,pos_y+325,pos_x+295,pos_y+325);
line(pos_x+250,pos_y+330,pos_x+293,pos_y+335);

//----- collar -----//
fill(#FD0000);
rect(pos_x+185,pos_y+395,100,13);

//----- bell -----//
fill(#FCBE0C);
ellipse(pos_x+235,pos_y+394,20,20);

//----- hands -----//
fill(255);
ellipse(pos_x+180,pos_y+395,30,28);
ellipse(pos_x+290,pos_y+395,30,28);

}

void keyPressed(){
if(key=='c'||key=='C'){
clr_bg_dora = color(random(0,255),random(0,255),random(0,255));
}
if(key=='r'||key=='R'){
clr_bg_dora = color(255);
}
}