2023年11月2日 星期四

chotwo_week08

 Week08  馬力歐

    week08_1_neton_x_y_vx_vy

▼ 畫一個等速度的球

                // 碰撞偵測: Mario vs. Pikachu // 牛頓力學 (1)靜者恆靜、動者恆動

  week08_1_neton_x_y_vx_vy


▼ 拋物線

(2) f=m*a 位置、速度、加速度

    week08_3_neton_floor_if_collision


▼ 偵測地板碰撞

▼ 能量會耗損,減速(vy)

▼ 能量會耗損,減速(vx)

    week08_4_mario_keyPressed_keyCode_UP_RIGHT_LEFT



▼ mario左右跑


    week08_5_mario_keyReleased_keyCode_UP_RIGHT_LEFT


▼ mario往上跳





    week08_7_mario_hit_box_if


▼ 如果撞到box就不能再往上飛



    week08_8_mario_vy_direction_stand_fall


▼ 離開box後跳一下可以往下墜

// 碰撞偵測: Mario vs. Pikachu 
// 牛頓力學 (1)靜者恆靜、動者恆動 (2) f=m*a 位置、速度、加速度
void setup(){
  size(400,400);
}
float marioX=50, marioY=250, vx=0, vy=0;
void draw(){
  background(108,137,255);
  marioX += vx;
  if(flying){  //如果在飛行
    marioY += vy; // 上下位置會改變
    vy += 0.98; // 重力加速度往下
    if(marioY >= 250){ // 碰到地板
      marioY =250;  // 站在地上
      flying = false; // 不再飛行
    }
  }
  fill(7,96,163); ellipse(marioX, marioY, 15,25);
  fill(255,0,0); ellipse(marioX, marioY-10, 10,10);
  fill(229,119,42); rect(0, 260, 400, 150);
  fill(229,119,42); rect(200, 150, 20, 20);
  if(hitBox(200,150, 20,20)){
    if(vy > 0){
      marioY = 150-10;
      flying = false;
    }else{
    vy = 0;
    marioY = 150+20+10;
    }
  }
}
boolean hitBox(int x, int y, int w, int h){
  if(x-7<marioX && marioX<x+w+7 && y-10<marioY && marioY<y+h+10) return true;
  else return false;
}
boolean flying = false;  // 初始,沒有在飛
void keyPressed(){
  if(keyCode==RIGHT) vx = 2;
  if(keyCode==LEFT) vx = -2;
  if(keyCode==UP && flying==false) { // 沒有在飛才能飛
    vy = -20;
    flying = true; // 開始飛行
  }
}
void keyReleased(){
  if(keyCode==LEFT || keyCode==RIGHT) vx = 0;
}

    week08_9_mario_stand_box_and_fall



▼ 離開box後直接往下墜

// 碰撞偵測: Mario vs. Pikachu 
// 牛頓力學 (1)靜者恆靜、動者恆動 (2) f=m*a 位置、速度、加速度
void setup(){
  size(400,400);
}
float marioX=50, marioY=250, vx=0, vy=0;
void draw(){
  background(108,137,255);
  marioX += vx;
  if(flying){  //如果在飛行
    marioY += vy; // 上下位置會改變
    vy += 0.98; // 重力加速度往下
    if(marioY >= 250){ // 碰到地板
      marioY =250;  // 站在地上
      flying = false; // 不再飛行
    }
  }
  fill(7,96,163); ellipse(marioX, marioY, 15,25);
  fill(255,0,0); ellipse(marioX, marioY-10, 10,10);
  fill(229,119,42); rect(0, 260, 400, 150);
  fill(229,119,42); rect(200, 150, 20, 20);
  if(hitBox(200,150, 20,20)){
    if(vy > 0){
      marioY = 150-10;
      flying = false;
      stand_box = true;
    }else{
    vy = 0;
    marioY = 150+20+10;
    }
  }
  if(stand_box==true && flying==false &&leaveBox(200,150,20,20)){
    stand_box = false;
    flying = true;
    vy = 0;
  }
}
boolean leaveBox(int x, int y, int w, int h){
  if(x-7>marioX || marioX>x+w+7) return true;
  else return false;
}
boolean hitBox(int x, int y, int w, int h){
  if(x-7<marioX && marioX<x+w+7 && y-10<marioY && marioY<y+h+10) return true;
  else return false;
}
boolean flying = false, stand_box = false;  // 初始,沒有在飛
void keyPressed(){
  if(keyCode==RIGHT) vx = 2;
  if(keyCode==LEFT) vx = -2;
  if(keyCode==UP && flying==false) { // 沒有在飛才能飛
    vy = -20;
    flying = true; // 開始飛行
  }
}
void keyReleased(){
  if(keyCode==LEFT || keyCode==RIGHT) vx = 0;
}

    

    
week08_A_mario_stand_box_and_fall


▼ 多加兩個箱子,增加一個drawAndTestBox函式
在void draw()內呼叫三個drawAndTestBox(x,y,寬,高);
// 碰撞偵測: Mario vs. Pikachu 
// 牛頓力學 (1)靜者恆靜、動者恆動 (2) f=m*a 位置、速度、加速度
void setup(){
  size(400,400);
}
float marioX=50, marioY=250, vx=0, vy=0;
void draw(){
  background(108,137,255);
  marioX += vx;
  if(flying){  //如果在飛行
    marioY += vy; // 上下位置會改變
    vy += 0.98; // 重力加速度往下
    if(marioY >= 250){ // 碰到地板
      marioY =250;  // 站在地上
      flying = false; // 不再飛行
    }
  }
  fill(7,96,163); ellipse(marioX, marioY, 15,25);
  fill(255,0,0); ellipse(marioX, marioY-10, 10,10);
  fill(229,119,42); rect(0, 260, 400, 150);
  drawAndTestBox(100,150, 20,20);
  drawAndTestBox(200,150, 20,20);
  drawAndTestBox(300,150, 20,20);void drawAndTestBox(int x, int y, int w, int h){
    fill(229,119,42); rect(x, y, w, h);
    if(hitBox(x, y, w, h)){
      if(vy > 0){
        marioY = y-10;
        flying = false;
        stand_box = true;
      }else{
        vy = 0;
        marioY = y+h+10;
      }
  }
  if(stand_box==true && flying==false &&leaveBox(x, y, w, h)){
    stand_box = false;
    flying = true;
    vy = 0;
  }
}
boolean leaveBox(int x, int y, int w, int h){
  if(x-7>marioX || marioX>x+w+7) return true;
  else return false;
}
boolean hitBox(int x, int y, int w, int h){
  if(x-7<marioX && marioX<x+w+7 && y-10<marioY && marioY<y+h+10) return true;
  else return false;
}
boolean flying = false, stand_box = false;  // 初始,沒有在飛
void keyPressed(){
  if(keyCode==RIGHT) vx = 2;
  if(keyCode==LEFT) vx = -2;
  if(keyCode==UP && flying==false) { // 沒有在飛才能飛
    vy = -20;
    flying = true; // 開始飛行
    stand_box = false; //現在有兩個變數都要處理
  }
}
void keyReleased(){
  if(keyCode==LEFT || keyCode==RIGHT) vx = 0;
}

沒有留言:

張貼留言