code.club

標題: 學習 processing 語言很不錯 [打印本頁]

作者: return    時間: 2015-11-7 23:52
標題: 學習 processing 語言很不錯
本帖最後由 return 於 2015-11-8 00:36 編輯

最近接觸了一種視覺化的基於JAVA的語言叫 processing,覺得不算難學,很快可以上手,做一些小遊戲。

最近做了很簡單的打磚塊和幾A幾B這種猜數字的小遊戲,頗有點成就感。只是程式本身就像一個充滿了補丁的衣服一樣,缺什麼補什麼,錯哪裡再改哪裡,所以十分凌亂,但執行起來還「堪用」吧。

打磚塊的遊戲 http://q.to/pong
只有一排磚,打完了會再出現。一開始請用滑鼠點一下圖中間,然後用滑鼠或者左右鍵來移動下方的板子。球往上時比較慢,下墜時會加快。可能會有些bug,請多指教。

有興趣學程式語言的版友也不妨去玩看看。

http://processing.org

政大最近有開線上課程,http://programming101.cs.nccu.edu.tw/index.html

作者: return    時間: 2021-7-21 06:52
  1. 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;
  2. boolean isPlaying,goLeft,goRight, show1,show2,show3,show4,show5,show6; //使用show變數來決定磚塊消失與否,可用陣列
  3. int n=0,gameState=1,score=0,best_score=0;
  4. PFont word;
  5. void setup(){
  6.   
  7.   size(500,400);
  8.   
  9.   word = createFont("Arial",50);
  10.   textFont(word);
  11.   textAlign(CENTER);
  12.   stroke(0);
  13.   bar_x = random(100,400);//擊板起始位置X座標隨機出現
  14.   bar_y = height-10;//擊板y定在下邊界減10的地方,擊板高度為10
  15.   ball_x= width/2-8;//球出現的x座標
  16.   ball_y= random(50,height/2-50);//球出現的y座標隨機出現
  17.   //球的x移動方向隨機決定,但如果在0.8到負0.8之間時要重做,以免球會呈現幾近垂直的上下
  18.   speed_x =random(-2.5,2.5);
  19.   while(speed_x>=-0.8&&speed_x<=0.8) speed_x =random(-2.5,2.5);
  20.   speed_y = 4;
  21.   isPlaying = false;//用滑鼠點擊後球才會開始移動
  22.   background(255);
  23.   

  24.   //磚塊的初始值  
  25.   brick_x = 0;
  26.   brick_y = 0;
  27.   brick_width = 80;
  28.   brick_height = 20;
  29.   
  30.   //brick wall 磚牆逐一設定
  31.   stroke(0);
  32.   fill(#FFFF00);
  33.   rect(brick_x,brick_y,brick_width,brick_height);
  34.   fill(#0000FF);
  35.   rect(brick_x+85,brick_y,brick_width,brick_height);
  36.   fill(#9ACD32);
  37.   rect(brick_x+85*2,brick_y,brick_width,brick_height);
  38.   fill(#EE2C2C);
  39.   rect(brick_x+85*3,brick_y,brick_width,brick_height);
  40.   fill(#ffa500);
  41.   rect(brick_x+85*4,brick_y,brick_width,brick_height);
  42.   fill(#FF00FF);
  43.   rect(brick_x+85*5,brick_y,brick_width,brick_height);
  44.   //磚塊目前全部呈現可見狀態
  45.   show1=show2=show3=show4=show5=show6=true;
  46.   //擊板的設定
  47.   noStroke();
  48.   fill(#1E90FF);
  49.   rect(bar_x,bar_y,100,15);
  50.   //球的設定
  51.   stroke(0);
  52.   fill(#DAA520);
  53.   ellipse(ball_x,ball_y,16,16);
  54.   
  55.   //up_edge是指當磚塊存在時的上界,其實就是y==20,相對於磚塊消失後的上界值是y==0
  56.   up_edge = brick_y+20;

  57.   }


  58. void draw(){
  59.      
  60.    if(gameState==1){
  61.   
  62.   if(keyPressed||mousePressed) isPlaying=true;
  63.   
  64.   if(isPlaying){
  65.   background(255);
  66.   
  67.   
  68.   
  69.   // brick wall
  70.   stroke(0);
  71.   //fill(#ffa500);
  72.   brick_x=0;
  73.   brick_y=0;
  74.   brick_width=80;
  75.   brick_height=20;

  76.   //如果show值為真,畫出各磚塊,可用陣列
  77.   fill(#FFFF00);
  78.   if(show1==true) rect(brick_x,brick_y,brick_width,brick_height);
  79.   fill(#0000FF);
  80.   if(show2==true) rect(brick_x+85,brick_y,brick_width,brick_height);
  81.   fill(#9ACD32);
  82.   if(show3==true) rect(brick_x+85*2,brick_y,brick_width,brick_height);
  83.   fill(#EE2C2C);
  84.   if(show4==true) rect(brick_x+85*3,brick_y,brick_width,brick_height);
  85.   fill(#ffa500);
  86.   if(show5==true) rect(brick_x+85*4,brick_y,brick_width,brick_height);
  87.   fill(#FF00FF);
  88.   if(show6==true) rect(brick_x+85*5,brick_y,brick_width,brick_height);
  89.   
  90.   textFont(word,20);
  91.   fill(#CDC9C9);
  92.   text("SCORE: ",50,380);
  93.   text(score,95,380);
  94.   


  95.   //ball
  96.   stroke(0);
  97.   fill(#DAA520);
  98.   ellipse(ball_x,ball_y,16,16);
  99.   
  100.   
  101.   
  102.   ball_x+=speed_x;
  103.   ball_y+=speed_y;
  104.   //如果球朝上則速度是y=-4,如果球向下則速度是y=5
  105.   if(speed_y>0) speed_y=5;;
  106.   if(speed_y<0) speed_y=-4;
  107.   //如果score>18則上下速度加快
  108.   if(score>18){
  109.    //if(speed_x>0) speed_x=2;
  110.    //if(speed_x<0) speed_x=-2;
  111.    if(speed_y>0) speed_y=6;
  112.    if(speed_y<0) speed_y=-5;
  113.   }
  114.    if(score>54){
  115.    //if(speed_x>0) speed_x=2;
  116.    //if(speed_x<0) speed_x=-2;
  117.    if(speed_y>0) speed_y=7;
  118.    if(speed_y<0) speed_y=-5;
  119.   }
  120.   
  121.     //如果球碰到某磚塊,則該磚塊的show值為false,磚塊消失
  122.      rnd=random(0.1,0.3);
  123.      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++;}
  124.      if(show2==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>86 && ball_x<164) {speed_y*=-1;show2=false;score++;}
  125.      if(show3==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>171 && ball_x<249) {speed_y*=-1;show3=false;score++;}
  126.      if(show4==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>256 && ball_x<334) {speed_y*=-1;show4=false;score++;}
  127.      if(show5==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>341 && ball_x<419) {speed_y*=-1;show5=false;score++;}
  128.      if(show6==true && (ball_y-8<=up_edge||ball_y-8<=0) && ball_x>426) {speed_y*=-1;show6=false;score++;}
  129. //如果show值都為false時則全部變回true,磚塊全都出現
  130.   if(show1==false&&show2==false&&show3==false&&show4==false&&show5==false&&show6==false&&ball_y>=22)
  131.    {
  132.      show1=true;show2=true;show3=true;show4=true;show5=true;show6=true;}
  133.    
  134.   //如果球要出界時,立即反彈   
  135.   if(ball_y-8<=0) {ball_y=8;speed_y*=-1;}   
  136.   if(ball_x+8>=width) speed_x*=-1;
  137.   if(ball_x-8<=0) speed_x*=-1;
  138.   //反擊時讓球的x移動以隨機處理,增加球反彈的不確定感
  139.   
  140.   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;}
  141.   
  142.   if(ball_x-8==bar_x+100&&ball_y+8>=height-10&&ball_y+8>=height) {
  143.   if(speed_x>0){speed_y*=-1;rnd=random(1.1,2);speed_x*=rnd;}
  144.   if(speed_x<0){speed_y*=-1;rnd=random(1.1,2);speed_x*=-rnd;}
  145.   }
  146.   if(ball_x+8==bar_x&&ball_y+8>=height-10&&ball_y+8>=height) {
  147.   if(speed_x>0)
  148.    {speed_y*=-1;rnd=random(1.1,2);speed_x*=-rnd;
  149.     if(speed_x>2.5) speed_x=2.5;
  150.      if(speed_x<-2.5) speed_x=-2.5;
  151.    }
  152.   if(speed_x<0)
  153.    {speed_y*=-1;rnd=random(1.1,2);speed_x*=rnd;
  154.     if(speed_x>3) speed_x=3;
  155.      if(speed_x<-3) speed_x=-3; }
  156.   }
  157.   //limits the max speed of speed_x if necessary
  158.      if(speed_x>3) speed_x=3;
  159.      if(speed_x<-3) speed_x=-3;
  160.     //bar 用滑鼠或鍵盤來控制擊板左右
  161.   stroke(0);
  162.   fill(#1E90FF);
  163.   rect(bar_x,bar_y,100,10);
  164.   if(goLeft) bar_x-=5;
  165.   if(goRight) bar_x+=5;
  166.   if(bar_x<=0) bar_x=0;
  167.   if(mousePressed) {
  168.     { if(mouseX<=bar_x)
  169.      for(int z=0;z<bar_x-mouseX;z++)
  170.      bar_x--;
  171.         }
  172.   if(mouseX>=bar_x)
  173.       for(int z=0;z<mouseX-bar_x;z++)
  174.        bar_x++;
  175.   }
  176.    
  177.     bar_x=mouseX;
  178.   
  179.   
  180.   
  181.   if(bar_x+100>=width) bar_x=width-100;
  182.   if(bar_x<=0) bar_x=0;
  183.   
  184.   
  185.   textFont(word,40);
  186.   fill(#0000FF);
  187.   if(ball_y-8>=height) ball_y=500;
  188.   fill(255,0,0);
  189.   //一旦球的y值大於下界表示球未被擊板接住,遊戲結束
  190.     if(ball_y>450) {text("GAME OVER",width/2,180);isPlaying = false;gameState=0;
  191.    if(score>best_score) best_score=score;
  192.     textFont(word,20);
  193.    fill(#FFA500);
  194.    text("Your scores are:",220,220);
  195.    fill(255,0,0);
  196.    text(score,310,220);
  197.    fill(#FFA500);
  198.    text("Your best scores are:",210,240);
  199.    fill(255,0,0);
  200.    text(best_score,320,240);
  201.    textFont(word,30);
  202.    fill(#006400);
  203.    text("Play again? Y/N",250,280);
  204.     }
  205. }


  206. }

  207. }

  208. void keyPressed(){
  209.   if(gameState==0)
  210.       if(key=='y'||key=='Y'){
  211.        gameState=1;
  212.        ball_x= width/2-8;//球出現的x座標
  213.   ball_y= random(50,height/2-50);//球出現的y座標隨機出現
  214.   speed_x =random(-2,2);
  215.   while(speed_x>=-0.5&&speed_x<=0.5) speed_x =random(-2,2);
  216.   bar_x=width/2;
  217.   show1=show2=show3=show4=show5=show6=true;
  218.   score=0;
  219.       }
  220.     if(gameState==0)
  221.       if(key=='n'||key=='N'){
  222.       background(0);
  223.       textFont(word,50);
  224.    fill(#ffff00);
  225.       text("Thanks for playing\n bye bye.",250,150);
  226.       }   
  227.        textFont(word,20);
  228.        fill(255);
  229.        text("made by SYC @ http://code.club",250,300);
  230.      
  231.   if(key==CODED)
  232.   {
  233.      
  234.    
  235.     switch(keyCode){
  236.       case LEFT:
  237.         goLeft= true;
  238.         break;
  239.       case RIGHT:
  240.         goRight= true;
  241.         break;
  242.     }
  243.   }

  244. }
  245. void keyReleased(){
  246.   if(key==CODED){
  247.     switch(keyCode){
  248.       case LEFT:
  249.        goLeft = false;
  250.        break;
  251.       case RIGHT:
  252.        goRight=false;
  253.        break;
  254.     }
  255.   }
  256. }
  257. void mousePressed(){
  258. if(key=='n'||key=='N') {link("http://code.club");}

  259. }

  260. void mouseClicked(){
  261. isPlaying=true;
  262. }
複製代碼





歡迎光臨 code.club (https://code.club/) Powered by Discuz! X3.2