蟑螂已經發育完全!
今天的任務是增加遊戲界面,讓遊戲看起來更豐富,目前的功能還尚未齊全,我會陸續地增加和修改美編。
圖(1)是開始介面,點擊圖中的"Start"按鈕即可進入遊玩的介面!
圖(2)是遊玩的介面,點擊右下角可返回上一頁開始介面。
圖(3)是結束畫面,點擊Restart可以重新開始遊戲。
![]() |
| 圖(1) |
![]() |
| 圖(3) |
程式碼如下:
float x,y,vx,vy,x2,y2,vx2,vy2;
int count=600;///計時器的總共時間
int score=0;
int save[]={0,1,2,3};
PFont myFont;///讀入字體
PImage img1,img2,img3,img4,img5;//讀入圖檔
void setup(){
size(500,500);
img1=loadImage("start1.png");//讀入圖片
img2=loadImage("restart1.png");
img3=loadImage("bg.png");//背景圖片
img4=loadImage("Cockroach.png");
img5=loadImage("restart.png");//返回鍵
generateCockroach();//一般的蟑螂
generateCockroach2();//特別的蟑螂
fill(255,0,0);
textSize(80);
myFont = createFont("微軟正黑體",30);///中文輸入法程式碼
}
int condition;//情況
void mousePressed() {///場景切換
if(condition==0){
if (mouseButton == LEFT&&dist(150,150, mouseX-120, mouseY-75)<50){ // 切換到開始遊戲介面
condition=1;
}
}
if(condition==2){
if (mouseButton == LEFT&&dist(175,175, mouseX-70, mouseY-70)<50) { // 切換到開始遊戲介面
condition=0;
}
}
if(condition==1){
if(mouseButton==LEFT&& dist(400,400,mouseX-100,mouseY-100)<50){
condition=0;//返回鍵
score=0;
}
}
}
void draw(){
if(condition==0){
background(255,192,103);
image(img1,75,75,350,350);
if (mouseButton == LEFT&&dist(200,200, mouseX, mouseY)<60){ // 切換到開始遊戲介面
condition=1;
}
}
if(condition==1){//condition=1且時間>0=遊戲介面
image(img3,0,0,500,500);
image(img4,x,y,300,300);//蟑螂的大小
image(img5,450,450,50,50);//返回鍵
x+=vx; y+=vy;//若超過左邊界、右邊界,則會隨機產生一般蟑螂。
x2+=vx2; y2+=vy2;//若超過左邊界、右邊界,則會隨機產生特別蟑螂。
if(frameCount%10==0){//每一秒,改一次角度
float angle = atan2(vy,vx);
//float angle2=(angle+radians(90));//時間到就轉90度。
float angle2=(angle+radians(random(-30,30)));//時間到就轉30度。
vx =cos(angle2);//一般蟑螂
vy =sin(angle2);//一般蟑螂
vx2=sin(angle2);//特別蟑螂
vy2=cos(angle2);//特別蟑螂
}
if(x<0 || y<0||x>500||y>500)generateCockroach();//不管邊界x,y<0或x,y>500,都會產生新的蟑螂。
noFill(); ellipse(mouseX,mouseY,50,50);//產生藍白拖。
if(mousePressed && dist(mouseX-150,mouseY-150,x,y)<40){
generateCockroach();//死後產生新的蟑螂。
score+=10;///分數加10分
}//一般蟑螂
if(frameCount>180&&frameCount<360){
fill(0);rect(x2,y2,20,20);//特別的蟑螂
if(x2<0 || y2<0||x2>500||y2>500)generateCockroach2();//不管邊界x,y<0或x,y>500,都會產生新的蟑螂。
if(mousePressed && dist(mouseX,mouseY,x2,y2)<40){
generateCockroach2();//死後產生新的特別蟑螂。
score+=100;//分數加100分
}
}//特別蟑螂
textFont(myFont);
text("分數:"+score,350,45);
fill(255,255,255);
textSize(36);
text("Time:" +int(count/60)+"秒",50,45); ///計時器。
count--;
if(count<=0){
save[0]=score;
if(save[1]>save[0]){
save[1]=save[0];
}
condition=2;//結束介面
count+=600;
score=0;
}
}
if(condition==2){//結束畫面
background(186,85,211);
image(img2,10,5,500,500);
textFont(myFont,80);
text("遊戲結束",80,160);
}
}
void generateCockroach(){//一般的蟑螂
int edge=int (random(4));//4個邊
if(edge==0){
x=300;y=random(300);vx=-random(1,3);vy=random(-3,+3);
}else if(edge==1){
x=random(300);y=300;vx=-random(-3,3);vy=random(1,3);
}else if(edge==2){
x=0;y=random(300);vx=random(1,3);vy=random(-3,+3);
}else if(edge==3){
x=random(300);y=0;vx=random(-3,3);vy=random(1,3);
}
}
void generateCockroach2(){///特別的蟑螂
int edge2=int (random(4));//4個邊
if(edge2==0){
x2=300;y2=random(300);vx=-random(1,3);vy=random(-3,+3);
}else if(edge2==1){
x2=random(300);y2=300;vx=-random(-3,3);vy=random(1,3);
}else if(edge2==2){
x2=0;y2=random(300);vx=random(1,3);vy=random(-3,+3);
}else if(edge2==3){
x2=random(300);y2=0;vx=random(-3,3);vy=random(1,3);
}
}



沒有留言:
張貼留言