วันศุกร์ที่ 20 กันยายน พ.ศ. 2556

UFO


UFO



การสร้างฟังก์ชั่นชุดนี้เป็นการกำหนดขึ้นเพื่อสร้าง UFO โดยการกำหนด class Diagram โดยแยกออกเป็น attribute และ method แต่เวลาเขียนฟังก์ชั่นนั้น ส่วนใหญ่เขียนออกมาเป็นรูปแบบปกติ แต่กำหนดฟังก์ชั่นในรูปแบบต่าง โดยต้องมีการสร้างฟังก์ชั่นหลายฟังก์ชั่น และกำหนดเงื่อนไข รวมทั้งตัวแปรไว้ภายใน แต่ต้องเรียกใช้โดย void setup นอกจากนี้ยังต้องมีการสร้าง class เพื่อการเก็บข้อมูลต่างๆ ไว้ภายใน สำหรับ class Diagram นั้นเขียนได้เป็นดังนี้
class diagram
name: Ufo
attribute:
   x: int
   y: int
   w: int
   i: int
   d: String d
   stop: int
   tron: String
   speed: int
method:
   Ufo(x: int, y: int , w: int , s: int, d: String, c: String )
   display()
  contron() {
  fast()
  buttom() {

Processing Code


void setup() {  //ฟังก์ชั่นหลักที่ใช้ในการกำหนดค่า และเรียกใช้สิ่งต่างๆ เป็นฟังก์ชั่นที่ไม่มีการวกกลับ
  size(500, 500);  //ฟังก์ชั่นที่ใช้ในการกำหนดขนาดของพื้นที่ หรือ size(width, height) 

}
Ufo a = new Ufo(250, 250, 250, 2, "up", "red");  //UFO a สีแดง จุดเริ่มอยู่แกน X 250 แกน Y 250 ขนาด 250 ความเร็ว 2 เริ่มด้วยการขยับขึ้น
Ufo b = new Ufo(250, 250, 100, 7, "down", "yello");  //UFO b สีเหลือง จุดเริ่มอยู่แกน X 250 แกน Y 250 ขนาด 100 ความเร็ว 7 เริ่มด้วยการขยับขึ้น
Ufo c = new Ufo(250, 250, 50, 10, "right", "blue");  //UFO c สีน้ำเงิน จุดเริ่มอยู่แกน X 250 แกน Y 250 ขนาด 50 ความเร็ว 10 เริ่มด้วยการขยับขึ้น
Ufo d = new Ufo(250, 250, 200, 5, "left", "green");  //UFO d สีเขียว จุดเริ่มอยู่แกน X 250 แกน Y 250 ขนาด 200 ความเร็ว 5 เริ่มด้วยการขยับขึ้น
void draw() {  //ฟังก์ชั่นหลักที่ใช้ในการวาด
  background(mouseX, mouseY, mouseX);  //ฟังก์ชั่นที่ใช้ในการเติมสีให้กับพื้นหลัง โดยสีที่ใส่ไปนี้มีค่าไปตามการเคลื่อนไหวของเมาส์
  a.display();  //แสดง UFO a
  b.display();  //แสดง UFO b
  c.display();  //แสดง UFO c
  d.display();  //แสดง UFO d
}

class Ufo {  //สร้าง class ชื่อ UFO สำหรับการเก็บข้อมูล
  int x, y, w, i;  //กำหนดตัวแปรทั้งสี่เป็นจำนวนเต็มเพื่อให้เก็บค่าแกน X แกน Y ความกว้าง และการนับ ตามลำดับ
  String d, s;  //กำหนดเพื่อเก็บทิศทาง
  int stop;  //กำหนดตัวแปรนี้เพื่อเป็นตัวหยุด
  String tron;  //กำหนดเพื่อเก็บค่าสี
  int speed;  //กำหนดตัวแปรนี้เพื่อเก็บความเร็ว
  Ufo(int x, int y, int w, int s, String d, String c) {
  this.x = x;
  this.y = y;
  this.w = w;
  this.d = d;
  this.i = 0;
  this.stop = 0;
  this.tron = c;
  this.speed = s;
}

void display() {  //ฟังก์ชั่นที่สร้างขึ้นเพื่อแสดง UFO
  contron();  //ตัวควบคุม
  float h = this.w/3;
  noStroke();
  if(tron.equals("red")) {
    fill(255, 0, 0);  //สีแดง
  }
  else if(tron.equals("blue")) {
    fill(0, 0, 255);  //สีน้ำเงิน
  }
  else if(tron.equals("yello")) {
    fill(255, 255, 0);  //สีเหลือง
  }
  else if(tron.equals("green")) {
    fill(0, 255, 0);  //สีเขียว
  }
  strokeWeight(3);  //ฟังก์ชั่นสำหรับการเพิ่มความหนาของเส้น
  stroke(0);  //ฟังก์ชั่นสำหรับการเติมสีในเส้น โดยสีที่ใส่ไปคือสีดำ
  ellipse(this.x, this. y, this.w, h);  //ฟังก์ชั่นสำหรับการวาดวงรี หรือ ellipse(x, y, width, height)
  float q = this.y-2*h/5, p=this.w-h, u=h;
  ellipse(this.x, q, p, u);
  fill(random(0, 255), random(0, 255), random(0, 255));  //ฟังก์ชั่นที่ใช้ในการเติมสีให้กับสิ่งต่างๆ โดยกำหนดให้เป็นไปตามแม่สี หรือเครื่องหมาย # รวมทั้งฟังก์ชั่น โดยสีที่ใส่ไปนี้ให้ออกมาแบบสุ่มตามฟังก์ชั่น random
  float r = h/5;
  ellipse(this.x, q, r, r);
  ellipse(this.x+3*h/5, q, r, r);
  ellipse(this.x-3*h/5, q, r, r); 
}

void contron() {  //ฟังก์ชั่นสำหรับการควบคุม
  fast();  //เคลื่อนตามเข้ม
  buttom();  //สร้างขึ้นเพื่อการกด Ufo
  //กำหนดเงื่อนไขเมื่อมีทิศทางตามนี้
  if(d.equals("up")) {
    y = y-this.speed;
  }
  else if(d.equals("down")) {
    y = y+this.speed;
  }
  else if(d.equals("right")) {
    x = x+this.speed;
  }
  else if(d.equals("left")) {
    x = x-this.speed;
  }
  else if(d.equals("stop")) {
 
  } 
}
 
void fast() {  //ฟังก์ชั่นเพื่อกำหนดการเคลื่อนที่ตามเข้ม
  if(x>=500-this.w/2&&y<500-this.w/6) { 
    d = "down"; 
  }
  else if(y>=500-this.w/6&&x>this.w/2) {
    d = "left";
  }
  else if(x<=this.w/2&&y>(this.w-this.w/3)/2) {
    d = "up";
  }
  else if(y<=this.w/6+2*w/15&&x<500-this.w/2) {
    d = "right";
  } 
}
 
void buttom() {  //ฟังก์ชั่นเพื่อการกด UFO
  float star1 = sqrt((w/2)*(w/2)-(w/6)*(w/6));
  float h1 = this.x+star1;
  float h2 = this.x-star1;
  float star2 = sqrt((h1-mouseX)*(h1-mouseX)+(this.y-mouseY)*(this.y-mouseY));
  float star3 = sqrt((h2-mouseX)*(h2-mouseX)+(this.y-mouseY)*(this.y-mouseY));
  float star4 = star2+star3;
  if(star4<=w&&mousePressed) {
    if(stop==0&&i%7==0) {
    d = "stop";
    stop = 1;
    }
    else if(stop==1&&i%10==0) {     
      stop = 0;
    }
  }
  float star5 = sqrt(((w-w/3)/2)*((w-w/3)/2)-(w/6)*(w/6));
  float h3 = this.x+star1;
  float h4 = this.x-star1;
  float star6 = sqrt((h3-mouseX)*(h3-mouseX)+(y-2*w/15-mouseY)*(y-2*w/15-mouseY));
  float star7 = sqrt((h4-mouseX)*(h4-mouseX)+(y-2*w/15-mouseY)*(y-2*w/15-mouseY));
  float star8 = star6+star7;
  if(star8<=w&&mousePressed) { 
    if(stop==0&&i%7==0) {
      d = "stop";
      stop = 1;
    }
    else if(stop==1&&i%10==0) {
      stop = 0;
    }
  }
  if(stop==1) {
    d = "stop";
  }
  i = i+1;
  }
}

Canvas


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

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