วันพุธที่ 23 ตุลาคม พ.ศ. 2556

A2-24/10/56

ตัวอย่าง Function  ที่มีใช้ในการสร้างเกมส์


Function ที่มีพารามิเตอร์แต่ไม่มีการรีเทิร์นค่ากลับ

              void drawBall(int a, int b, int r) {
                    noStroke ();
                    strokeWeight (1);
                    fill (#F50202);
                    ellipse(a+Move_up, b+Set_up, r, r);
                    ellipse(a+Move_down-90, b+Set_down+240, r, r);//down
                    stroke (0);
                    fill(255);
                    ellipse(a-13+Move_up, b+Set_up, 25, 25);
                    ellipse(a+13+Move_up, b+Set_up, 25, 25);
                    ellipse(a-13+Move_down-90, b+Set_down+240, 25, 25);//down
                    ellipse(a+13+Move_down-90, b+Set_down+240, 25, 25);//down
                    fill(0);
                    quad(a-2+Move_up, b+Set_up-23, a-4+Move_up, b+Set_up-17, a-24+Move_up,
                    b+Set_up-20, a-22+Move_up, b+Set_up-27);
                    quad(a+2+Move_up, b+Set_up-23, a+4+Move_up, b+Set_up-17, a+24+Move_up,
                    b+Set_up-20, a+22+Move_up, b+Set_up-27);
                    quad(a-2+Move_down-90, b+Set_down-23+240, a-4+Move_down-90, b+Set_down-17+240,
                    a-24+Move_down-90, b+Set_down-20+240, a-22+Move_down-90, b+Set_down-27+240);//down
                    quad(a+2+Move_down-90, b+Set_down-23+240, a+4+Move_down-90, b+Set_down-17+240,
                     a+24+Move_down-90, b+Set_down-20+240, a+22+Move_down-90, b+Set_down-27+240);//down
                    ellipse(a-8+Move_up, b+Set_up, 10, 10);
                    ellipse(a+8+Move_up, b+Set_up, 10, 10);
                    ellipse(a-8+Move_down-90, b+Set_down+240, 10, 10);//down
                    ellipse(a+8+Move_down-90, b+Set_down+240, 10, 10);//down
               }

   
       Function นี้เป็นฟังก์ชั่นที่ใช้สำหรับสร้างลูกบอล ภายในฟังก์ชันมีการกำหนดค่าให้แก่พารามิเตอร์ เมื่อพารามิเตอร์รับค่าดังกล่าวมาก็จะนำไปใช้ในฟังก์ชั่น ทำให้สะดวกเเละรวดเร็วในการเรียกใช้งาน สามารถกำหนดและนำเอาไปใช้ได้หลายๆค่าและหลายครั้ง หากมีการเปลี่ยนแปลงค่าก็สามารถเปลี่ยนได้ที่เดียวเลย ดังนั้นพารามิเตอร์ก็จะมีการนำเอาค่าใหม่ไปใช้

Function ที่ไม่มีพารามิเตอร์และไม่มีการรีเทิร์นค่ากลับ

          void moveBall(){
                     if (Move_up<800) {
                       Move_up=Move_up+Speed_up;
                       if (Move_up>=190 && Move_up<200) {
                         if (keyCode==LEFT && Set_up==0) {
                         }
                         else {
                           fill(250, 12, 13);
                          Move_up=190;
                           Set_up=Set_up+1;
                         }
                       }
                       if (Move_up>=440 && Move_up<=460) {
                          if (keyCode==RIGHT && Set_up==0) {
                         }
                         else {
                            Move_up=440;
                           Set_up=Set_up+1;
                           fill(250, 12, 13);
                         }
                       }
                       if (Move_up>=800) {
                         Move_up = 0;
                         Speed_up = Speed_up + 0.5;
                       }
                     }
                     if (Move_down<800) {
                       Move_down = Move_down+Speed_down;
                       if (Move_down>=290 && Move_down<300) {
                         if (keyCode=='A' && Set_down==0) {
                         }
                         else {
                           fill(250, 12, 13);
                           Move_down=290;
                           Set_down=Set_down+1;
                         }
                       }
                        if (Move_down>=530 && Move_down<=540) {
                         if (keyCode=='D' && Set_down==0) {
                         }
                         else {
                           Move_down=530;
                           Set_down=Set_down+1;
                           fill(250, 12, 13);
                         }
                       }
                       if (Move_down>=800) {
                         Move_down = -30;
                         Speed_down = Speed_down + 0.5;
                       }
                     }
                     if (miss>=1 && miss <5) {
                       if (Set_up==400) {
                         Set_up=0;
                         Move_up=0;
                         Move_up = Move_up+Speed_up;
                       }
                       if (Set_down==200) {
                         Move_down=-90;
                         Set_down=0;
                         Move_down = Move_down+Speed_up;
                       }
                     }
                   }
         Function นี้เป็นฟังก์ชันที่ใช้สร้างเงื่อนไขการขยับของบอล  ภายในฟังก์ชั่นไม่มีทั้งพารามิเตอร์และ
การกำหนดค่า มีการกำหนด Statement ให้แก่ฟังก์ชั่น เมื่อมีการเรียกใช้ฟังก์ชั่นดังกล่าวแล้วมีการแสดงค่าตาม Statement

if else condition

                   void scoreBall() {
                     fill(0);
                     if (miss<5) {
                       if (Move_up<800) {
                         if (Move_up>=190 && Move_up<191) {
                           if (Set_up==0) {
                             score++;
                           }
                           if (Set_up==10) {
                             miss++;
                           }
                         }

                         if (Move_up>=440 && Move_up<441) {
                           if (Set_up==0) {
                             score++;
                           }
                           if (Set_up==10) {
                             miss++;
                           }
                         }
                       }
                       if (Move_down<800) {
                         if (Move_down>=290&&Move_down<291) {
                           if (Set_down==0) {
                             score++;
                           }
                           if (Set_down==10) {
                             miss++;
                           }
                         }

                         if (Move_down>=530 && Move_down<531) {
                           if (Set_down==0) {
                             score++;
                           }
                           if (Set_down==10) {
                             miss++;
                           }
                         }
                       }
                     }
                     if (miss>=5) {
                       miss=5;
                       Right_down=0;
                       Left_down=+5;
                       slide_Ldown=-5;
                       Right_up=0;
                       Left_up=0;
                       slide_Rup=-5;
                       slide_Ldown=-5;
                       slide_Rdown=-5;
                       slide_Lup=-5;
                       x_Minion=x_Minion+2;
                       y_Minion=y_Minion+0.6;
                       if (x_Minion>=410) {
                         x_Minion=410;
                       }
                       if (y_Minion>=250) {
                         y_Minion=250;
                         fill (255);
                         rect(-10, -10, 620, 420);
                         fill (#FC0505);
                         textSize(120);
                         text("GAMEOVER", 50, 150);
                         textSize(36);
                         text("Score : "+score, 100, 260);
                         text("Miss : "+miss, 280, 260);
                         text("Restart  press  R", 150, 360);
                       }
                       Move_down=-30;
                       Move_up=-30;
                     }
                     else {
                       fill(0);
                       textSize(20);
                       text("Score : "+score, 480, 180);
                       text("Miss : "+miss, 480, 230);
                     }
                   }


         Function นี้เป็นฟังก์ชั่นที่สร้างขึ้นเพื่อคำนวณคะแนนและความผิดพลาดเมื่อมีการตกของลูกบอล พร้อมกับแสดงค่าให้เราเห็นในรูปแบบของคำสั่ง text ภายในฟังก์ชั่นมีการใช้เงื่อนไข if-else ซึ่งการนำเอาเงื่อนไขที่นำเอาไปใช้สามารถซ้อนกันได้

while loop condition        

       
                   void drawFloor() {
                     int[][]dis_Brick= {{-10, 30, 70, 110, 230, 270, 310, 350, 470, 510, 550, 590 }, {0, 80, 320} };
                     int i=0;
                     while (i<dis_Brick[0].length) {
                       strokeWeight(1.8);
                       stroke(#000000);
                       fill(#6F3006);
                       rect(dis_Brick[0][i], dis_Brick[1][1], 40, 20);
                       rect(dis_Brick[0][i]-10, dis_Brick[1][1]+20, 40, 20);
                       rect(dis_Brick[0][i], dis_Brick[1][2], 40, 20);
                       rect(dis_Brick[0][i]-10, dis_Brick[1][2]+20, 40, 20);//Brick up
                       noStroke();
                       fill(#46220B);
                       rect(dis_Brick[0][i]+3, dis_Brick[1][1]+10, 35, 10);
                       rect(dis_Brick[0][i]-10+3, dis_Brick[1][1]+20+10, 35, 10);
                       rect(dis_Brick[0][i]+3, dis_Brick[1][2]+10, 35, 10);
                       rect(dis_Brick[0][i]-10+3, dis_Brick[1][2]+20+10, 35, 10);//Brick down
                       i=i+1;
                     }
                 }

        Function นี้เป็นฟังก์ชั่นที่ใช้ในการสร้างพื้นอิฐ ภายในฟังก์ชั่นมีการสร้างเงื่อนไขในรูปแบบของการวนลูป   จะมีการทำรูปแบบเดิมซ้ำๆจนครบเงื่อนไขที่กำหนด (i<dis_Brick[0].length) เพื่อสะดวกและรวดเร็วในการสร้างภาพขึ้นมา นอกจากนี้ภายในฟังก์ชั่นยังมีการกำหนดตัวแปรในรูปของ array 2  มิติในการเก็บค่าระยะห่างของอิฐ เพื่อให้ง่ายต่อการนำไปใช้และไม่ต้องมีการกำหนดค่าของสิ่งเดิม หลายๆค่า (local variable )

Global variable


ตัวแปร Global ทั้งหมดในเกมนี้ได้แก่



float Move_up = 0;   //ตัวแปรควบคุมการเคลื่อนที่ให้ไปทางขวาของลูกบอลลูกบน
int Right_down = 0;   //ตัวแปรควบคุมการขยับแขนของมินเนี่ยน (แขนขวาขยับขึ้นด้านบน)
int Left_up = 0;   //ตัวแปรควบคุมการขยับแขนของมินเนี่ยน (แขนซ้ายขยับขึ้นด้านบน)
int Right_up = 0;   //ตัวแปรควบคุมการขยับแขนของมินเนี่ยน (แขนขวาขยับลงข้างล่าง)
int Left_down = 0;   //ตัวแปรควบคุมการขยับแขนของมินเนี่ยน (แขนซ้ายขยับลงข้างล่าง)
float Move_down = 0;   //ตัวแปรควบคุมการเคลื่อนที่ให้ไปทางขวาของลูกบอลลูกบน
int Set_up = 0;   //ตัวแปรที่ควบคุมลูกบอลให้ร่วงลงมาตามเงื่อนไขของลูกบอลลูกบน
int Set_down=0;   //ตัวแปรที่ควบคุมลูกบอลให้ร่วงลงมาตามเงื่อนไขของลูกบอลลูกล่าง
float Speed_up=1;   //กำหนดความเร็วของลูกบอลลูกบน
float Speed_down = 1;   //กำหนดความเร็วของลูกบอลลูกล่าง
int miss=0;   //กำหนดตัวแปรเก็บค่าและแสดงผลจำนวนครั้งที่ลูกบอลร่วง
int score=0;   //กำหนดตัวแปรเก็บค่าและแสดงผลจำนวนครั้งที่ลูกบอลข้ามผ่านช่องได้
int slide_Lup=0;   //ตัวแปรควบคุมอิฐที่ค่อยๆเลื่อนมาปิดช่องโหว่ เคลื่อนที่ตามมือมินเนี่ยนด้วยการกดปุ่มซ้าย (ซ้ายบน)
int slide_Rup=0;   //ตัวแปรควบคุมอิฐที่ค่อยๆเลื่อนมาปิดช่องโหว่ เคลื่อนที่ตามมือมินเนี่ยนด้วยการกดปุ่มขวา (ขวาบน)
int slide_Ldown=0;   //ตัวแปรควบคุมอิฐที่ค่อยๆเลื่อนมาปิดช่องโหว่ เคลื่อนที่ตามมือมินเนี่ยนด้วยการกดตัว A (ซ้ายล่าง)
int slide_Rdown=0;   //ตัวแปรควบคุมอิฐที่ค่อยๆเลื่อนมาปิดช่องโหว่ เคลื่อนที่ตามมือมินเนี่ยนด้วยการกดปุ่ม D (ขวาล่าง)
float x_Minion = 225;   //กำหนดระยะแนวนอนของตัวมินเนี่ยน

float y_Minion = 190;   //กำหนดระยะตามแนวตั้งของตัวมินเนี่ยน



       

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

Othello



int [][] Bor= {  //ประกาศ+กำหนด array  ชนิด integer
  {
    0, 0, 0, 0, 1, 1, 1, 1
  }
  , {
    1, 1, 1, 1, 0, 0, 0, 0
  }
  , {
    1, 0, 1, 0, 0, 1, 1, 1
  }
  , {
    0, 1, 0, 0, 0, 0, 1, 0
  }
  , {
    1, 0, 1, 0, 0, 1, 1, 1
  }
  , {
    1, 1, 1, 1, 0, 0, 0, 1
  }
  , {
    0, 0, 0, 0, 1, 1, 1, 0
  }
  , {
    1, 1, 1, 1, 0, 0, 0, 1
  }
};
void setup() {
  size(400, 400);
  background(#0F6F21);
  drawtable(); //เรียกใช้ ฟังก์ชั่นที่วาดตาราง drawtable()
  drawBody(); //เรียกใช้ฟังชั่นที่วาดลูกหมาก drawBody()
}
void drawtable() {
  int a = 0;
  float hightX = 0;
  float x = width/8;
  float highY = 0;
  float y = height/8;
  stroke (#11481B);
  strokeWeight(3);
  while (a < 9) {
    line(hightX+x, 0, hightX+x, height);
    line(0, highY+y, width, highY+y);
    hightX = hightX+x;
    highY =highY+y;
    a = a+1;
  }
}
void drawBody() {
  int i ;
  int j ;
  float b = width/16;
  float q = width/8;
  float c = height/16;
  float e = height/8;

  for (i=0;i < Bor.length;i++) {  // ใช้ for loop ในการวนลูป โดย มีการทำงาน คือ for(กำหนดค่าเริ่มต้นของตัวแปล;เงื่อนไขที่วนลูป;เพิ่มค่าตัวแปลที่กำหนดเมื่อครบรอบ)
    for (j=0;j < Bor[i].length;j++) {
      if (Bor[i][j] == 1) {  //เงื่อนไขที่ใช้ในการวาด ลูกหมาก คือ ถ้า array ที่เก็บค่า มีค่าที่เก็บใน อินเด็ซ เท่ากับ 1 ให้ทำ ตาม statement
        noStroke();
        fill(255);
        ellipse(b, c, width/10, width/10);
      }
      if (Bor[i][j] == 0) { //เงื่อนไขที่ใช้ในการวาด ลูกหมาก คือ ถ้า array ที่เก็บค่า มีค่าที่เก็บใน อินเด็ซ เท่ากับ 0 ให้ทำ ตาม statement
        noStroke();
        fill(0);
        ellipse(b, c, width/10, width/10);
      }
      b = b+q;
    }

    b = width/16;
    c = c+e;
    j = 0;
  }
}

วันเสาร์ที่ 21 กันยายน พ.ศ. 2556

Factorial




int fac(int n){
if(n==1){
return n;
}else{
return fac(n-1)*n;
}

}
void setup(){
background(2);
size(100,100);
textSize(20);
text(fac(6),50,50);
}

Matrix calculation




void setup() {
  background(2);
  int[][] a = { {1,2},{2,1}};
  int[][] b = {{5,4},{4,5}};
  int[][] cul = new int[2][2];                                            
  int x=30;
  int y=50;
  int i=0;
  int j=0;
  while (i<a.length) {
    while(j<a[i].length) {
      cul[i][j] = a[i][j]+b[i][j];
      textSize(15);
      text(" "+cul[i][j], x, y);
      x=x+20;
      j++;
    }
    y=y+20;
    i++;
  }
}

Bar Chart [Parallel Array]




void setup() {   
  size (550, 500); 
  background(#54BCB4);
}
float max;
float min;
float avr;
void draw () { 
  int x = 80; 
  int i = 0;
  int j = 0; 
  int v = 390;
  float [][] Bar = { 
    { 
      19.7, 21.9, 19.5, 19.0
    } 
    , 
    { 
      14.5, 14.0, 16.0, 15.5
    }
  }; 
  String [] Age = {
    "0-4", "5-9", "10-14", "15-17"
  };
  while (i < Bar[0].length) {
    textSize(10);
    fill(#1B2E86);
    rect (x, 320, 30, -Bar[0][i]*10);
    textSize(12);
    text(Bar[0][i]+"%", x, 350);
    fill(#B55ECE);
    rect (x+35, 320, 30, -Bar[1][j]*10);
    textSize(12);
    text(Bar[1][i]+"%", x+40, 350);
    fill(#D37180);
    textSize(14);
    text(Age[i]+"year", x+10, 97); 
    i = i + 1; 
    x = x + 100;

    textSize(17);
    fill (#280A3E);
    text("Number and percentage of child and youth aged 0 - 17 years ", 23, 30);
    text("according to living arrangements by living with parents 2008", 23, 50);
    while (j<=2) {
      if (Bar[i][j]<min) {
        min=Bar[i][j];
      }
      if (Bar[i][j]>max) {
        max=Bar[i][j];
      }
      j=j+1;
    }
  }

  fill(#1B2E86);
  rect(350, v, 15, 15);
  text("neither parent", 370, v+15);
  fill(#B55ECE);
  rect(350, v+20, 15, 15);
  text("mother only", 370, v+35);
  text("MAX: "+max, 150, v+15);
  text("MIN: "+min, 150, v+35);
}

Class Bottun




void setup() {
  size(200, 200);
  background (0);
}
void draw () {
  Button a = new Button ();
  a.click ();
  fill(255);
  text("Thiranan ", 80, 100);
}
class Button {
  int x;
  int y;
  Button(){
    this.x=60;
    this.y=80;
  }
  void click() {
    if (mousePressed == true) {
      fill(#E6A8ED);
    }else {
      fill(#FFFFFF);
    }
    rect(x, y, 80, 30);
  }
}

for loop

Example

for statement calculates and displays a commission

code

double sales=0.0;
double comm =0.0;
Console : : Write("Enter the sales : ");
sales = Convert : : ToDouble(Console: :ReadLine());
for(double rate = .1;rate<=.25;rate=rate+.05)     // for(initialization ; condition ; update){statement}
{
    comm = sales * rate ;
    Console: :WriteLine("Commission: ${0}",Convert: :ToString(comm));
}//end for

result

Commission: $2500
Commission: $3750
Commission: $5000
Commission: $6250

An Introduction to Programming whith C++ / Diane Zak
    ISBN : 0-619-06473-0
    page : 348