2023年11月2日 星期四

Week08

 //碰撞偵測

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;

}//只會往下 好無聊

float x=20,y=250,vx=2,vy=-20;
void draw(){
  ellipse(x,y,10,10);
  x+=vx;
  y+=vy;
  vy+= 0.98;//重力加速度
}

void draw(){
  x+=vx;
  y+=vy;
  vy+= 0.98;
  if(y>400){
    y=400;
    vy=-vy*0.8;
  }//偵測到地板並彈跳
  ellipse(x,y,10,10);
}




void setup(){
  size(400,400);
}
float marioX=20,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(229,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==RIGHT||keyCode==LEFT) vx=0;
}

void keyPressed(){
  if(keyCode==RIGHT) vx=2;
  if(keyCode==LEFT) vx=-2;
  if(keyCode==UP&&flying==false){//不能連跳 
    vy=-20;
    flying = true;
  }
}

fill(229,119,42);rect(200,150,20,20);箱子

  if(hitBox(200,150,20,20)){

    vy=0;

    marioY= 150+20;

  }

}

boolean flying = false;

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;

}//碰撞箱判定

if(hitBox(200,150,20,20)){

    if(vy>0){

    marioY= 150-10;

    flying=false;

    }else{

    vy=0;

    marioY= 150+20+10;

    }

  }

}

boolean flying = 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;

}//能站在盒子上但有問題


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 flying = false,stand_box=false;
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;
}

fill(255,0,0);ellipse(marioX,marioY,15,20);
  fill(229,119,42);rect(0,260,400,150);
  drawAndBox(200,150,20,20);
  drawAndBox(100,150,20,20);
  drawAndBox(300,150,20,20);
}
void drawAndBox(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;
  }
}//將碰撞箱做成含式


沒有留言:

張貼留言