
利用布林變數控制遊戲是否結束
int life=10;
boolean gg=false;
void setup(){
size(500,500);
}
void draw(){
background(0,0,205);
fill(255); rect(50,50,100,20);
fill(255,0,0); rect(50,50,life*10,20);
if(gg){
textSize(40);
text("Game Over",150,150);
}
}
void mousePressed(){
life--;
if(life<=0) gg=true;
}
2.開場畫面切換



int state=0;
int remaintime=0;
void setup(){
size(800,600);
}
void draw(){
if (state==0){
op();
if(key=='1') {state=1; remaintime=5*60;}
}
else if(state==1){
play();
}
else if(state==2){
ed();
}
}
void op(){
background(0);
fill(#F5DE07);
textSize(30); text("Press 1 to play",10,100);
}
void play(){
background(#F0D90C);
fill(#161FF2); text("Press 2 to end",10,100);
text("remain: "+remaintime,10,150);
remaintime--;
if(remaintime<=0) state=2;
}
void ed(){
background(0);
fill(#F0D90C); text("Game Over",10,100);
}
將(remaintime+59)/60 可以換成一般秒數
沒+59的話會和實際秒數延遲一秒
沒有留言:
張貼留言