2023年11月2日 星期四

互動技術 week08

week08-1 

 //碰撞偵測 //牛頓力學

void setup(){

   size(400,400); 

}

float x=200,y=200,vx=1,vy=1;

void draw(){

   ellipse(x,y,10,10); 

   x += vx;

   y += vy;

}

week08-2

拋物線

//碰撞偵測

//牛頓力學

void setup(){

   size(400,400); 

}

float x=50,y=250,vx=2,vy=-20;

void draw(){

   ellipse(x,y,10,10); 

   x += vx;

   y += vy;

   vy += 0.98;//重力加速度

}


week08-3

Y=350;停止下墜

//碰撞偵測

//牛頓力學

void setup(){

   size(400,400); 

}

float x=50,y=250,vx=2,vy=-20;

void draw(){

   x += vx;

   y += vy;

   vy += 0.98;

   if(y>350)

   {

      y=350; 

   }

   ellipse(x,y,10,10); 

}


week08-3-2

//碰撞偵測

//牛頓力學

void setup(){

   size(400,400); 

}

float x=50,y=250,vx=2,vy=-20;

void draw(){

   x += vx;

   y += vy;

   vy += 0.98;

   if(y>350)

   {

      y=350; 

      vy= -vy*0.5;

     //vx = vx*0.8;

   }

   ellipse(x,y,10,10); 

}


week08-4

//碰撞偵測

//牛頓力學

void setup(){

   size(400,400); 

}

float mirroX=50,mirroY=250,vx=0,vy=0;

void draw(){

  background(108,137,255);

    mirroX+=vx;

    ellipse(mirroX,mirroY,10,20);

    fill(255,0,0);ellipse(mirroX,mirroY,15,20);

    fill(209,119,42);rect(0,260,400,150);

}

void keyPressed(){

    if(keyCode==RIGHT)vx = 2; //往右速度2

    if(keyCode==LEFT)vx=-2; //往左速度-2

}



week08-5 往右,左,上會飛走

//碰撞偵測

//牛頓力學

void setup(){

   size(400,400); 

}

float mirroX=50,mirroY=250,vx=0,vy=0;

void draw(){

  background(108,137,255);

    mirroX+=vx;

    mirroY+=vy;

    //vy+=0.98;

    ellipse(mirroX,mirroY,10,20);

    fill(255,0,0);ellipse(mirroX,mirroY,15,20);

    fill(209,119,42);rect(0,260,400,150);

}

void keyPressed(){

    if(keyCode==RIGHT)vx = 2;

    if(keyCode==LEFT)vx=-2;

    if(keyCode==UP)vy=-20;

}

void keyReleased(){

   if(keyCode==LEFT || keyCode==RIGHT)vx=0; 

}

week08-6

//碰撞偵測
//牛頓力學
void setup(){
   size(400,400); 
}
float mirroX=50,mirroY=250,vx=0,vy=0;
void draw(){
  background(108,137,255);
    mirroX+=vx;
    if(flying){//如果再飛
    mirroY+=vy;//上下位置會改變
    vy+=0.98;//重力加速度往下
    }
    if(mirroY>=250){//碰到地板
       mirroY = 250;
       flying = false;//不再飛行
    }

    fill(255,0,0);ellipse(mirroX,mirroY,15,20);
    fill(209,119,42);rect(0,260,400,150);
}
boolean flying = false;//一開始沒在飛
void keyPressed(){
    if(keyCode==RIGHT)vx = 2;
    if(keyCode==LEFT)vx=-2;
    if(keyCode==UP){//小心大括號 //在空中可跳
      vy=-20;
      flying=true;//開始飛
    }
}
void keyReleased(){
   if(keyCode==LEFT || keyCode==RIGHT)vx=0; 
}


week08-6-2 

//碰撞偵測

//牛頓力學

void setup(){

   size(400,400); 

}

float mirroX=50,mirroY=250,vx=0,vy=0;

void draw(){

  background(108,137,255);

    mirroX+=vx;

    if(flying){//如果再飛

    mirroY+=vy;//上下位置會改變

    vy+=0.98;//重力加速度往下

    }

    if(mirroY>=250){//碰到地板

       mirroY = 250;

       flying = false;//不再飛行

    }


    fill(255,0,0);ellipse(mirroX,mirroY,15,20);

    fill(209,119,42);rect(0,260,400,150);

}

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-7 碰撞

//碰撞偵測
//牛頓力學
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(255,0,0);ellipse(marioX,marioY,15,20);
    fill(209,119,42);rect(0,260,400,150);
    fill(209,119,42);rect(200,150,20,20);
    if(hitBox(200,150,20,20)){// 碰撞判斷
       vy=0;
       marioY = 150+20;
    }
}
boolean hitBox(int x,int y,int w,int h){
   if(x<marioX && marioX<x+w && y<marioY && marioY<y+h)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-8

void setup(){
  size(400,400);
}
float marioX=50, marioY=250, vx=0, vy=0;///vx,vy剛開始速度=0
void draw(){
  background(108,137,255);
  marioX += vx;
  if(flying){//如果在飛行
    marioY += vy;///Y方向位置會改變
    vy += 0.98;////重力加速度
    if(marioY >= 250){//碰到地板
      marioY = 250;//站在地上
      flying = false;///不再飛行
    }
  }
  fill(255,0,0); ellipse( marioX, marioY, 15, 20);///馬力歐本人
  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;//馬力歐的厚度
  ///碰到方塊的下邊界
  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
void setup(){
  size(400,400);
}
float marioX=50, marioY=250, vx=0, vy=0;///vx,vy剛開始速度=0
void draw(){
  background(108,137,255);
  marioX += vx;
  if(flying){//如果在飛行
    marioY += vy;///Y方向位置會改變
    vy += 0.98;////重力加速度
    if(marioY >= 250){//碰到地板
      marioY = 250;//站在地上
      flying = false;///不再飛行
    }
  }
  fill(255,0,0); ellipse( marioX, marioY, 15, 20);///馬力歐本人
  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){///離開box
  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;
  ///碰到方塊的下邊界
  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-10 加其他方塊

void setup(){

  size(400,400);

}

float marioX=50, marioY=250, vx=0, vy=0;///vx,vy剛開始速度=0

void draw(){

  background(108,137,255);

  marioX += vx;

  if(flying){//如果在飛行

    marioY += vy;///Y方向位置會改變

    vy += 0.98;////重力加速度

    if(marioY >= 250){//碰到地板

      marioY = 250;//站在地上

      flying = false;///不再飛行

    }

  }

  fill(255,0,0); ellipse( marioX, marioY, 15, 20);///馬力歐本人

  fill(229,119,42); rect(0,260,400,150);

  drawAndTestBox(200,150,20,20);

  drawAndTestBox(250,150,20,20);///多加一個方塊

}

boolean leaveBox(int x,int y,int w,int h){///離開box

  if (x-7>marioX || marioX>x+w+7 )return true;

  else return false;

}

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 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;

  ///碰到方塊的下邊界

  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;

}




沒有留言:

張貼留言