期末作品製作
炸潛水艇
1.船和炸彈的操控
▲船隻左右移動
image(img1,x,y,300,200);//船隻大小
if(keyPressed && keyCode==LEFT)x-=1.5;//船隻向左移動
if(keyPressed && keyCode==RIGHT)x+=1.5;//船隻向右移動
▲滑鼠點擊發射
void mousePressed()
{
N++;
bombX[N-1]=x+50;bombY[N-1]=y+110;//每次砲彈都會從船隻發射
bombVX[N-1]=0;bombVY[N-1]=1;
}
▲用陣列設定潛水艇的變數
float []boatX=new float[3];//潛水艇由左至右的xy座標和速度
float []boatY=new float[3];
float []boatVX=new float[3];
float []boatX1=new float[3];//潛水艇由右至左xy座標和速度
float []boatY1=new float[3];
float []boatVX1=new float[3];
void setup()
{
for(int i=0 ; i<3 ; i++)//亂數生成潛水艇位置和速度
{
boatX[i]=random(-200,-200);//潛水艇從左方產生
boatY[i]=random(300,600);
boatVX[i]=random(1,2);
boatX1[i]=random(800,800);//潛水艇從右方產生
boatY1[i]=random(300,600);
boatVX1[i]=random(-2,-1);
}
}
3.新增倒數計時器和結算畫面
fill(#030303);textSize(30);
text("剩餘時間:"+int(count/60)+"秒",600,50);//倒數計時器
if(count<=0)//結算畫面
{
background(0,255,255);
fill(0);textSize(80);text("Game Over",180,300);
fill(0);textSize(40);text("Score: ",300,500);
}
else{
count--;
}
▲預期製作進度
1.船隻隨機生成(完成)
2.砲彈和潛水艇的碰撞偵測(碰撞條件有問題,需做更正)
3.結束畫面(完成)
4.倒數計時器(完成)
5.分數計算(和碰撞偵測一併完成)
★目前程式碼★
float x=300,y=85;//船隻起始位置
float []bombX=new float[100];//用陣列計算砲彈生成數和速度
float []bombY=new float[100];
float []bombVX=new float[100];
float []bombVY=new float[100];
float []boatX=new float[3];//潛水艇由左至右的xy座標和速度
float []boatY=new float[3];
float []boatVX=new float[3];
float []boatX1=new float[3];//潛水艇由右至左xy座標和速度
float []boatY1=new float[3];
float []boatVX1=new float[3];
PImage img1,img2,img3,img4;
boolean []die={false,false,false};
int count=3000;
int score=0;
PFont myFont;//讀入字體
int N=0;
void setup()
{
size(800,800);//視窗大小
img1=loadImage("boat.png");//船
img2=loadImage("deep1.png");//潛水艇
img3=loadImage("deep.png");//反向潛水艇
img4=loadImage("bomb.png");//魚雷
for(int i=0 ; i<3 ; i++)//亂數生成潛水艇位置和速度
{
boatX[i]=random(-200,-200);//潛水艇從左方產生
boatY[i]=random(300,600);
boatVX[i]=random(1,2);
boatX1[i]=random(800,800);//潛水艇從右方產生
boatY1[i]=random(300,600);
boatVX1[i]=random(-2,-1);
}
myFont = createFont("微軟正黑體",30);//讀入需要的中文字體
textFont(myFont);
}
void draw()
{
background(#C9E4F0);//天空顏色
fill(#225BD3);rect(0,200,800,1000);//海洋顏色與調整位置
image(img1,x,y,300,200);//船隻大小
if(keyPressed && keyCode==LEFT)x-=1.5;//船隻向左移動
if(keyPressed && keyCode==RIGHT)x+=1.5;//船隻向右移動
for(int i=0;i<3;i++)
{
boatX[i]+=boatVX[i];
image(img2,boatX[i],boatY[i],250,120);//由左至右潛水艇大小和座標
if(dist(bombX[i],bombY[i],boatX[i],boatY[i])<50)
{
die[i]=true;
}
if(boatX[i]>850)//由左至右重新隨機生成
{
boatX[i]=-200;
boatY[i]=random(300,750);
boatVX[i]=random(1,2);
}
if(die[i])//這段判斷需改
{
boatX[i]=0;
boatY[i]=random(300,750);
boatVX[i]=random(1,2);
}
}
for(int i=0;i<3;i++)
{
boatX1[i]+=boatVX1[i];
image(img3,boatX1[i],boatY1[i],200,80);//由右至左潛水艇大小和座標
if(dist(bombX[i],bombY[i],boatX1[i],boatY1[i])<50)
{
die[i]=true;
}
if(boatX1[i]<-200)//由右至左重新隨機生成
{
boatX1[i]=1000;
boatY1[i]=random(300,750);
boatVX1[i]=random(-2,-1);
}
if(die[i])//這段判斷需改
{
boatX1[i]=800;
boatY1[i]=random(300,750);
boatVX1[i]=random(-2,-1);
}
}
for(int i=0;i<N;i++)
{
bombY[i]+=bombVY[i];
image(img4,bombX[i],bombY[i],200,50);
if(bombY[i]>800)
{
for(int k=i;k<N-1;k++)//此迴圈作為回收砲彈用途,陣列從0計算,0...i...N-1,補中間的空洞,需左移補上
{
bombX[k]=bombX[k+1];
bombY[k]=bombY[k+1];
bombVX[k]=bombVX[k+1];
bombVY[k]=bombVY[k+1];
}
N--;
}
}
println(N);
fill(#030303);textSize(30);
text("剩餘時間:"+int(count/60)+"秒",600,50);//倒數計時器
if(count<=0)//結算畫面
{
background(0,255,255);
fill(0);textSize(80);text("Game Over",180,300);
fill(0);textSize(40);text("Score: ",300,500);
}
else{
count--;
}
}
void mousePressed()
{
N++;
bombX[N-1]=x+50;bombY[N-1]=y+110;//每次砲彈都會從船隻發射
bombVX[N-1]=0;bombVY[N-1]=1;
}
沒有留言:
張貼留言