2023年11月2日 星期四

LS._. 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;
}

 碰撞偵測:拋物線

  • 另存剛剛的程式碼,寫出一個拋物線
  • 程式碼
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;
}

碰撞偵測:碰撞偵測

  • 另存剛剛的程式碼,寫出碰撞偵測(看有沒有碰到地板)
  • 碰到地板彈起來
  • 到一個點後會一直彈跳
  • 程式碼
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;
  if(y>350){
    y=350;
    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 keyReleased(){
  if(keyCode==LEFT||keyCode==LEFT)vx=0;
}

馬力歐:往上飛

  • 另存剛剛的程式碼,讓馬力歐能向上移動
  • 程式碼
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==LEFT)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&&flying==false){
    vy=-20;
    flying=true;
  }
}
void keyReleased(){
  if(keyCode==LEFT||keyCode==LEFT)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);
  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;
  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==LEFT)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);
  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==LEFT)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);
  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;
  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==LEFT)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);
  drawAndTextBox(100,150,20,20);
  drawAndTextBox(200,150,20,20);
  drawAndTextBox(300,150,20,20);
}
void drawAndTextBox(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=150-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;
  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;
    stand_box=false;
  }
}
void keyReleased(){
  if(keyCode==LEFT||keyCode==LEFT)vx=0;
}

上傳程式碼

  • 將程式碼上傳至github.com

沒有留言:

張貼留言