2023年11月2日 星期四

bb-week08

 //碰撞偵測:Mario vs, Pikachu

//牛頓力學 
void setup(){
  size(400,400);
}
float x=200,y=200;
void draw(){
    ellipse(x,y,10,10);
}
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;
}









      vy = -vy*0.5;能量耗損速度變慢
      vx =  vx * 0.8;

void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=0,vy=0;
void draw(){
    background(108,137,255);
    marioX += vx;
    fill(255,0,0); ellipse(marioX,marioY,15,20);
    fill(229,119,42); rect(0,260,400,150);
    }
void keyPressed(){
 if(keyCode==RIGHT) vx =  2;
 if(keyCode==LEFT)  vx = -2;
}

使用左右鍵可以移動但還不能停下來
void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=0,vy=0;
void draw(){
    background(108,137,255);
    marioX += vx;
    marioY += vy;
    fill(255,0,0); ellipse(marioX,marioY,15,20);
    fill(229,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;  
  }
可以停下來了但是往上跳不能掉下來

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(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 = -5;
 flying = true;//開始飛行
 }
}
void keyReleased(){
 if(keyCode==LEFT || keyCode==RIGHT) vx = 0;  
  }

 if(keyCode==UP && flying== false){ 
 vy = -20;
加上就不能一直跳
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(229,119,42); rect(0,260,400,150);
    fill(229,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;  
  }
加上方塊並且撞到會停下來









可以把小球停在方塊上了但是左右移動無法掉落下來
                      
這樣就可以掉落下來了


沒有留言:

張貼留言