2023年11月2日 星期四

week08 Ray

 

WEEK 8


step 1-1  往右下角有殘影



step 1-2 加上重力




step 1-3 加底

 


step 1-4 Y軸耗損



step 1-5 X軸耗損

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){ //利用if判斷有無到地板
    y=350;
    vy = -vy * 0.5; //能量耗損
    vx = vx * 0.8;
  }
  ellipse(x,y,10,10);
}


step 2-1 左右走動馬力歐




step 2-2 放開按鍵時不會繼續跑,但是跳起還會停不住



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


step 2-4 放方塊加上碰撞箱


step 2-5 離開箱子會落下




step 2-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);
  drawAndTextBox(200,150,20,20);
  drawAndTextBox(100,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=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;//加7 -7mario身體 +10 -10上下邊界
  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;
}

沒有留言:

張貼留言