//看有沒有碰到地板
//碰撞偵測 Mario vs Pikachu
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的位置
{
y = 350;
vy = -vy*0.9;//碰撞後 y方向耗損的能量(速度耗損) 變慢
}
ellipse(x ,y ,10,10);
}
x方向會慢慢跑 但會一直抖動
step 01-5 馬力歐初始狀態 左鍵往左 右鍵往右 但會一直動
void setup()
{
size(400,400);//視窗大小
}
float marioX=50,marioY=250, vx=0, vy=0;//初速度=0
void draw()
{
background(108,137,255);
marioX += vx;
ellipse(marioX,marioY,10,20);
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;
}
step02-1 馬力歐左鍵放開就不會跑了 往上鍵馬力歐就跑走囉
void setup()
{
size(400,400);//視窗大小
}
float marioX=50,marioY=250, vx=0, vy=0;//初速度=0
void draw()
{
background(108,137,255);
marioX += vx;
marioY += vy;//vy += 0.98
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;
}
step 02-2馬力歐可以做飛行 向下鍵 馬力歐可以往下
要解決不會彈出去框框外 沒有在飛才可以飛
void setup()
{
size(400,400);//視窗大小
}
float marioX=50,marioY=250, vx=0, vy=0;//初速度=0
void draw()
{
background(108,137,255);
marioX += vx;
if( flying )//如果在飛行中
{
marioY += vy;//vy += 0.98 上下的位置會改變
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;
}
step 02-3 馬力歐如果碰到box 速度歸0 馬力歐會躲在box下面 不能飛上去
void setup()
{
size(400,400);//視窗大小
}
float marioX=50,marioY=250, vx=0, vy=0;//初速度=0
void draw()
{
background(108,137,255);
marioX += vx;
if( flying )//如果在飛行中
{
marioY += vy;//vy += 0.98 上下的位置會改變
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) )//馬力歐碰撞到box的偵測
{
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;
}
step03-1 馬力歐可以站在box上面 碰到box不會有殘影
void setup()
{
size(400,400);//視窗大小
}
float marioX=50,marioY=250, vx=0, vy=0;//初速度=0
void draw()
{
background(108,137,255);
marioX += vx;
if( flying )//如果在飛行中
{
marioY += vy;//vy += 0.98 上下的位置會改變
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) )//馬力歐碰撞到box的偵測
{
if(vy>0)
{
vy = 0;
marioY = 150-10;//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;
}
step 03-2 馬力歐站在box上面後 可以跳下去
void setup()
{
size(400,400);//視窗大小
}
float marioX=50,marioY=250, vx=0, vy=0;//初速度=0
void draw()
{
background(108,137,255);
marioX += vx;
if( flying )//如果在飛行中
{
marioY += vy;//vy += 0.98 上下的位置會改變
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) )//馬力歐碰撞到box的偵測
{
if(vy>0)
{
vy = 0;
marioY = 150-10;//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;
}
step 03-3 做出三個方塊 完成馬力歐
void setup()
{
size(400,400);//視窗大小
}
float marioX=50,marioY=250, vx=0, vy=0;//初速度=0
void draw()
{
background(108,137,255);
marioX += vx;
if( flying )//如果在飛行中
{
marioY += vy;//vy += 0.98 上下的位置會改變
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) )//馬力歐碰撞到box的偵測
{
if(vy>0)
{
vy = 0;
marioY = y-10;//150寬度 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;//現在有2個變數 都要處理
}
}
void keyReleased()
{
if(keyCode==LEFT || keyCode==RIGHT) vx=0;
}
沒有留言:
張貼留言