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

A 2 30/10/12

float x_selectmode=20;
      // ตัวแปรชนิด จำนวนจริง เก็บระยะแกน x ของ ข้อความ selectmode
int radius_smooth = 80;
     // ตัวแปรชนิด จำนวนเต็ม เก็บค่าของความกว้างของปุ่ม smooth
int radius_step = 80;
    // ตัวแปรชนิด จำนวนเต็ม  เก็บค่าของความกว้างของปุ่ม step
float distance_x, distance_y, distance_total;
   // ตัวแปรชนิด จำนวนจริง  เก็บค่าระยะห่างจากจุดศูนย์กลางของแกน x แกน y
      และระยะห่างระหว่างแกน x แกน y ตามลำดับ
int divided =50;
     // ตัวแปรชนิด จำนวนเต็ม เก็บค่าตัวแปรเพื่อเอาไว้หน่วงจังหวะ
int miss = 0;
     // ตัวแปรชนิด จำนวนเต็ม เก็บจำนวนคนที่ตกน้ำ
int highscore;
      // ตัวแปรชนิด จำนวนเต็ม เก็บคะแนนที่มากที่สุดในการเล่นเกม
int checkscoreArea = 90;
int checkscoreArea1 =90;
    // ตัวแปรชนิด จำนวนเต็ม เก็บระยะเพื่อเพิ่มคะแนน

int stateButton =0;
 
  // ตัวแปรชนิด จำนวนเต็ม เช๊คสถานะของปุ่ม

int scores = 0;
 
  // ตัวแปรชนิด จำนวนเต็ม เก็บคะแนนก่อน gameover

int rounds = 1;

    // ตัวแปรชนิด จำนวนเต็ม เก็บจำนวนรอบของเกม


int [] position = new int [8];
 
  // ตัวแปรอาเรย์ ชนิด จำนวนเต็ม เก็บตำแหน่งเริ่มต้นของคนเดิน


ซึ่งเก็บเป็นแบบอาเรย์จะช่วยให้ ลดการประกาศ global variable ไปได้มาก

int count = 0;

  // ตัวแปรชนิด จำนวนเต็ม เพื่อนับค่าเพิ่มขึ้นเรื่อยๆ


int changefoot=0;
  //ใช้เชคช่วงเพื่อเปลี่ยนการวาดรูปเท้าของคนเดิน

int inc = 1;
  //ตัวแปร int เอาไว้สำหรับเพิ่ม ลด ระยะแกน x ของคนเดินทั้งในโหมด step และ smooth

void setup() {
  size(400, 400); //กำหนดขนาดหน้าจอ
}
void draw() {
  drawstartpage();
  startSmooth();
 startStep();

}
void mousePressed() { //ฟังก์ชั่นของระบบ จะทำงานเมื่อมีเม้าส์คลิกลงไป

  distance_x = mouseX-120;//พิกัดเม้าส์ X ลบกับพิกัดแกน X ของจุดศูนย์กลางของวงกลมปุ่มกด smooth
  distance_y = mouseY-330;//พิกัดเม้าส์ Y ลบกับพิกัดแกน Y ของจุดศูนย์กลางของวงกลมปุ่มกด smooth
  distance_total = sqrt((distance_x*distance_x)+(distance_y*distance_y));
//เชคระยะว่าเม้าอยู่ในวงกลมหรือไม่ โดยใช้หลักของพีทากอรัส
  if (distance_total <= radius_smooth /2 ) {{
//หากเชคระยะมาได้ว่ามีค่าน้อยกว่าหรือเท่ากับรัศมีของวงกลมหาร2 ให้ statebutton เท่ากับ 1
    stateButton =1;
  }
  distance_x = mouseX-280; //พิกัดเม้าส์ X ลบกับพิกัดแกน X ของจุดศูนย์กลางของวงกลมปุ่มกด state
  distance_y = mouseY-330; //พิกัดเม้าส์ Y ลบกับพิกัดแกน Y ของจุดศูนย์กลางของวงกลมปุ่มกด state
  distance_total = sqrt((distance_x*distance_x)+(distance_y*distance_y));
  if (distance_total <= radius_step /2 ) {
//หากเชคระยะมาได้ว่ามีค่าน้อยกว่าหรือเท่ากับรัศมีของวงกลมหาร2 ให้ statebutton เท่ากับ 2
    stateButton =2;
  }

  if (mouseX >= 140 && mouseX <= 260 && mouseY >= 250 && mouseY <= 310) {
//ถ้า พิกัดเม้าส์ x อยู่ในช่วง 140<=x<=260 และพิกัดเม้าส y อยู่ในช่วง 250<=y<=310 ใช้เชคปุ่ม tryagain
    if (stateButton == 1) {  //ถ้า state เท่ากับ 1 ก้อให้ state เท่ากับ1 เช่นเดิม
      stateButton=1;
    }
    else {
      stateButton=2; //กรณีอื่น ให้ state เท่ากับ 2
    }
  }
  reset();
  if (mouseX >= 220 && mouseX <= 340 && mouseY >= 250 && mouseY <= 310) {
    stateButton =0 ;   //ให้ state เป็น 0
    startpage();//เรียกใชั้ฟังก์ชั่น startpage แล้วโปรแกรมก้อจะค้างอยู่หน้าstartpage เพราะ ได้ตั้งค่า state เป็น 0 แล้ว จะเริ่มเล่นเกมอีกทีได้ก็ต่อเมื่อเลือกโหมดในการเล่น
  }
}

void drawstartpage() {//ฟังก์ชั่นวาดหน้า startpage
  int color1, color2, color3;
  background(0);
  color1= (int)random(0, 255);//random ต่าตั้งแต่ 0-100 เก็บไว้ใน color เพื่อนำไปใช้เติมสี คำว่า manhole
  color2 = (int)random(0, 255);
  color3 = (int )random(0, 255);
  fill(color1, color2, color3);
  textSize(120);
  text("MAN", 10, 100);
  text("HOLE", 60, 220);
  textSize(15);
  fill(255);
  textSize(15);
  fill(255);
  text("NINTENDO GAME AND WATCH", 80, 120);
  textSize(18);
  text("SELECT GAME MODE", x_selectmode, 260);
  x_selectmode+= .1; // ให้พิกิด x ของประโยคนี้(SELECT GAME MODE)บวกเพิ่มขึ้นไปเรื่อยๆ
  if (x_selectmode >= 400) {//ถ้าหากพิกัดแกน x ของประโยคนี้มากกว่าหรือเท่ากับ 400 แล้ว ให้ พิกัดแกน x เท่ากับ -200 กล่าวคือให้กลับมาเริ่มต้นใหม่นั่นเอง
    x_selectmode = -200;
  }
  strokeWeight(10);
  stroke(#48D0F7);
  drawSmoothbutton();
  drawStepbutton();
}
void reset() {
  changeconvert = 0;
  miss = 0;
  checkscoreArea= 90;
checkscoreArea1 = 90;
  scores = 0;
  rounds = 1;
 position[0] = -100;
  position[1] = 125;
  position[2] = -370;
  position[3] = 125;
  position[4] = 550;
  position[5] = 275;
  position[6] = 620;
  position[7] = 275;


  if (stateButton ==1 ) {
    drawbackgrounds();
    drawMantop(position[0],position[1]); // เรียกใช้ฟังก์ชันวาดคนเดินแบบรับพารามิเตอร์แกน x , y
    drawMantop(position[2],position[3]);
    drawManbottom(position[4],position[5]);
    drawManbottom(position[6],position[7]);
    changeSmooth();
    keyPressed();
    gameover(1);
    showmissed();
  }
  if (stateButton == 2) {
      drawbackgrounds();
    drawMantop(position[0],position[1]);
    drawMantop(position[2],position[3]);
    drawManbottom(position[4],position[5]);
    drawManbottom(position[6],position[7]);
    changeStep();
    keyPressed();
    gameover(50);
    showmissed();
  }
}
void drawSmoothbutton() {//ฟังก์ชั่นวาดปุ่ม smooth
  fill(0);
  stroke(#FFDF24);
  fill(#F20F0F);
  ellipse(120, 330, radius_smooth, radius_smooth);
  fill(255);
  textSize(14);
  text("SMOOTH", 90, 340);
  distance_x = mouseX-120;//พิกัดเม้าส์ X ลบกับพิกัดแกน X ของจุดศูนย์กลางของวงกลมปุ่ม smooth
  distance_y = mouseY-330;//พิกัดเม้าส์ Y ลบกับพิกัดแกน Y ของจุดศูนย์กลางของวงกลมปุ่ม smooth
  distance_total = sqrt((distance_x*distance_x)+(distance_y*distance_y)); //เชคระยะว่าเม้าอยู่ในวงกลมหรือไม่ โดยใช้หลักของพีทากอรัส
  if (distance_total<= radius_smooth/2) { //หากเชคระยะมาได้ว่ามีค่าน้อยกว่าหรือเท่ากับรัศมีของวงกลมหาร2 ให้รัศมีเท่ากับ 100 กล่าวคือหากเม้าส์เข้าไปในรัศมีก็จะเห็นปุ่มวงกลมขยายใหญ่ขึ้น
    radius_smooth= 100;
  }
  else {
    radius_smooth = 80; //นอกจากนั้นให้รัศมีเท่ากับ 80  กล่าวคือหากเม้าสไม่เข้าไปในรัศม ปุ่มวงกลมก็จะเท่าเดิมไม่ขยายใหญ่ขึ้น
  }
}
void drawStepbutton() {//ฟังก์ชั่นวาดปุ่ม step
  fill(0);
  fill(#F20F0F);
  ellipse(280, 330, radius_step, radius_step);
  fill(255);
  textSize(17);
  text("STEP", 263, 340);
  distance_x = mouseX-280;
  distance_y = mouseY-330;
  distance_total = sqrt((distance_x*distance_x)+(distance_y*distance_y));
  if (distance_total<= radius_step/2) {
    radius_step= 100;
  }
  else {
    radius_step = 80;
  }
}
void startSmooth() {  // ฟังก์ชันเริ่มเกมในโหมด smooth
  if (stateButton ==1 ) {
    drawbackgrounds();
    drawMantop();
    drawManbottom();
    changeSmooth();
    drawManbottom();
    keyPressed();
    checkgameover(1);
    showmissed();
  }
}
void startStep() {     // ฟังก์ชันเริ่มเกมในโหมด step
  if (stateButton == 2) {
    drawbackgrounds();
    drawMantop();
    drawManbottom();
    changeStep();
    keyPressed();
    checkgameover(50);
    showmissed();
  }
}
void drawTryAgainbutton() {//ฟังก์ชั่นวาดปุ่ม tryagain
  noStroke();
  fill(#B2FCEF);
  rect(60, 250, 120, 60 );
  fill(#1BE3A8);
  rect(70, 260, 100, 40 );
  fill(0);
  textSize(19);
  text("TryAgain", 80, 285);
}
void drawManubutton() {//ฟังก์ชั่นวาดปุ่ม Manu
  noStroke();
  fill(#B2FCEF);
  rect(220, 250, 120, 60 );
  fill(#1BE3A8);
  rect(230, 260, 100, 40 );
  fill(0);
  textSize(19);
  text("Manu", 250, 285);
}
void highScore() {    //ฟังก์ชั่นวาด highscore
  text("HIGH SCORE = "+highscore, 250, 20);
  if (highscore < scores) {//ถ้า highscore น้อยกว่า score ให้ highscore มีค่าเท่ากับ scores แล้วนำค่า highscore ไปแสดงผล
    highscore = scores;
  }
}
void drawbackgrounds() {
// ฟังก์ชัน drawbackgrounds แก้ไขโดยการแยกเป็นฟังก์ชันย่อยๆ เพื่อลดขนาดของฟังก์ชัน
   drawbackgrounds 
  background(#C9FFFB); //ใส่สีพื้นหลังสีฟ้า
  drawwall(0, 180);
  drawwall(152, 180);
  drawwall(315, 180);
  drawcurvofwall(150, 180);
  drawwall(0, 330);
  drawwall(150, 330);
  drawwall(315, 330);
  drawcurvofwall(150, 330);
  drawwater(95, 180);
  drawwater(245, 180);
  drawwater(95, 330);
  drawwater(245, 330);
  drawmountain();
  drawcloud();
  fill(0);
  textSize(15);
  text("Miss ", 100, 20); //เขียนคำว่า Miss
  text("Score = "+ scores, 300, 50); //เขียนคำว่า Score
  highScore(); //เรียกใช้ฟังก์ชัน highScore
}
void drawwall(int x, int y) {   // วาดกำแพงสำหรับคนเดิน
  fill(0);
  stroke(#5A2E01);
  strokeWeight(10);
  line(x, y, x+80, y); //วาดขอบบนทางเดิน
  noStroke();
  fill(#A77413);
  rect(x, y, 80, 50); //วาดกำแพง
}
void drawcurvofwall(int x, int y) {  // ฟังก์ชันวาดส่วนเว้าของกำแพง
  fill(#C9FFFB);
  ellipse(x+80, y+30, 50, 60);
  ellipse(x, y+30, 50, 60);
  ellipse(x-150, y+30, 50, 60);
  ellipse(x-70, y+30, 50, 60);
  ellipse(x+165, y+30, 50, 60);
  fill(#C9FFFB);
  ellipse(x+245, y+30, 50, 60);
}
void drawwater(int x, int y) {  //ฟังก์ชันวาดน้ำ
  fill(#2BDED0);
  ellipse(x, y+50, 20, 5);
  ellipse(x+15, y+45, 20, 5);
  ellipse(x+30, y+50, 20, 5);
  ellipse(x+45, y+45, 20, 5);
}
void drawmountain() { // ฟังก์ชันวาดรูปภูเขา
  fill(#31FF2E);
  quad(-20, 175, 100, 100, 120, 100, 220, 175);//วาดภูเขาลูกที่หนึ่ง
  ellipse(110, 105, 22, 22);
  quad(200, 175, 300, 70, 320, 70, 420, 175);//วาดภูเข้าลูกที่สอง
  ellipse(310, 74, 21, 21);
}
void drawcloud() {  // ฟังก์ชันวาดรูปก้อนเมฆ
  fill(255);
  ellipse(x_selectmode+150, 30, 70, 40);
  ellipse(x_selectmode+180, 40, 60, 30);
  ellipse(x_selectmode+120, 40, 50, 30);
  ellipse(x_selectmode+300, 60, 50, 30);
  ellipse(x_selectmode+330, 50, 80, 50);
  ellipse(x_selectmode+360, 60, 60, 30);
}
void drawmiss(int x, int y) {//ฟังก์ชั่นวาดภาพหน้าคนที่จะขึ้นมาเมื่อคนตกน้ำ  โดยรับพาริมิเตอร์นำไปใช้เป็นพิกัดแกน xและy ในการวาดรูป
  fill(#F5800A);
  strokeWeight(1);
  ellipse(x, y, 30, 25); //หัว

  fill(255);
  ellipse(x, y+7, 20, 10);// ปาก
  stroke(0);

  line(x-7, y+4, x-5, y+10);
  line(x-5, y+10, x-2, y+2);
  line(x-2, y+2, x+1, y+11);
  line(x+1, y+11, x+4, y+2);
  line(x+4, y+2, x+6, y+10);

  noStroke();
  ellipse(x-4, y-5, 8, 10); // ตา
  ellipse(x+4, y-5, 8, 10);
  fill(0);
  ellipse(x-3, y-3, 4, 4);
  ellipse(x+4, y-3, 4, 4);
}
void drawFaceman(int x_man, int y_man) {//ฟังก์ชั่นวาดภาพหน้าคนเดิน  โดยรับพาริมิเตอร์นำไปใช้เป็นพิกัดแกน xและy ในการวาดรูป
  fill(0);
  stroke(0);
  strokeWeight(5);
  ellipse(x_man, y_man, 30, 25); //หัว
  fill(255);
  noStroke();
  ellipse(x_man, y_man+7, 25, 10);// ปาก
  stroke(0);
  strokeWeight(1);
  line(x_man-2, y_man, x_man-2, y_man+13);// ฟัน
  line(x_man-8, y_man, x_man-8, y_man+10);
  line(x_man-5, y_man, x_man-5, y_man+12);
  line(x_man+1, y_man, x_man+1, y_man+12);
  line(x_man+4, y_man, x_man+4, y_man+10);
  line(x_man+7, y_man, x_man+7, y_man+10);
}
void drawHandmantop(int x_mantop, int y_mantop) {//ฟังก์ชั่นวาดภาพแขนคนเดินด้านบน  โดยรับพาริมิเตอร์นำไปใช้เป็นพิกัดแกน xและy ในการวาดรูป
  line(x_mantop-14, y_mantop+23, x_mantop-25, y_mantop+12);  // แขน
  ellipse(x_mantop-25, y_mantop+12, 5, 5);
  line(x_mantop+13, y_mantop+23, x_mantop+25, y_mantop+12);  // แขน
  ellipse(x_mantop+25, y_mantop+12, 5, 5);
}
void drawHandmanbottom(int x_manbottom, int y_manbottom) {//ฟังก์ชั่นวาดภาพแขนคนเดินด้านล่าง  โดยรับพาริมิเตอร์นำไปใช้เป็นพิกัดแกน xและy ในการวาดรูป
  line(x_manbottom-14, y_manbottom+23, x_manbottom-25, y_manbottom+12);  // แขน
  ellipse(x_manbottom-25, position[0]+12, 5, 5);
  line(x_manbottom+13, y_manbottom+23, x_manbottom+25, y_manbottom+30);  // แขน
  ellipse(x_manbottom+25, y_manbottom+30, 5, 5);
}
void drawLegman(int x_man, int y_man) {//ฟังก์ชั่นวาดภาพแขนคนเดินด้านล่าง  โดยรับพาริมิเตอร์นำไปใช้เป็นพิกัดแกน xและy ในการวาดรูป

  line(x_man-5, y_man+38, x_man-10, y_man+45); // ขา
  line(x_man+5, y_man+38, x_man+10, y_man+45);
  if (changefoott>=1 && changefoot<=40) {//ฟังก์ชั่นวาดภาพขาคนเดิน  โดยรับพาริมิเตอร์นำไปใช้เป็นพิกัดแกน xและy ในการวาดรูป
    line(x_man-20, y_man+40, x_man-5, y_man+48);// เท้า
    line(x_man+5, y_man+48, x_man+20, y_man+48);
  }
 if (changefoot>40 && changefoot<80) {
    line(x_man-20, y_man+48, x_man-5, y_man+48);// เท้า
    line(x_man+5, y_man+48, x_man+20, y_man+40);
  }
  changefoot++;
  if (changefoot == 80) {
    changefoot=0;
  }
}
void drawMantop(int x,int y) {
  if (y >=125 && y< 135) {
    drawFaceman(x, y);
    ellipse(x-1,y-5, 8, 10); // ตา
    ellipse(x+7,y-5, 7, 10);
    fill(0);
    ellipse(x,y-5, 4, 4);
    ellipse(x+8,y -5, 4, 4);
    ellipse(x+15,y, 10, 5);// จมูก
    drawBodyman(x,y);
    drawHandmantop(x, y);
    drawLegman(x, y);
  }
}
void drawManbottom(int x,int y) {  // ฟังก์ชันวาดคนเดินด้านล่าง แบบ รับพารามิเตอร์เป็นพิกัดแกน x ,y เพื่อวาดคน
  if (y>= 275 && y < 285) {
    drawFaceman(x,y);
    ellipse(x-8,y-5, 8, 10); // ตา
    ellipse(x, y-5, 7, 10);
    fill(0);
    ellipse(x-10,y-5, 4, 4);
    ellipse(x-2,y-5, 4, 4);
    ellipse(x-15,y, 10, 5);// จมูก
    drawBodyman(x, y);
    drawHandmanbottom(x, y);
    drawLegman(x, y);
  }
 }
void manhold(int x, int y) {//ฟังก์ชั่นวาดภาพคนถือสะพาน โดยรับพาริมิเตอร์นำไปใช้เป็นพิกัดแกน xและy ในการวาดรูป
  fill(0);
  ellipse(x, y, 30, 25); //หัว
  fill(255);
  noStroke();
  ellipse(x, y+7, 20, 10);// ปาก
  stroke(0);
  strokeWeight(1);
  line(x-2, y, x-2, y+13);// ฟัน
  line(x-8, y, x-8, y+10);
  line(x-5, y, x-5, y+12);
  line(x+1, y, x+1, y+12);
  line(x+4, y, x+4, y+10);
  line(x+7, y, x+7, y+10);

  ellipse(x-1, y-5, 8, 10); // ตา
  ellipse(x+7, y-5, 8, 10);
  fill(0);
  ellipse(x-1, y-7, 4, 4);
  ellipse(x+7, y-7, 4, 4);
  strokeWeight(5);
  line(x, y+15, x, y+20);// คอ
  strokeWeight(15);
  line(x+3, y+23, x+10, y+35);// ตัว
  strokeWeight(5);
  line(x-10, y+15, x-30, y-10);  // แขน
  line(x-35, y-10, x-20, y-10);
  line(x+10, y+20, x+35, y+10);  // แขน
  ellipse(x+30, y+13, 5, 5);
  line(x+10, y+35, x+30, y+30); // ขา
  ellipse(x+30, y+30, 5, 10);
  stroke(0);
  strokeWeight(7);
  line(x-63, y-12, x-11, y-12);
}
void manfall(int x, int y) {//ฟังก์ชั่นวาดภาพขาคนตกน้ำ โดยรับพาริมิเตอร์นำไปใช้เป็นพิกัดแกน xและy ในการวาดรูป
  stroke(0);
  strokeWeight(15);// ตัว
  line(x+5, y+18, x+20, y+25);
  strokeWeight(1);
  fill(0);
  ellipse(x, y, 30, 25); //หัว
  fill(255);
  ellipse(x, y+7, 20, 10);// ปาก
  stroke(0);
  ellipse(x-1, y-5, 8, 10); // ตา
  ellipse(x+7, y-5, 8, 10);
  fill(0);
  ellipse(x-3, y-5, 4, 4);
  ellipse(x+4, y-5, 4, 4);
  strokeWeight(5);
  strokeWeight(5);
  line(x+20, y+25, x+35, y+20);//ขา
  line(x+35, y+20, x+40, y+25);
  line(x+35, y+27, x+50, y+27); // เท้า

  line(x+5, y+15, x-10, y+30);  // แขน
  ellipse(x-6, y+25, 5, 5);
  line(x+5, y+15, x+30, y+10);  // แขน
  ellipse(x+25, y+12, 5, 5);
  fill(#2BDED0);
  noStroke();
  ellipse(x-30, y+20, 20, 10);
  ellipse(x+50, y+10, 20, 10);
  fill(#C9FFFB);
  noStroke();
  ellipse(x-20, y+25, 15, 15);
  ellipse(x+42, y+14, 15, 10);
}
void changeSmooth() { //ฟังกชั่นวาดคนในโหมดการเล่นแบบ smooth
  drawMantop();  //เรียกใช้ฟังก์ชั่น drawmantop เพื่อรวาดคนข้างบน
  inc = 1;
  if(rounds>=2){ //ถ้า round มากกว่าหรือเท่ากับ 2 ให้ inc เท่ากับ 2 ดังนั้น คนจะเคลื่อนที่เรวขึ้น
     inc = 2 ;
  }
  if (position[0]+15<= width+80 && position[1] == 125  ) {  //ถ้า array ตำแหน่งที่ 0 ของposition +15 แล้วน้อยกว่าเท่ากับความกว่างหน้าจอ+80 และ array ตำแหน่งที่ 1 เท่ากับ 125
    position[0]+= inc;  //ให้ค่า position[2] บวก inc ไปเรื่อยๆนั่นคือค่า x ของคนจะบวกไปเรื่อย ทำให้เห็นเปนคนเหลื่อนที่ไปในแนวแกน x ทางบวก
  }
  if (position[0]+15>width+70 && position[1] == 125 ) { //ถ้า array ตำแหน่งที่ 0 ของposition +15 แล้วมากกว่าเท่ากับความกว่างหน้าจอ+70 และ array ตำแหน่งที่ 1 เท่ากับ 125
    drawManbottom();   //เรียกใช้ฟังก์ชั่น drawmanbottom เพื่อรวาดคนข้างล่าง
    position[4]-=inc;   //ให้ค่า position[4] ลบ inc ไปเรื่อยๆนั่นคือค่า x ของคนจะบวกไปเรื่อย ทำให้เห็นเปนคนเคลื่อนที่ไปในแนวแกน x ทางลบ
  }
  if (position[4]+15 <= -80 && position[5]== 275 ) { //ถ้า array ตำแหน่งที่ 4 ของposition +15 แล้วน้อยกว่าเท่ากับ -80 และ array ตำแหน่งที่ 5 เท่ากับ 125
    position[0] = -50; //ให้ array ตำแหน่งที่ 0 ของposition เท่ากับ -50
    position[4] = 550; //ให้ array ตำแหน่งที่ 4 ของposition เท่ากับ -50
  }
  if (rounds>=2) { /ถ้า round หรือจานวนรอบ มากกว่าเท่ากับ 2
    drawMantop();  //วาดคนข้างบน กล่าวคือ ข้างบนจะมการววาดดนเดินไป 2 สองคน
    if (position[2]+15<= width+80 ) {
      position[2]+= inc;
    }
    if (position[2]+15>width+70 && position[3] == 125 ) {
      drawManbottom();  //วาดคนข้างล่าง กล่าวคือ ข้างล่างจะมการววาดดนเดินกลับ 2 สองคน
      position[6]-=inc;
    }
    if (position[6]+15 <= -80 && position[7]== 275 ) {
      position[2] = -370;
      position[6] = 620;
    }
  }
  if (position[4]== -20) {
// ถ้า position[1] หรือ ค่า x ของคนข้างล่างน้อยกว่าเท่ากับ 20 ให้ round (จำนวนรอบ) บวกเพิ่มขึ้น 1
    rounds++;
  }
}
void changeStep() {  //ฟังกชั่นวาดคนในโหมดการเล่นแบบ step
  inc = 70;  //ฟังกชั่นวาดคนในโหมดการเล่นแบบ step
  if(rounds>=3){
      divided = 40;
  }
  if (count%divided==0) { //ถ้า count หาร divided  ได้เศษ 0 แล้วจะทำงาน ซึ่งเงื่อไขนี้ใช้เพื่อ หน่วงการเคลื่อนที่ของคน ให้เดินเป็น step
    if (position[0]+15<= width+80 && position[1] == 125) {
      position[0]+= inc;
    }
    if (position[0]+15>width+70 && position[1] == 125 ) {
      position[4]-=inc;
    }
    if (position[4]+15 <= -80 && position[5]== 275 ) {
      position[0]= -100;
      position[4] = 550;
    }
    if (rounds>=2) {
      if (position[2]+15<= width+80 && position[3] == 125) {
        position[2]+= inc;
      }
      if (position[2]+15>width+70 && position[3] == 125 ) {
        position[6]-=inc;
      }
      if (position[6]+15 <= -80 && position[7]== 275 ) {
        position[2]= -370;
        position[6] = 620;
      }
    }

    if (position[4] == -80) {
      rounds++;
    }
  }
 count++;
}
void keyPressed() {  //ฟังก์ชั่นที่ใช้ในการกดปุ่ม
  if (key == 'w' ) {  //สร้างเงื่อนไขว่าถ้ากดปุ่ม w
    if ( position[0]+15 >= checkscoreArea-10 && position[0]+15<= checkscoreArea+50  ) {
//และถ้าตำแหน่งที่2 +15 มีค่ามากกว่าหรือเท่ากับ checkscoreArea-10และตำแหน่งที่2+15มีค่าน้อยกว่าหรือเท่ากับcheckscoreArea+50
      if (position[1] == 125 ) {  //และถ้าค่าตำแหน่งที่3 =125
        scores = scores + 1;  //ให้เพิ่มค่าscoreครั้งละ1
      }
      checkscoreArea = 0;   //ให้เปลี่ยนค่าcheckเท่ากับ 0
    }
    if (position[2]+15 >= checkscoreArea1-10 && position[2]+15<= checkscoreArea1+50) {
 //สร้างเงื่อนไขว่าถ้าตำแหน่งที่4+15มากกว่าหรือเท่ากับcheck1 -10และมีค่าน้อยกว่าหรือเท่ากับcheck1+50
      if (position[3] == 125 ) {  //สร้างเงื่อนไขว่าถ้าค่าของตำแหน่งที่3 =125
        scores = scores + 1;  //ให้เพิ่มค่าscoreครั้งละ1
      }
      checkscoreArea1 = 0;   //เปลี่ยนค่าcheck1เป็น0
    }

    strokeWeight(1);  //ให้น้ำหนักเส้น=1
    drawmanhold(150, 190);  //เรียกฟังก์ชั่นวาดคนมารับโดยกำหนดค่าx,yให้กับฟังก์ชั่น
  }

  if (key == 'd' ) {  //สร้างเงื่อนไขว่าถ้ากดปุ่มd
   if (position[4]-15 >= checkscoreArea+630 && position[4]-15 <= checkscoreArea+697) {
//ถ้าค่าของตำแหน่งที่4-15มากกว่าหรือเท่ากับค่าcheck+360และตำแหน่งที่4-15น้อยกว่าหรือเท่ากับcheck+697
      if (position[5] == 275) { //ถ้าค่าตำแหน่งที่5 เท่ากับ275
        scores = scores + 1;   //ให้เพิ่มค่าscoreครังละ1
      }
      checkscoreArea=-800;
    }
    if (position[6]-15 >= checkscoreArea1+780 && position[6]-15 <= checkscoreArea1+847) {
//ถ้าค่าของตำแหน่งที่6-15มากกว่าหรือเท่ากับcheck1+780และน้อยกว่าหรือเท่ากับcheck1+847
      if (position[7] == 275) { //และถ้าตำแหน่งที่ 7 เท่ากับ275
        scores = scores + 1;   //ให้เพิ่มค่าscoreครังละ1
      }
      checkscoreArea1=-800;
    }
    strokeWeight(1);  //ให้น้ำหนักเส้น=1
    drawmanhold(150, 340); //เรียกฟังก์ชั่นวาดคนมารับโดยกำหนดค่าx,yให้กับฟังก์ชั่น
  }

  if (key == 'o' ) {  //สร้างเงื่อนไขว่าถ้ากดปุ่มo

    if (position[0]+15 >= checkscoreArea+235 &&position[0]+15 <= checkscoreArea+290) {
//ถ้าค่าของตำแหน่งที่0+15มากกว่าหรือเท่ากับcheck+235และน้อยกว่าหรือเท่ากับcheck+290
      if (position[1] == 125) { //และถ้าตำแหน่งที่ 1 เท่ากับ125
        scores = scores + 1;  //ให้เพิ่มค่าscoreครังละ1
      }
      checkscoreArea=-290;
    }
    if (position[2]+15 >= checkscoreArea1+235 &&position[2]+15 <= checkscoreArea1+290) {
//ถ้าค่าของตำแหน่งที่2+15มากกว่าหรือเท่ากับcheck+235และน้อยกว่าหรือเท่ากับcheck+290
      if (position[3] == 125) {//และถ้าตำแหน่งที่ 3 เท่ากับ125
        scores = scores + 1;   //ให้เพิ่มค่าscoreครังละ1
      }
      checkscoreArea1=-510;
    }

    strokeWeight(1);
    drawmanhold(300, 190); //เรียกฟังก์ชั่นวาดคนมารับโดยกำหนดค่าx,yให้กับฟังก์ชั่น
  }
  if (key == 'k' ) {  //สร้างเงื่อนไขว่าถ้ากดปุ่มk

    if (position[4]-15 >= checkscoreArea+525 && position[4]-15 <=checkscoreArea+585) {
      if (position[5]== 275) {
        scores = scores + 1;
      }
      checkscoreArea = -550;
    }
    if (position[6]+15 >= checkscoreArea1+745 && position[6]+15 <=checkscoreArea1+805) {
      if (position[7]== 275) {
        scores = scores + 1;
      }
      checkscoreArea1 = -700;
    }
    strokeWeight(1);
    drawmanhold(300, 340); //เรียกฟังก์ชั่นวาดคนมารับโดยกำหนดค่าx,yให้กับฟังก์ชั่น
  }

  if ( checkscoreArea==-800) { //สร้างเงื่อนไขว่าค่าcheckเท่ากับ-800หรือไม่
    checkscoreArea = 90; //ถ้าเข้าเงื่อนไขให้เปลี่ยนค่าcheck เท่ากับ90
  }
  if ( checkscoreArea1==-800) { //สร้างเงื่อนไขว่าค่าcheck1เท่ากับ-800หรือไม่
    checkscoreArea1 = 90; //ถ้าเข้าเงื่อนไขให้เปลี่ยนค่าcheck1 เท่ากับ90
  }
}


void checkgameover(int divided) {//สร้างฟังก์ชั่นgameoverโดยรับค่าพารามิเตอร์ที่ชื่อว่า divided
  if (count%divided==0) { //สร้างเงื่อนไขถ้าค่าcountหารค่าdividedแล้วเหลือเศษ 0
    if (key != 'w') {  //ถ้าไม่ได้กดปุ่มw
      if (position[0]>= 87&& position[0]<=110   ) {
//ถ้าค่าตำแหน่งที่ 0 มากว่าเท่ากับ87และน้อยกว่าหรือเท่ากับ110
        position[0] =95; //ให้ค่าของตำแหน่งที่ 0 เท่ากับ95
        position[1]+=1; //ให้เพิ่มค่าของตำแหน่งที่1 เพิ่มขึ้นครั้งละ1
      }
      if (position[2]>= 87&& position[2]<=110   ) {
//ถ้าค่าตำแหน่งที่2มากว่าเท่ากับ87และน้อยกว่าหรือเท่ากับ110
        position[2]=95; //ให้ค่าของตำแหน่งที่2เท่ากับ95
        position[3]+=1; //ให้เพิ่มค่าของตำแหน่งที่3เพิ่มขึ้นครั้งละ1
      }
    }
    if (key != 'o') {  //ถ้าไม่ได้กดปุ่มo
      if (position[0] >= 235&& position[0]<=260) {
//ถ้าค่าตำแหน่งที่0มากว่าเท่ากับ235และน้อยกว่าหรือเท่ากับ260
        position[0]=255; //ให้ค่าของตำแหน่งที่0เท่ากับ255
        position[1]+=1; //ให้เพิ่มค่าของตำแหน่งที่1เพิ่มขึ้นครั้งละ1
      }
      if (position[2] >= 235&& position[2]<=260) {
//ถ้าค่าตำแหน่งที่2มากว่าเท่ากับ235และน้อยกว่าหรือเท่ากับ260
        position[2]=255; //ให้ค่าของตำแหน่งที่2เท่ากับ255
        position[3]+=1; //ให้เพิ่มค่าของตำแหน่งที่3เพิ่มขึ้นครั้งละ1
      }
    }
    if (key != 'k') { //ถ้าไม่ได้กดปุ่มk
      if (position[4]>= 240&& position[4]<=270) { //ถ้าค่าตำแหน่งที่4มากว่าเท่ากับ240และน้อยกว่าหรือเท่ากับ270
        position[4]= 275; //ให้ค่าของตำแหน่งที่1เท่ากับ275
        position[5]+=1; //ให้เพิ่มค่าของตำแหน่งที่0เพิ่มขึ้นครั้งละ1
      }
      if (position[6] >= 240&& position[6]<=270) {
 //ถ้าค่าตำแหน่งที่5มากว่าเท่ากับ240และน้อยกว่าหรือเท่ากับ270
        position[6]= 275; //ให้ค่าของตำแหน่งที่6เท่ากับ275
        position[7]+=1; //ให้เพิ่มค่าของตำแหน่งที่7เพิ่มขึ้นครั้งละ1
      }
    }
    if (key != 'd') { //ถ้าไม่ได้กดปุ่มd
      if (position[4]>= 120&& position[4]<=148) {
 //ถ้าค่าตำแหน่งที่4มากว่าเท่ากับ120และน้อยกว่าหรือเท่ากับ148
        position[4] = 130; //ให้ค่าของตำแหน่งที่4เท่ากับ130
        position[5]+=1; //ให้เพิ่มค่าของตำแหน่งที5เพิ่มขึ้นครั้งละ1
      }
      else if (position[6]>= 120&& position[6]<=148) {
//แล้วถ้าตำแหน่งที่6มากกว่าหรือเท่าหับ120และน้อยกว่าหรือเท่ากับ148
        position[6] = 130; //ให้ค่าของตำแหน่งที่6เท่ากับ130
        position[7]+=1; //ให้เพิ่มค่าของตำแหน่งที7เพิ่มขึ้นครั้งละ1
      }
    }
  }
  if (position[1]>125) { //ถ้าค่าตำแหน่งที่1มากกว่า125เพื่อไม่ให้เข้าเงื่อนไขของฟังก์ชัน keypressed();
    position[1]++; //ให้เพิ่มค่าตำแหน่งที่1ครั้งละ1
  }
  if (position[5]>275) { //ถ้าค่าตำแหน่งที่5มากกว่า275เพื่อไม่ให้เข้าเงื่อนไขของฟังก์ชัน keypressed();
    position[5]++; //ให้เพิ่มค่าตำแหน่งที่5ครั้งละ1
  }

  if (position[3]>125) {   //ถ้าค่าตำแหน่งที่3มากกว่า125เพื่อไม่ให้เข้าเงื่อนไขของฟังก์ชัน keypressed();
    position[3]++; //ให้เพิ่มค่าตำแหน่งที่3ครั้งละ1
  }
  if (position[7]>275) {  //ถ้าค่าตำแหน่งที7มากกว่า275เพื่อไม่ให้เข้าเงื่อนไขของฟังก์ชัน keypressed();
    position[7]++; //ให้เพิ่มค่าตำแหน่งที่7ครั้งละ1
  }

  if (position[1] >= 135 || position[5]>=286 || position[3] >= 135 || position[7]>=286) {

   // เชคระยะแกน y มากกว่า 135 หรือ 286 แล้วหรือยัง (ซึ่งตอนนี้ตัวคนที่เดินจะไม่แสดงแล้ว)
 
 if (position[0] == 95 || position[2] == 95 ) {
      drawmanfall(110, 190);
 //เรียกฟังก์ชันวาดรุปคนที่ตกน้ำและจะวาดคนตกน้ำจนกระทั่ง แกน y ของคนเดินด้านบนมากกว่า 200
      if (position[1] > 200 || position[3] > 200) {
//    เมื่อแกน y ของคนเดินด้านบน มากกว่า 200 แล้ว ให้ค่า y เท่ากับ 450 ทำให้คนตกน้ำหายไป
        position[1] = 450;
        position[3] = 450;
      }
    }

    if (position[0] == 255 || position[2] == 255) {
      drawmanfall(255, 190);

      if (position[1]> 200 || position[3]> 200) {
        position[1] = 450;
        position[3] = 450;
      }
    }

    if (position[4] == 130 || position[6] == 130) {
      drawmanfall(110, 340);
 //เรียกฟังก์ชันวาดรุปคนที่ตกน้ำและจะวาดคนตกน้ำจนกระทั่ง แกน y ของคนเดินด้านล่างมากกว่า 350
      if (position[5]> 350 || position[7] > 350) {
//    เมื่อแกน y ของคนเดินด้านบน มากกว่า 350 แล้ว ให้ค่า y เท่ากับ 450 ทำให้คนตกน้ำหายไป
        position[5]= 450;
        position[7] = 450;
      }
    }

    if (position[4]== 275 || position[6]== 275) {
     drawmanfall(255, 340);

      if (position[5] > 350 || position[7] > 350) {
        position[5] = 450;
        position[7] = 450;
      }
    }

  }

}
void otherdisappear(){ // ฟังก์ชันตรวจสอบเงื่อนไขเพื่อให้ตัวละครอื่นๆที่ยังไม่ตกหายไป เมื่อมีตัวละครตัวใดตัวหนึ่งตกแล้ว
          // ***** ไม่สามารถยุบรวมฟังก์ชันได้เนื่องจากคำสั่งที่ทำเมื่อเข้าเงื่อนไขนั้นไม่เหมือนกัน
  if (position[1] >= 126) {
    position[2] = 450;
    position[4] = 450;
    position[6] = 450;
  }
  if (position[3] >= 126) {
    position[0] = 450;
    position[4] = 450;
    position[6] = 450;
  }
  if (position[5] >= 276) {
    position[0] = 450;
    position[2] = 450;
    position[6] = 450;
  }
  if (position[7] >= 276) {
    position[0] = 450;
    position[2] = 450;
    position[4] = 450;
  }
}
void showmissed() {
  if (position[1]==450 || position[5] == 450 || position[3]==450 || position[7] == 450) {
 // ตรวจสอบหากมีแกน y ของคนหนึ่งเท่ากับ 450 ให้ค่า miss เพิ่มขึ้นทีละ 1
    miss++;
  }
  if (position[1] ==450 || position[3] == 450) {
  //ตรวจสอบเงื่อนไขถ้าหากมีแกน y ของตัวที่ 1 หรือ 2  ข้างบน เท่ากับ 450 (ตกน้ำ)
     ให้ทุกตัวที่อยู่ข้างบนเริ่มเคลื่อนที่ใหม่ตามค่าที่กำหนด ต่อไปนี้
    position[1] =125;
    position[0]= -100;
    position[3] = 125;
    position[2] = -370;
    checkscoreArea = 90;
    checkscoreArea1 = 90;
  }
  else if (position[5]== 450 || position[7]== 450) {
  //ตรวจสอบเงื่อนไขถ้าหากมีแกน y ของตัวที่ 1 หรือ 2  ข้างล่าง เท่ากับ 450 (ตกน้ำ)
     ให้ทุกตัวที่อยู่ข้างล่างเริ่มเคลื่อนที่ใหม่ตามค่าที่กำหนด ต่อไปนี้
    position[4]= 550;
    position[5] = 275;
    position[6]= 620;
    position[7] = 275;

    checkscoreArea = -290;
    checkscoreArea1 = -510;
  }

  if (miss>0) {  // ตรวจสอบเงื่อนไขเพื่อวาดรูปหน้าคน หรือ ชีวิตที่เหลืออยุ่ในการเล่นเกม
    noStroke();
    drawmiss(150, 15);
    if (miss >1) {
      drawmiss(185, 15);
    }
 if (miss>2) {
// เมื่อตกน้ำครบ 3 รอบ ให้แสดงข้อความ Game over และ เรียกใช้ฟังก์ชันวาดปุ่ม TryAgain และ Manu
      drawmiss(220, 15);
      fill(#FF6C17);
      textSize(50);
      text("Game Over", (width/4)-20, 140);
      position[1] = 451;
      position[5] = 451;
      position[3] = 451;
      position[7] = 451;
      drawTryAgainbutton();
      drawManubutton();
    }
  }
}

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

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