WEEK08
Step01
讓圓點往右下移動
//碰撞偵測 //牛頓力學(1)靜者恆靜、動者恆動 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) f=m*a 位置、速度、加速度 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)靜者恆靜、動者恆動,(2) f=m*a 位置、速度、加速度 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); }
能量損耗
//碰撞偵測 //牛頓力學(1)靜者恆靜、動者恆動,(2) f=m*a 位置、速度、加速度 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;//能量耗損、速度變慢 } ellipse(x,y,10,10); }
做出馬力歐
//碰撞偵測 //牛頓力學(1)靜者恆靜、動者恆動,(2) f=m*a 位置、速度、加速度 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,25); fill(220,119,42); rect(0,260,400,150); } void keyPressed(){ if(keyCode==RIGHT)vx=2; if(keyCode==LEFT)vx=-2; }
Step02
做出正常的跳躍效果
//碰撞偵測 //牛頓力學(1)靜者恆靜、動者恆動,(2) f=m*a 位置、速度、加速度 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,25); 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; }
做出一個碰撞箱
//碰撞偵測 //牛頓力學(1)靜者恆靜、動者恆動,(2) f=m*a 位置、速度、加速度 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,25); 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; }
讓碰撞箱做得更好
//碰撞偵測 //牛頓力學(1)靜者恆靜、動者恆動,(2) f=m*a 位置、速度、加速度 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; 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; }
讓碰撞箱可以站在上面且可以掉下來
//碰撞偵測 //牛頓力學(1)靜者恆靜、動者恆動,(2) f=m*a 位置、速度、加速度 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; 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; }
讓碰撞箱的程式碼變成變數,可以快速做出很多碰撞箱
//碰撞偵測 //牛頓力學(1)靜者恆靜、動者恆動,(2) f=m*a 位置、速度、加速度 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(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;//開始飛 } } void keyReleased(){ if(keyCode==LEFT|| keyCode==RIGHT)vx=0; }
沒有留言:
張貼留言