2023年11月2日 星期四

Hank - week08

 1 . 


2 . 新增拋物線

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




3 . 給他限制

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;
  }
  ellipse(x, y, 10, 10);
}




4 . 讓彈跳正常反應
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.6;//讓能量越來越小
  }
  ellipse(x, y, 10, 10);
}



5 . 做出Mario簡單樣式
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;
   }
   
}



6 . 新增跳躍
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;
   
}


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



8 . 將連續跳躍的Bug修改掉
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==RIGHT) vx = 0;
   
}



9 . 新增一個Box 餅且發生碰撞時會影響到mario的行動
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;
      if(marioY>150+10) marioY = 150 + 20;
      if(marioY<150+10) marioY = 150;
  }
}
boolean flying = false;
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;
}
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;
   
}

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


11 . 解決飛到上面卡住
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 && !flying && 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 flying = false, stand_box=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;
}
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;
   
}



12 . 新增三個方格
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 && !flying && 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 flying = false, stand_box=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;
}
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;
   
}



沒有留言:

張貼留言