- float bar_x,bar_y,ball_x,ball_y,speed_x,speed_y,brick_x,brick_y,brick_width,brick_height,i,j,up_edge,rnd;
- boolean isPlaying,goLeft,goRight, show1,show2,show3,show4,show5,show6; //使用show變數來決定磚塊消失與否,可用陣列
- int n=0,gameState=1,score=0,best_score=0;
- PFont word;
- void setup(){
-
- size(500,400);
-
- word = createFont("Arial",50);
- textFont(word);
- textAlign(CENTER);
- stroke(0);
- bar_x = random(100,400);//擊板起始位置X座標隨機出現
- bar_y = height-10;//擊板y定在下邊界減10的地方,擊板高度為10
- ball_x= width/2-8;//球出現的x座標
- ball_y= random(50,height/2-50);//球出現的y座標隨機出現
- //球的x移動方向隨機決定,但如果在0.8到負0.8之間時要重做,以免球會呈現幾近垂直的上下
- speed_x =random(-2.5,2.5);
- while(speed_x>=-0.8&&speed_x<=0.8) speed_x =random(-2.5,2.5);
- speed_y = 4;
- isPlaying = false;//用滑鼠點擊後球才會開始移動
- background(255);
-
-
- //磚塊的初始值
- brick_x = 0;
- brick_y = 0;
- brick_width = 80;
- brick_height = 20;
-
- //brick wall 磚牆逐一設定
- stroke(0);
- fill(#FFFF00);
- rect(brick_x,brick_y,brick_width,brick_height);
- fill(#0000FF);
- rect(brick_x+85,brick_y,brick_width,brick_height);
- fill(#9ACD32);
- rect(brick_x+85*2,brick_y,brick_width,brick_height);
- fill(#EE2C2C);
- rect(brick_x+85*3,brick_y,brick_width,brick_height);
- fill(#ffa500);
- rect(brick_x+85*4,brick_y,brick_width,brick_height);
- fill(#FF00FF);
- rect(brick_x+85*5,brick_y,brick_width,brick_height);
- //磚塊目前全部呈現可見狀態
- show1=show2=show3=show4=show5=show6=true;
- //擊板的設定
- noStroke();
- fill(#1E90FF);
- rect(bar_x,bar_y,100,15);
- //球的設定
- stroke(0);
- fill(#DAA520);
- ellipse(ball_x,ball_y,16,16);
-
- //up_edge是指當磚塊存在時的上界,其實就是y==20,相對於磚塊消失後的上界值是y==0
- up_edge = brick_y+20;
- }
- void draw(){
-
- if(gameState==1){
-
- if(keyPressed||mousePressed) isPlaying=true;
-
- if(isPlaying){
- background(255);
-
-
-
- // brick wall
- stroke(0);
- //fill(#ffa500);
- brick_x=0;
- brick_y=0;
- brick_width=80;
- brick_height=20;
- //如果show值為真,畫出各磚塊,可用陣列
- fill(#FFFF00);
- if(show1==true) rect(brick_x,brick_y,brick_width,brick_height);
- fill(#0000FF);
- if(show2==true) rect(brick_x+85,brick_y,brick_width,brick_height);
- fill(#9ACD32);
- if(show3==true) rect(brick_x+85*2,brick_y,brick_width,brick_height);
- fill(#EE2C2C);
- if(show4==true) rect(brick_x+85*3,brick_y,brick_width,brick_height);
- fill(#ffa500);
- if(show5==true) rect(brick_x+85*4,brick_y,brick_width,brick_height);
- fill(#FF00FF);
- if(show6==true) rect(brick_x+85*5,brick_y,brick_width,brick_height);
-
- textFont(word,20);
- fill(#CDC9C9);
- text("SCORE: ",50,380);
- text(score,95,380);
-
-
- //ball
- stroke(0);
- fill(#DAA520);
- ellipse(ball_x,ball_y,16,16);
-
-
-
- ball_x+=speed_x;
- ball_y+=speed_y;
- //如果球朝上則速度是y=-4,如果球向下則速度是y=5
- if(speed_y>0) speed_y=5;;
- if(speed_y<0) speed_y=-4;
- //如果score>18則上下速度加快
- if(score>18){
- //if(speed_x>0) speed_x=2;
- //if(speed_x<0) speed_x=-2;
- if(speed_y>0) speed_y=6;
- if(speed_y<0) speed_y=-5;
- }
- if(score>54){
- //if(speed_x>0) speed_x=2;
- //if(speed_x<0) speed_x=-2;
- if(speed_y>0) speed_y=7;
- if(speed_y<0) speed_y=-5;
- }
-
- //如果球碰到某磚塊,則該磚塊的show值為false,磚塊消失
- rnd=random(0.1,0.3);
- if(show1==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>brick_x && ball_x<80) {speed_y*=-1;show1=false;score++;}
- if(show2==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>86 && ball_x<164) {speed_y*=-1;show2=false;score++;}
- if(show3==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>171 && ball_x<249) {speed_y*=-1;show3=false;score++;}
- if(show4==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>256 && ball_x<334) {speed_y*=-1;show4=false;score++;}
- if(show5==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>341 && ball_x<419) {speed_y*=-1;show5=false;score++;}
- if(show6==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>426) {speed_y*=-1;show6=false;score++;}
- //如果show值都為false時則全部變回true,磚塊全都出現
- if(show1==false&&show2==false&&show3==false&&show4==false&&show5==false&&show6==false&&ball_y>=22)
- {
- show1=true;show2=true;show3=true;show4=true;show5=true;show6=true;}
-
- //如果球要出界時,立即反彈
- if(ball_y-8<=0) {ball_y=8;speed_y*=-1;}
- if(ball_x+8>=width) speed_x*=-1;
- if(ball_x-8<=0) speed_x*=-1;
- //反擊時讓球的x移動以隨機處理,增加球反彈的不確定感
-
- if(ball_x>=bar_x&&ball_x<=bar_x+100&&ball_y+8>=bar_y) {rnd=random(0.7,1.6); speed_y*=-1;speed_x*=rnd;}
-
- if(ball_x-8==bar_x+100&&ball_y+8>=height-10&&ball_y+8>=height) {
- if(speed_x>0){speed_y*=-1;rnd=random(1.1,2);speed_x*=rnd;}
- if(speed_x<0){speed_y*=-1;rnd=random(1.1,2);speed_x*=-rnd;}
- }
- if(ball_x+8==bar_x&&ball_y+8>=height-10&&ball_y+8>=height) {
- if(speed_x>0)
- {speed_y*=-1;rnd=random(1.1,2);speed_x*=-rnd;
- if(speed_x>2.5) speed_x=2.5;
- if(speed_x<-2.5) speed_x=-2.5;
- }
- if(speed_x<0)
- {speed_y*=-1;rnd=random(1.1,2);speed_x*=rnd;
- if(speed_x>3) speed_x=3;
- if(speed_x<-3) speed_x=-3; }
- }
- //limits the max speed of speed_x if necessary
- if(speed_x>3) speed_x=3;
- if(speed_x<-3) speed_x=-3;
- //bar 用滑鼠或鍵盤來控制擊板左右
- stroke(0);
- fill(#1E90FF);
- rect(bar_x,bar_y,100,10);
- if(goLeft) bar_x-=5;
- if(goRight) bar_x+=5;
- if(bar_x<=0) bar_x=0;
- if(mousePressed) {
- { if(mouseX<=bar_x)
- for(int z=0;z<bar_x-mouseX;z++)
- bar_x--;
- }
- if(mouseX>=bar_x)
- for(int z=0;z<mouseX-bar_x;z++)
- bar_x++;
- }
-
- bar_x=mouseX;
-
-
-
- if(bar_x+100>=width) bar_x=width-100;
- if(bar_x<=0) bar_x=0;
-
-
- textFont(word,40);
- fill(#0000FF);
- if(ball_y-8>=height) ball_y=500;
- fill(255,0,0);
- //一旦球的y值大於下界表示球未被擊板接住,遊戲結束
- if(ball_y>450) {text("GAME OVER",width/2,180);isPlaying = false;gameState=0;
- if(score>best_score) best_score=score;
- textFont(word,20);
- fill(#FFA500);
- text("Your scores are:",220,220);
- fill(255,0,0);
- text(score,310,220);
- fill(#FFA500);
- text("Your best scores are:",210,240);
- fill(255,0,0);
- text(best_score,320,240);
- textFont(word,30);
- fill(#006400);
- text("Play again? Y/N",250,280);
- }
- }
- }
- }
- void keyPressed(){
- if(gameState==0)
- if(key=='y'||key=='Y'){
- gameState=1;
- ball_x= width/2-8;//球出現的x座標
- ball_y= random(50,height/2-50);//球出現的y座標隨機出現
- speed_x =random(-2,2);
- while(speed_x>=-0.5&&speed_x<=0.5) speed_x =random(-2,2);
- bar_x=width/2;
- show1=show2=show3=show4=show5=show6=true;
- score=0;
- }
- if(gameState==0)
- if(key=='n'||key=='N'){
- background(0);
- textFont(word,50);
- fill(#ffff00);
- text("Thanks for playing\n bye bye.",250,150);
- }
- textFont(word,20);
- fill(255);
- text("made by SYC @ http://code.club",250,300);
-
- if(key==CODED)
- {
-
-
- switch(keyCode){
- case LEFT:
- goLeft= true;
- break;
- case RIGHT:
- goRight= true;
- break;
- }
- }
-
- }
- void keyReleased(){
- if(key==CODED){
- switch(keyCode){
- case LEFT:
- goLeft = false;
- break;
- case RIGHT:
- goRight=false;
- break;
- }
- }
- }
- void mousePressed(){
- if(key=='n'||key=='N') {link("http://code.club");}
- }
- void mouseClicked(){
- isPlaying=true;
- }
複製代碼 |