2023年11月2日 星期四

Week08 啟動

 Week08


-----------------------------------------------------------------------------------------------------------------------------

1.畫出一個往右下移動的物體

//碰撞偵測
void setup(){
  size(400,400);
  background(#9EC7CB);
}
float x=200,y=200,vx=1,vy=1;
void draw(){
  ellipse(x,y,10,10);
  x+=vx;
  y+=vy;
}


2.拋物線(?
//碰撞偵測
void setup(){
  size(400,400);
  background(#9EC7CB);
}
float x=50,y=250,vx=2,vy=-20;
void draw(){
  ellipse(x,y,10,10);
  x+=vx;
  y+=vy;
  vy+=0.98;//重力加速度
}


3.碰到指定位置後會往右邊跑
//碰撞偵測
void setup(){
  size(400,400);
  background(#9EC7CB);
}
float x=50,y=250,vx=2,vy=-20;
void draw(){
  x+=vx;
  y+=vy;
  vy+=0.98;//重力加速度
  if(y>350){//利用if判斷,看有沒有到地板 y=350位置
    y=350;
  }
  ellipse(x,y,10,10);
}


3_2變成像彈跳的樣子
//碰撞偵測
void setup(){
  size(400,400);
  background(#9EC7CB);
}
float x=50,y=250,vx=2,vy=-20;
void draw(){
  x+=vx;
  y+=vy;
  vy+=0.98;//重力加速度
  if(y>350){//利用if判斷,看有沒有到地板 y=350位置
    y=350;
    vy=-vy*0.5;//能量耗損,速度變慢
  }
  ellipse(x,y,10,10);
}



4.讓物體可以被鍵盤左右鍵控制
//碰撞偵測
void setup(){
  size(400,400);
}
float marioX=50,marioY=250,vx=2,vy=-20;
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;
}


5.新增程式碼,放開左右鍵時,物體會停止
//碰撞偵測
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==RIGHT||keyCode==LEFT) vx=0;
}



6.新增程式碼,讓物體跳起來並正常降落
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=-20;
  flying=true;//開始飛行  
  }
}
void keyReleased(){
   if(keyCode==RIGHT||keyCode==LEFT) vx=0;
}



有連續跳耀的問題

6_2上一項有著物體在空中時,連續按上鍵會連續跳耀的問題,新增一點東西將其修正
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==RIGHT||keyCode==LEFT) vx=0;
}



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(229,119,42); rect(0,260,400,150);
  fill(299,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==RIGHT||keyCode==LEFT) 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;
  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(299,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==RIGHT||keyCode==LEFT) vx=0;
}


9.修正上一項跳上方塊後,離開方塊會繼續浮在半空中的問題
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(299,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==RIGHT||keyCode==LEFT) vx=0;
}


10.最終完成版
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);
  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(299,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==RIGHT||keyCode==LEFT) vx=0;
}



沒有留言:

張貼留言