2023年11月2日 星期四

jason1230 week08

1-1


碰撞偵測,利用牛頓力學
設速度vx,vy,讓線能移動

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

1-2

利用位置,速度,加速度
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;
}

1-3

用if判斷是否碰到地板,if(y>350){
    y=350;
  }
只要y的值大於350,碰撞都會設成350
讓物件碰到地板的時候能夠彈起來,vy的速度要變為負的
因為彈起來會消耗動能(摩擦力)



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

1-4

設計背景的顏色,並增加keyPressed,用左右鍵去移動

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,10,20);
   fill(229,119,42);rect(0,260,400,150);
}
void keyPressed(){
  if(keyCode==RIGHT)vx=2;
  if(keyCode==LEFT)vx=-2;
}

2-1

因為原本按移動鍵會停不下來,新增keyReleased按下去會停止(煞車)

void setup(){
  size(400,400);  
}
float marioX=50,marioY=250,vx=0,vy=0;
void draw(){
   background(108,137,255);
   marioX+=vx;
   marioY+=vy;
   //vy+=0.98;
   fill(255,0,0);ellipse(marioX,marioY,10,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;
}

2-2

前面往上跳的話會一直往上衝,要讓vy新增重力加速度


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,10,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==RIGHT)vx=0;
}

2-3

讓角色碰到方塊會停下來,設碰到方塊的範圍(等於角色的碰撞範圍)用hitBox函式,如果碰到方塊,速度會為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,10,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;
}

3-1

讓角色可以跳到方塊上,需要變更hitBox函式

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,10,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;
  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;
}

3-2

新增函式宣告leaveBox讓角色離開方塊可以掉下來


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,10,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){
     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==LEFT || keyCode==RIGHT)vx=0;
}

3-3


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,10,20);
   fill(229,119,42);rect(0,260,400,150);
   drawAndTexBox(100,150,20,20);
   drawAndTexBox(200,150,20,20);
   drawAndTexBox(300,150,20,20);
}
void drawAndTexBox(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 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==LEFT || keyCode==RIGHT)vx=0;
}

沒有留言:

張貼留言