期末作業
期末作業接近尾聲了,但還是有一些問題,而且還換了一種寫法,努力在dayline前做完!
程式碼
PImage img1,img2,img3,img4;//讀入圖片
float []bombX=new float[100];//用陣列計算砲彈生成數和速度
float []bombY=new float[100];
float []bombVX=new float[100];
float []bombVY=new float[100];
boolean []BombDisappear = new boolean[100];
int bx[]= new int[6];//紀錄船的xy座標
int by[]= new int[6];
int count=3000;//一秒=60frame
int score=0;//初始分數為0
int rec=0;//紀錄哪個魚雷
PFont myFont;//讀入字體
boolean flag = true;//判斷是否生成物件
int temp = 0;//設定打到船隻的計數器
class Boat{//發明Boat物件
float x,y,vx;
boolean dead, flag;
Boat(){//建構同名的東西
x = -100;
y = random(300,600);
vx = random(1,3);
dead = false;//沒有死掉
flag = false;
}
void draw(){
if(dead==true)
{
BombDisappear[rec]=false;//魚雷不消失
return;//不畫船
}
image(img2,x-120,y-80,250,120);//由左至右的船,調整中心點位置
for(int i=0;i<100;i++)
{
if(dist(bombX[i]+100,bombY[i]+50,x,y)<40)//調整炸彈中心點位置
{
rec=i;//紀錄發射魚雷和擊中的船隻為同一個
dead=true;//判斷死亡
score+=100;//判斷加分
BombDisappear[rec]=true;//判斷魚雷打到就消失
bombX[i]+=1000;
bombY[i]+=1000;
}
}
x = x + vx;
if(x>850)//超出界定範圍,就重新隨機生成
{
x=-200;
y=random(300,650);
vx=random(1,3);
}
}
}
class Boat1{//發明Boat1物件
float x1,y1,vx1;
boolean dead, flag;
Boat1(){//建構同名的東西
x1 = 900;
y1 = random(300,600);
vx1 = random(-3,-1);
dead = false;//沒有死掉
}
void draw(){
if(dead==true)
{
BombDisappear[rec]=false;//魚雷不消失
return;//不畫船
}
image(img3,x1-120,y1-50,200,80);//由右至左的船,調整中心點位置
for(int i=0;i<100;i++)
{
if(dist(bombX[i]+100,bombY[i]+50,x1,y1)<40)//調整炸彈中心點位置
{
rec=i;//紀錄發射魚雷和擊中的船隻為同一個
dead=true;//判斷死亡
score+=100;//判斷加分
BombDisappear[rec]=true;//判斷魚雷打到就消失
bombX[i]+=1000;
bombY[i]+=1000;
}
}
x1 = x1 + vx1;
if(x1<-200)//超出界定範圍,就重新隨機生成
{
x1=1000;
y1=random(300,650);
vx1=random(-3,-1);
}
}
}
Boat[]ships;
Boat1[]ships1;
float x2=300,y2=85;//船隻起始位置
int N=0;
void setup(){
size(800,800);
img1=loadImage("boat.png");//船
img4=loadImage("bomb.png");//魚雷
img2=loadImage("deep1.png");//由右至左的潛水艇
img3=loadImage("deep.png");//由左至右的潛水艇
myFont = createFont("微軟正黑體",30);//讀入需要的中文字體
textFont(myFont);
for(int i=0;i<100;i++)BombDisappear[i]=false;
}
void draw()
{
println(temp);
if(temp == 6)
{
flag = true;
temp = 0;
}
if(flag == true)//當temp=6時,flag就會被判斷為true,然後生成船隻物件
{
ships = new Boat[3];
ships1 = new Boat1[3];
for(int i=0;i<3;i++)
{
ships[i]= new Boat();//生成船隻
ships1[i]= new Boat1();
ships[i].x=bx[i];//紀錄xy座標
ships[i].y=by[i];
ships1[i].x1=bx[i+3];//0,1,2被用過,需+3
ships1[i].y1=by[i+3];
}
flag=false;//在被重置之前,所有的船隻物件只會生成一次
}
background(#C9E4F0);//天空顏色
fill(#225BD3);rect(0,200,800,1000);//海洋顏色與調整位置
image(img1,x2,y2,300,200);//船隻大小
if(keyPressed && keyCode==LEFT)x2-=2;//船隻向左移動
if(keyPressed && keyCode==RIGHT)x2+=2;//船隻向右移動
for(int i=0;i<3;i++) //左邊船隻
{
if(ships[i] == null) continue;//如果物件被更新為空值,就不再去畫船隻
if (ships[i].dead == true)
{
ships[i] = null;//用null設定刪除被打到的船隻物件
temp++;
}
else
ships[i].draw();
}
for(int i=0;i<3;i++) //右邊船隻
{
if(ships1[i] == null) continue;//如果物件被更新為空值,就不再去畫船隻
if (ships1[i].dead == true)
{
ships1[i] = null;//用null設定刪除被打到的船隻物件
temp++;
}
else
ships1[i].draw();
}
for(rec=0;rec<N;rec++)
{
if(BombDisappear[rec]==false)
{
bombY[rec]+=bombVY[rec];//更新魚雷的y座標
image(img4,bombX[rec],bombY[rec],200,50);//魚雷圖
}
}
fill(#030303);textSize(30);//結算畫面背景顏色和字型大小
text("剩餘時間:"+int(count/60)+"秒",600,50);//倒數計時器
if(count<=0)//結算畫面
{
background(0,255,255);
fill(0);textSize(80);text("遊戲結束",240,300);
fill(0);textSize(40);text("分數: "+score+"分",300,500);//計算總共分數
}
else{
count--;
}
}
void mousePressed()//魚雷生成
{
N++;
bombX[N-1]=x2+50;bombY[N-1]=y2+110;//每次魚雷都會從船隻發射
bombVX[N-1]=0;bombVY[N-1]=3;
}
沒有留言:
張貼留言