极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14025|回复: 1

Processing之旅-----【给鱼插上翅膀,Processing基础扩展库5】

[复制链接]
发表于 2013-7-19 11:04:00 | 显示全部楼层 |阅读模式
今天我们继续学习Processing的自带库--Serial库(串口通信库)。
串口通信相对来说一个比较重要的库,因为应用比较多。毕竟Processing+Arduino的应用组合是当前十分受欢迎的一个模式。
下面我就从一个例子中感受一下Serial库带来的震撼效果。
本例中用到了碰撞检测(边界检测),飘雪系统,音效,弧线轨迹等。
实际上用底层手段,粗糙的模拟了一套2d游戏框架(有点吹大了)。不过确实是这样的。
总体来说,技术含量居中,属于介于学年设计,毕业设计的水平。
希望大家有所参考吧。

  1. import ddf.minim.*;
  2. import processing.serial.*;
  3. Table myGameSave;
  4. Serial myPort = new Serial(this,"COM20", 9600);
  5. Minim minim= new Minim(this);
  6. AudioPlayer BKMplayer;
  7. AudioPlayer BZSplayer;
  8. AudioPlayer CJBplayer;
  9. AudioPlayer TTplayer;
  10. AudioPlayer WDplayer;
  11. AudioPlayer XYplayer;

  12. PImage mybackground;
  13. PImage biaoti;
  14. PImage xuehua1;
  15. PImage xuehua2;
  16. PImage xuehua3;
  17. PImage myMan;
  18. PImage myManDao;
  19. PImage myGoldBi;
  20. PImage citie;
  21. PImage danyanguai;
  22. PImage dashouguai;
  23. PImage kuloutou;
  24. PImage lansexiaoguai;
  25. PImage xiaoji;
  26. PImage xiaoxiaoji;
  27. PImage xueren;
  28. PImage youling;
  29. PImage zhadan;
  30. PFont myFont;
  31. int stage=0;
  32. String GameName="饥饿的机器人";
  33. int PlayNowTime;
  34. int GameLiveTime;
  35. int GameRank;
  36. int GoldNum;
  37. int AddGoldNum;
  38. int m_LanFormMove;
  39. int BestGameRank;
  40. int BestGameGoldNum;
  41. int CreateGoldTime;
  42. int CreateObstacleTime;

  43. // float noiseScale=0.005;//地形变化指数
  44. int xspacing = 8;   // How far apart should each horizontal location be spaced
  45. int w;              // Width of entire wave
  46. int maxwaves = 1;   // total # of waves to add together
  47. float theta = 0.0;
  48. float[] amplitude = new float[maxwaves];   // Height of wave
  49. float[] dx = new float[maxwaves];          // Value for incrementing X, to be calculated as a function of period and xspacing
  50. float[] yvalues;                           // Using an array to store height values for the wave (not entirely necessary)
  51. int WindowsX;
  52. int WindowsY;
  53. int RobotManX;
  54. int RobotManY;
  55. int realRobotManX;
  56. int realRobotManY;
  57. int GoldX;
  58. int GoldY;
  59. int ObstacleX;
  60. int ObstacleY;
  61. int xuanyunshijian;
  62. int wudishijian;
  63. int  Arduinokey;
  64. boolean ArduinoKeyA;
  65. boolean ArduinoKeyB;
  66. float mylinek = 0.0;
  67. boolean  IsObstacleisLive;
  68. boolean IsGoldisLive;
  69. float  RobotManJump;
  70. int [][]ThingMap=new int[10][2];
  71. xuehua []m_xiaxuehuazu=new xuehua[10];
  72. void setup()
  73. {
  74.   myGameSave=loadTable("Save.csv","header");
  75.   println("myGameSave.getRowCount(): "+myGameSave.getRowCount());
  76.   if (myGameSave.getRowCount()==1)
  77.   {
  78.        TableRow newRow=myGameSave.addRow();
  79.        newRow.setInt("GameRankSave",0);
  80.        newRow.setInt("GameGoldNumSave",0);
  81.        saveTable(myGameSave,"data/Save.csv");
  82.   }
  83.   
  84.   size(1024, 768,P3D);
  85.   //
  86.   BKMplayer=minim.loadFile("bkmusic.mp3");
  87.   BZSplayer=minim.loadFile("baozhasheng.wav");
  88.   CJBplayer=minim.loadFile("chijinbi.wav");
  89.   TTplayer=minim.loadFile("tantiao.wav");
  90.   WDplayer=minim.loadFile("wudiyinxiao.wav");
  91.   XYplayer=minim.loadFile("xuanyun.wav");
  92.   //Ani.init(this);//初始化ani库
  93.   w = width +160;
  94.   for (int i = 0; i < maxwaves; i++)
  95.     {
  96.     amplitude[i] = random(0,10);
  97.     float period = random(800,1500); // How many pixels before the wave repeats
  98.     dx[i] = (TWO_PI / period) * xspacing;
  99.   }
  100.   yvalues = new float[w/xspacing];
  101.   frameRate(60);
  102.   WindowsX=0;
  103.   WindowsY=0;
  104.   myFont=createFont("微软雅黑",32);
  105.   mybackground=loadImage("background.jpg");
  106.   biaoti=loadImage("biaoti.png");
  107.   myMan=loadImage("man.png");
  108.   myManDao=loadImage("mandao.png");
  109.   myGoldBi=loadImage("goldbi.png");//1,2,3
  110.   xuehua1=loadImage("xuehua1.png");
  111.   xuehua2=loadImage("xuehua2.png");
  112.   xuehua3=loadImage("xuehua3.png");
  113.   citie=loadImage("citie.png");//5
  114.   danyanguai=loadImage("danyanguai.png");//10
  115.   dashouguai=loadImage("dashouguai.png");//15
  116.   lansexiaoguai=loadImage("lansexiaoguai.png");//20
  117.   xiaoji=loadImage("xiaoji.png");//25
  118.   xiaoxiaoji=loadImage("xiaoxiaoji.png");//30
  119.   xueren=loadImage("xueren.png");//35
  120.   youling=loadImage("youling.png");//40
  121.   zhadan=loadImage("zhadan.png");//45
  122.   kuloutou=loadImage("kuloutou.png");//50
  123.   textFont(myFont);
  124.   GameRank=0;
  125.   GameLiveTime=12;
  126.   GoldNum=0;
  127.   m_LanFormMove=0;
  128.   RobotManX=150;
  129.   for (int i = 0; i<10; i++){
  130.       m_xiaxuehuazu[i]=new xuehua();
  131.   }
  132.   BKMplayer.loop();
  133.   BeginGame();
  134.   
  135. }

  136. void draw()
  137. {
  138.   // image(mybackground, 0, 0, width, height);
  139.   // image(myMan, 0, 0, 100, 100);
  140.   GetArduinoKey();
  141.   switch (stage)
  142.   {
  143.     case 0 ://游戏开始选择画面
  144.     {
  145.      
  146.       BeginGame();
  147.       xiaxue();
  148.       break;
  149.     }
  150.     case 1 ://游戏开始
  151.     {
  152.       PlayGame();
  153.       PrintText();
  154.       xiaxue();
  155.       break;
  156.     }
  157.     case 2://制作人员
  158.     {
  159.       CreditsPage();
  160.       xiaxue();
  161.       break;
  162.     }
  163.     case 3://游戏结束
  164.     {
  165.       GameOver();
  166.       xiaxue();
  167.       break;
  168.     }
  169.     case 4 ://游戏胜利
  170.     {
  171.       GameWin();
  172.       xiaxue();
  173.       break;  
  174.     }
  175.     default :
  176.     {
  177.       break;  
  178.     }
  179.   
  180.          
  181.   }  

  182.    
  183. }

  184. void keyPressed()
  185. {
  186.   switch (stage)
  187.   {
  188.     case 0 ://游戏开始选择画面
  189.     {
  190.       if (key=='a'||key=='A')
  191.       {
  192.         stage=1;
  193.         InitGame();
  194.       }
  195.       if (key=='b'||key=='B')
  196.       {
  197.         CreditsPage();
  198.       }
  199.       break;
  200.     }
  201.     case 1 ://游戏开始
  202.     {
  203.       if (key=='b'||key=='B')
  204.       {
  205.         
  206.         if (wudishijian>0)
  207.         {
  208.             WDplayer.loop(0);
  209.             wudishijian=0;
  210.         }
  211.         else
  212.         {
  213.           WDplayer.loop(0);
  214.           wudishijian=2;
  215.         }
  216.         
  217.         EliminateObstacle();
  218.         println("ok");  
  219.       }
  220.       if (key=='a'||key=='A')
  221.       {
  222.         TTplayer.loop(0);
  223.         RobotManJump=5;
  224.         TTplayer.play();
  225.       }
  226.       break;
  227.     }
  228.     case 2://制作人员
  229.     {
  230.       if(keyCode>0)
  231.       {
  232.         BeginGame();
  233.       }
  234.       break;
  235.     }
  236.     case 3://游戏结束
  237.     {
  238.       if (key=='a'||key=='A')
  239.       {
  240.         stage=0;
  241.         BeginGame();
  242.       }
  243.       if (key=='b'||key=='B')
  244.       {
  245.         exit();
  246.       }
  247.       break;
  248.     }
  249.     case 4://游戏胜利
  250.     {
  251.       if (key=='a'||key=='A')
  252.       {
  253.         WindowsX=0;
  254.         WindowsY=0;
  255.         GameRank++;

  256.         InitGame();
  257.       }
  258.       if (key=='b'||key=='B')
  259.       {
  260.         exit();
  261.       }
  262.       break ;
  263.     }   
  264.          
  265.   }  
  266. }
  267. void BeginGame()//开始游戏界面
  268. {
  269.   stage=0;
  270.   // image(mybackground, 0,0, width, height);
  271.   MoveBackImage(WindowsX++,WindowsY++);
  272.   if (WindowsX>mybackground.width-width)
  273.   {
  274.     WindowsX=0;  
  275.   }
  276.   if (WindowsY>mybackground.height-height)
  277.   {
  278.     WindowsY=0;  
  279.   }
  280.    textSize(100);
  281.     // fill((millis()/100)%255, (millis()/100)%255, (millis()/100)%255, (millis()/100)%100+155);
  282.     //   text(GameName, 190, 200);
  283.     image(biaoti, 0, 0, width, height);
  284.     noStroke();
  285.     fill((millis()/100)%255, (millis()/100)%255, (millis()/100)%255, (millis()/100)%100+155);
  286.     textSize(40);
  287.     text("输入A 开始游戏",150, 500);
  288.     text("输入B 查看创作人员",150,600);
  289.     GoldNum=0;
  290.     GameRank=0;
  291. }
  292. void InitGame()//初始化游戏状态
  293. {
  294.     stage=1;
  295.     PlayNowTime=millis()/1000;
  296.     GameLiveTime=60;
  297.    
  298.     //CreateSomeThing();
  299.     PlayGame();
  300.     CreateGold();
  301. }
  302. void PlayGame()//游戏开始
  303. {

  304.   if (IsGameWin())
  305.   {
  306.      GameWin();  
  307.   }
  308.   if (!IsGameOver())
  309.   {
  310.     DrawlandForm();
  311.   }
  312.   else
  313.   {
  314.     GameOver();
  315.   }
  316. }

  317. void EliminateObstacle()
  318. {
  319.   if (wudishijian>0)
  320.   {
  321.     fill(248,252,31, 100);
  322.     // rect(realRobotManX, realRobotManY, 100, 100);
  323.     ellipse(realRobotManX+40, realRobotManY+40, 100, 100);
  324.   }
  325.    
  326. }
  327. void DrawPlayerGoldMan(int x,int y)//绘制主人公
  328. {
  329.   RobotManJump-=((millis()/1000)%2);
  330.   xuanyunshijian-=((millis()/1000)%2);
  331.   // wudishijian-=((millis()/1000)%1.5);
  332.   if (xuanyunshijian>0)
  333.   {
  334.       image(myManDao, x, y, 80, 80);
  335.   }
  336.   else
  337.   {
  338.     if (RobotManJump>0)
  339.     {
  340.        realRobotManX=x;
  341.        realRobotManY=y-100;
  342.        image(myMan, x, y-100, 80, 80);
  343.        EliminateObstacle();
  344.        println("(RobotManJump-(millis()/1000)%2): "+(RobotManJump-(millis()/1000)%2));
  345.     }
  346.     else
  347.     {
  348.       realRobotManX=x;
  349.       realRobotManY=y;
  350.       image(myMan, x, y, 80, 80);
  351.       EliminateObstacle();
  352.       RobotManJump=0;
  353.     }
  354.   }
  355.   
  356. }

  357. void MoveBackImage(int x,int y)//绘制动态的背景图片
  358. {
  359.   if (X<mybackground.width&&y<mybackground.height)
  360.   {
  361.     image(mybackground, -x, -y);
  362.   }
  363.   else
  364.   {
  365.     println("绘制动态的背景图片error!");  
  366.   }
  367. }
  368. void DrawlandForm()//绘制动态雪景地形
  369. {
  370.   // image(mybackground, 0, 0, width, height);
  371.   if(xuanyunshijian>0)
  372.   {
  373.     MoveBackImage(WindowsX,WindowsY);
  374.   }
  375.   else
  376.   {
  377.     WindowsX=WindowsX+1+GameRank;
  378.     WindowsY=WindowsY+1+GameRank;
  379.     MoveBackImage(WindowsX,WindowsY);
  380.   }
  381.   if (WindowsX>mybackground.width-width)
  382.   {
  383.     WindowsX=0;  
  384.   }
  385.   if (WindowsY>mybackground.height-height)
  386.   {
  387.     WindowsY=0;  
  388.   }
  389.   // for (int x = width; x>0; x-=10)
  390.   // {
  391.      // float noiseVal = noise((LanFormMove())*noiseScale,100*noiseScale);
  392.    //      //线级渲染 速度慢,但效果细腻
  393.    //      // stroke(222,248,255);
  394.    //      // line(x,noiseVal*100+400, x, height);
  395.    //      // 方块级渲染 速度快,但效果较粗糙
  396.      // noStroke();
  397.      // fill(222, 248, 255,250);
  398.      // rect(x, noiseVal*100+400, 10,noiseVal*100+400);
  399.      // RobotManY=(int)noiseVal*100+400;
  400.      // DrawPlayerGoldMan(RobotManX,RobotManY-40);
  401.      // rect(x, noiseVal*100+400, 80, 80);

  402.      // if (IsGoldisLive)
  403.      // {
  404.      //   DrawAGold((int)noiseVal*100+400);
  405.      // }
  406.      // if (IsObstacleisLive)
  407.      // {
  408.      //   DrawAObstacle((int)noiseVal*100+400);
  409.      // }
  410.      noStroke();
  411.      calcWave();
  412.      renderWave();
  413.      fill(222, 248, 255,250);
  414.      triangle(0,RobotManY-50,0,height,900,height);
  415.      mylinek=(float)(height-(RobotManY-50))/(float)(900-0);
  416.      //println("mylinek: "+mylinek);
  417.      DrawPlayerGoldMan(RobotManX,RobotManY);
  418.      DrawAGold();

  419.   // }
  420. }
  421. void calcWave() {//计算主人公滑雪动作
  422.   theta += 0.001;
  423.   for (int i = 0; i < yvalues.length; i++) {
  424.     yvalues[i] = 0;
  425.   }

  426.   // Accumulate wave height values
  427.   for (int j = 0; j < maxwaves; j++) {
  428.     float x = theta;
  429.     for (int i = 0; i < yvalues.length; i++) {
  430.       // Every other wave is cosine instead of sine
  431.       if (j % 2 == 0)  yvalues[i] += sin(x)*amplitude[j];
  432.       else yvalues[i] += cos(x)*amplitude[j];
  433.       x+=dx[j];
  434.     }
  435.   }
  436. }

  437. void renderWave() {//输出主人公滑雪动作
  438.   noStroke();
  439.   fill(255,50);
  440.   for (int x = 0; x < yvalues.length; x++)
  441.   {
  442.      RobotManY=(int)yvalues[x]+400-80;
  443.   }
  444. }
  445. void CreateGold()//创建金币
  446. {
  447.   // CreateGoldTime=millis()/1000;
  448.   // GoldX=width;
  449.   // IsGoldisLive=true;
  450.   for (int x = 0; x<10; x++){
  451.     for (int y = 0; y<2; y++){
  452.       ThingMap[x][y]=(int)random(0, 60);
  453.     }
  454.   }
  455.   GoldX=RobotManX+800;
  456.   GoldY=RobotManY+400;
  457. }
  458. void DrawAGold()//绘制金币
  459. {
  460.   if (xuanyunshijian>0)
  461.   {
  462.    
  463.   }
  464.   else
  465.   {
  466.     GoldX-=(2+GameRank*2);
  467.     GoldY-=(1+GameRank);
  468.   }
  469.   if (GoldX+10*80+80<RobotManX)
  470.   {
  471.     CreateGold();  
  472.   }
  473.    for (int x = 0; x<10; x++){
  474.        for (int y = 0; y<2; y++){

  475.          switch (ThingMap[x][y])
  476.          {
  477.            case 1 ://绘制创建小金币
  478.            {
  479.              image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
  480.             AddGoldNum=2;
  481.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  482.             {
  483.               CJBplayer.loop(0);
  484.               GoldNum+=AddGoldNum;
  485.               println("is can ");
  486.               ThingMap[x][y]=0;
  487.             }
  488.             AddGoldNum=0;
  489.              break ;
  490.            }  
  491.            case 2 ://绘制创建中金币
  492.            {
  493.              image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
  494.             AddGoldNum=4;
  495.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  496.             {
  497.               CJBplayer.loop(0);
  498.               GoldNum+=AddGoldNum;
  499.               println("is can ");
  500.               ThingMap[x][y]=0;
  501.             }
  502.             AddGoldNum=0;
  503.              break ;
  504.            }
  505.            case 3 ://绘制创建大金币
  506.            {
  507.              image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45,50, 50);
  508.             AddGoldNum=8;
  509.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  510.             {
  511.               CJBplayer.loop(0);
  512.               GoldNum+=AddGoldNum;
  513.               println("is can ");
  514.               ThingMap[x][y]=0;
  515.             }
  516.             AddGoldNum=0;
  517.              break ;
  518.            }
  519.           case 4 ://绘制创建小金币
  520.           {

  521.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
  522.             AddGoldNum=2;
  523.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  524.             {
  525.               CJBplayer.loop(0);
  526.               GoldNum+=AddGoldNum;
  527.               println("is can ");
  528.               ThingMap[x][y]=0;
  529.             }
  530.             AddGoldNum=0;
  531.             break ;
  532.           }
  533.           case 5 ://磁铁怪
  534.           {
  535.             image(citie, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  536.             if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  537.             {
  538.               AddGoldNum=-5;
  539.               if (wudishijian>0)
  540.               {
  541.                   WDplayer.loop(0);
  542.                   GoldNum+=AddGoldNum;
  543.                   ThingMap[x][y]=0;
  544.               }
  545.               else
  546.               {
  547.                 XYplayer.loop(0);
  548.                 xuanyunshijian=2;
  549.                 GameLiveTime-=1;  
  550.                 ThingMap[x][y]=0;
  551.               }
  552.               
  553.             }
  554.             AddGoldNum=0;
  555.             break;
  556.           }
  557.           case 6 ://绘制创建小金币
  558.           {
  559.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
  560.             AddGoldNum=2;
  561.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  562.             {
  563.               CJBplayer.loop(0);
  564.               GoldNum+=AddGoldNum;
  565.               println("is can ");
  566.               ThingMap[x][y]=0;
  567.             }
  568.             AddGoldNum=0;
  569.             break ;
  570.           }
  571.           case 10 :
  572.           {
  573.             image(danyanguai, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  574.             if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  575.             {
  576.               AddGoldNum=-5;
  577.               if (wudishijian>0)
  578.               {
  579.                   WDplayer.loop(0);
  580.                   GoldNum+=AddGoldNum;
  581.                   ThingMap[x][y]=0;
  582.               }
  583.               else
  584.               {
  585.                 XYplayer.loop(0);
  586.                 xuanyunshijian=3;
  587.                 GameLiveTime-=1;  
  588.                 ThingMap[x][y]=0;
  589.               }
  590.             }
  591.             AddGoldNum=0;
  592.             break;
  593.           }
  594.           case 11://绘制创建小金币
  595.           {
  596.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
  597.             AddGoldNum=2;
  598.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  599.             {
  600.               CJBplayer.loop(0);
  601.               GoldNum+=AddGoldNum;
  602.               println("is can ");
  603.               ThingMap[x][y]=0;
  604.             }
  605.             AddGoldNum=0;
  606.             break ;
  607.           }
  608.           case 12 ://绘制创建小金币
  609.           {
  610.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
  611.             AddGoldNum=2;
  612.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  613.             {
  614.               CJBplayer.loop(0);
  615.               GoldNum+=AddGoldNum;
  616.               println("is can ");
  617.               ThingMap[x][y]=0;
  618.             }
  619.             AddGoldNum=0;
  620.             break ;
  621.           }
  622.           case 13 ://绘制创建小金币
  623.           {
  624.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
  625.             AddGoldNum=2;
  626.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  627.             {
  628.               CJBplayer.loop(0);
  629.               GoldNum+=AddGoldNum;
  630.               println("is can ");
  631.               ThingMap[x][y]=0;
  632.             }
  633.             AddGoldNum=0;
  634.             break ;
  635.           }
  636.           case 14 ://绘制创建小金币
  637.           {
  638.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
  639.             AddGoldNum=2;
  640.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  641.             {
  642.               CJBplayer.loop(0);
  643.               GoldNum+=AddGoldNum;
  644.               println("is can ");
  645.               ThingMap[x][y]=0;
  646.             }
  647.             AddGoldNum=0;
  648.             break ;
  649.           }  
  650.           case 15 :
  651.           {
  652.             image(dashouguai, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  653.             if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  654.             {
  655.               AddGoldNum=-7;
  656.               if (wudishijian>0)
  657.               {
  658.                   WDplayer.loop(0);
  659.                   GoldNum+=AddGoldNum;
  660.                   ThingMap[x][y]=0;
  661.               }
  662.               else
  663.               {
  664.                 XYplayer.loop(0);
  665.                 xuanyunshijian=3;  
  666.                 GameLiveTime-=1;
  667.                 ThingMap[x][y]=0;
  668.               }
  669.             }
  670.             AddGoldNum=0;
  671.             break;
  672.           }
  673.           case 20 :
  674.           {
  675.             image(kuloutou, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  676.             if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  677.             {
  678.               AddGoldNum=-8;
  679.               if (wudishijian>0)
  680.               {
  681.                   WDplayer.loop(0);
  682.                   GoldNum+=AddGoldNum;
  683.                   ThingMap[x][y]=0;
  684.               }
  685.               else
  686.               {
  687.                 XYplayer.loop(0);
  688.                 xuanyunshijian=2;
  689.                 GameLiveTime-=5;  
  690.                 ThingMap[x][y]=0;
  691.               }
  692.             }
  693.             AddGoldNum=0;
  694.             break;
  695.           }
  696.           case 21 ://绘制创建中金币
  697.           {
  698.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
  699.             AddGoldNum=4;
  700.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  701.             {
  702.               CJBplayer.loop(0);
  703.               GoldNum+=AddGoldNum;
  704.               println("is can ");
  705.               ThingMap[x][y]=0;
  706.             }
  707.             AddGoldNum=0;
  708.             break ;
  709.           }
  710.           case 25 :
  711.           {
  712.             image(lansexiaoguai, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  713.             if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  714.             {
  715.               AddGoldNum=-9;
  716.               if (wudishijian>0)
  717.               {
  718.                   WDplayer.loop(0);
  719.                   GoldNum+=AddGoldNum;
  720.                   ThingMap[x][y]=0;
  721.               }
  722.               else
  723.               {
  724.                 XYplayer.loop(0);
  725.                 xuanyunshijian=2;
  726.                 GameLiveTime-=2;
  727.                 ThingMap[x][y]=0;
  728.               }
  729.             }
  730.             AddGoldNum=0;
  731.             break;
  732.           }
  733.           case 26 ://绘制创建中金币
  734.           {
  735.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
  736.             AddGoldNum=4;
  737.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  738.             {
  739.               CJBplayer.loop(0);
  740.               GoldNum+=AddGoldNum;
  741.               println("is can ");
  742.               ThingMap[x][y]=0;
  743.             }
  744.             AddGoldNum=0;
  745.             break ;
  746.           }
  747.           case 30 :
  748.           {
  749.             image(xiaoji, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  750.             if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  751.             {
  752.               CJBplayer.loop(0);
  753.               GameLiveTime+=5;  
  754.               ThingMap[x][y]=0;
  755.             }
  756.             break;
  757.           }
  758.           case 31 ://绘制创建中金币
  759.           {
  760.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
  761.             AddGoldNum=4;
  762.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  763.             {
  764.               CJBplayer.loop(0);
  765.               GoldNum+=AddGoldNum;
  766.               println("is can ");
  767.               ThingMap[x][y]=0;
  768.             }
  769.             AddGoldNum=0;
  770.             break ;
  771.           }
  772.           case 35 :
  773.           {
  774.             image(xiaoxiaoji, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  775.             if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  776.             {
  777.               CJBplayer.loop(0);
  778.               GameLiveTime+=3;   
  779.               ThingMap[x][y]=0;
  780.             }
  781.             break;
  782.           }
  783.           case 36://绘制创建中金币
  784.           {
  785.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
  786.             AddGoldNum=4;
  787.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  788.             {
  789.               CJBplayer.loop(0);
  790.               GoldNum+=AddGoldNum;
  791.               println("is can ");
  792.               ThingMap[x][y]=0;
  793.             }
  794.             AddGoldNum=0;
  795.             break ;
  796.           }
  797.           case 40 :
  798.           {
  799.             image(xueren, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  800.            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  801.             {
  802.               AddGoldNum=-10;
  803.               if (wudishijian>0)
  804.               {
  805.                   WDplayer.loop(0);
  806.                   GoldNum+=AddGoldNum;
  807.                   ThingMap[x][y]=0;
  808.               }
  809.               else
  810.               {
  811.                 XYplayer.loop(0);
  812.                 xuanyunshijian=4;
  813.                 GameLiveTime-=2;  
  814.                 ThingMap[x][y]=0;
  815.               }
  816.             }
  817.             AddGoldNum=0;
  818.             break;
  819.           }
  820.           case 41 ://绘制创建大金币
  821.           {
  822.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45,50, 50);
  823.             AddGoldNum=8;
  824.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  825.             {
  826.               CJBplayer.loop(0);
  827.               GoldNum+=AddGoldNum;
  828.               println("is can ");
  829.               ThingMap[x][y]=0;
  830.             }
  831.             AddGoldNum=0;
  832.             break ;
  833.           }
  834.           case 45 :
  835.           {
  836.             image(youling, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  837.             if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  838.             {
  839.               AddGoldNum=-15;
  840.               if (wudishijian>0)
  841.               {
  842.                   WDplayer.loop(0);
  843.                   GoldNum+=AddGoldNum;
  844.                   ThingMap[x][y]=0;
  845.               }
  846.               else
  847.               {
  848.                 XYplayer.loop(0);
  849.                 xuanyunshijian=4;
  850.                 GameLiveTime-=5;
  851.                 ThingMap[x][y]=0;
  852.               }
  853.             }
  854.             AddGoldNum=0;
  855.             break;
  856.           }
  857.           case 46 ://绘制创建大金币
  858.           {
  859.             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45,50, 50);
  860.             AddGoldNum=8;
  861.             if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
  862.             {
  863.               CJBplayer.loop(0);
  864.               GoldNum+=AddGoldNum;
  865.               println("is can ");
  866.               ThingMap[x][y]=0;
  867.             }
  868.             AddGoldNum=0;
  869.             break ;
  870.           }
  871.           case 50 :
  872.           {
  873.             image(zhadan, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
  874.             if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
  875.             {
  876.               AddGoldNum=-30;
  877.               if (wudishijian>0)
  878.               {
  879.                   WDplayer.loop(0);
  880.                   GoldNum+=AddGoldNum;
  881.                   ThingMap[x][y]=0;
  882.               }
  883.               else
  884.               {
  885.                 XYplayer.loop(0);
  886.                 xuanyunshijian=3;
  887.                 GameLiveTime-=10;
  888.                 ThingMap[x][y]=0;
  889.                 CreateGold();
  890.               }
  891.             }
  892.             AddGoldNum=0;
  893.             break;
  894.           }
  895.            default :
  896.             
  897.            break;  
  898.          }
  899.          
  900.        }
  901.    }
  902.    if (GoldX<RobotManX-(20*80+80))
  903.    {
  904.      IsGoldisLive=false;
  905.    }
  906.   // if (IsGetGold())
  907.   // {
  908.   //   GoldNum+=AddGoldNum;
  909.   //   //IsGoldisLive=false;
  910.   //   println("is can ");
  911.   // }
  912.    
  913. }
  914. boolean IsGetGold(int x,int y)//主人公是否得到了金币
  915. {

  916.   if(x<realRobotManX+80&&(y>realRobotManY-10&&y<realRobotManY+80))
  917.   {
  918.     return true;
  919.   }
  920.   else
  921.   {
  922.     return false;  
  923.   }
  924.    
  925. }
  926. // int LanFormMove()//雪景地形的移动参数计算函数
  927. // {
  928. //   if (m_LanFormMove<width)
  929. //   {
  930. //     m_LanFormMove++;
  931. //     return m_LanFormMove;  
  932. //   }
  933. //   else
  934. //   {
  935. //     m_LanFormMove=0;  
  936. //     return m_LanFormMove;
  937. //   }
  938. // }
  939. boolean IsGameWin()
  940. {
  941.   if(!IsGameOver())
  942.   {
  943.     if (GoldNum>30+GameRank*70)
  944.     {
  945.       return true;  
  946.     }
  947.     else
  948.     {
  949.       return false;  
  950.     }
  951.   }
  952.   else
  953.   {
  954.     return false;   
  955.   }
  956. }
  957. boolean IsGameOver()//是否游戏结束 主人公生存时间为零,即游戏结束
  958. {
  959.   if ((millis()/1000-PlayNowTime)>GameLiveTime)
  960.   {
  961.     return true;
  962.   }
  963.   else
  964.   {
  965.     return false;   
  966.   }
  967. }
  968. void CreditsPage()//显示制作人员的窗口
  969. {
  970.         stage=2;
  971.         image(mybackground, 0, 0, width, height);
  972.         textSize(100);
  973.         fill(236, 245, 17);
  974.         text("作者:杨帆",300,120);
  975.         GetGameResult();
  976.         textSize(60);
  977.         text("至今挑战到了:"+BestGameRank+"游戏难度!", 0, 400);
  978.         text("至今获得最大金币数量为:"+BestGameGoldNum, 0, 500);
  979. }
  980. void PrintText()//显示游戏数据,如主人公生存时间等
  981. {
  982.       fill(0,0,0,0);
  983.       rect(450, 0, width, 30);
  984.       textSize(30);
  985.       fill(34,62,196,200);
  986.       String m_time="";
  987.       //m_time+="主人公生存时间:"+(GameLiveTime-(millis()/1000-PlayNowTime))+"S";
  988.       m_time+="  "+"游戏难度:"+GameRank;
  989.       m_time+="  "+"金币数:"+GoldNum;
  990.       m_time+="  "+"过关金币数:"+(30+GameRank*70);
  991.       text(m_time,450,0,width,100);
  992.       text("时间:", 0, 0,100, 100);
  993.       DrawPowerRect();

  994. }
  995. void DrawPowerRect()//画时间条
  996. {
  997.   int power;
  998.   power=(GameLiveTime-(millis()/1000-PlayNowTime))*5;
  999.   noFill();
  1000.   rect(100, 0, 450, 30);
  1001.   fill(30, 72, 232,200);
  1002.   rect(100, 0, power, 30);
  1003. }
  1004. void GameOver()//游戏结束
  1005. {
  1006.   stage=3;
  1007.   // background(0);
  1008.   MoveBackImage(WindowsX++,WindowsY++);
  1009.   if (WindowsX>mybackground.width-width)
  1010.   {
  1011.     WindowsX=0;  
  1012.   }
  1013.   if (WindowsY>mybackground.height-height)
  1014.   {
  1015.     WindowsY=0;  
  1016.   }
  1017.   textSize(30);
  1018.   text("按 A 键 ", 0, 80);
  1019.   textSize(100);
  1020.   text("再来一次!",220, 200);
  1021.   textSize(30);
  1022.   text("按 B 键 ", 0, 240);
  1023.   textSize(100);
  1024.   text("游戏结束!", 220, 350);
  1025.   String m_time="金币数:"+GoldNum+"个";
  1026.   text(m_time,220,500);
  1027.   SaveGameResult();

  1028. }
  1029. void GameWin()
  1030. {
  1031.   stage=4;
  1032.   WindowsX=0;
  1033.   WindowsY=0;
  1034.   MoveBackImage(WindowsX++,WindowsY++);
  1035.   if (WindowsX>mybackground.width-width)
  1036.   {
  1037.     WindowsX=0;  
  1038.   }
  1039.   if (WindowsY>mybackground.height-height)
  1040.   {
  1041.     WindowsY=0;  
  1042.   }
  1043.   textSize(30);
  1044.   text("按 A 键 ", 0, 80);
  1045.   textSize(100);
  1046.   text("进行下一关!",220, 200);
  1047.   textSize(30);
  1048.   text("按 B 键 ", 0, 240);
  1049.   textSize(100);
  1050.   text("游戏结束!", 220, 350);
  1051.   String m_time="金币数:"+GoldNum+"个";
  1052.   text(m_time,220,500);
  1053.   SaveGameResult();

  1054. }
  1055. void xiaxue()
  1056. {

  1057.   for (int i = 0; i<10; i++){
  1058.     m_xiaxuehuazu[i].displayxiaxue();
  1059.   }

  1060. }
  1061. void SaveGameResult()
  1062. {
  1063.   GetGameResult();
  1064.   if (GameRank>BestGameRank)
  1065.   {
  1066.     myGameSave.setInt(0,0,GameRank);
  1067.     println("1BestGameRank: "+GameRank);
  1068.   }
  1069.   if (GoldNum>BestGameGoldNum)
  1070.   {
  1071.     myGameSave.setInt(1,0,GoldNum);
  1072.     println("2BestGameGoldNum: "+GoldNum);
  1073.   }
  1074.   saveTable(myGameSave,"data/Save.csv");
  1075. }
  1076. void GetGameResult()
  1077. {
  1078.    BestGameRank=myGameSave.getInt(0,0);
  1079.    BestGameGoldNum=myGameSave.getInt(1,0);
  1080.    println("BestGameRank: "+BestGameRank);
  1081.    println("BestGameGoldNum: "+BestGameGoldNum);
  1082. }
  1083. void GetArduinoKey()
  1084. {
  1085.   int temp;
  1086.   while (myPort.available()>0)
  1087.   {
  1088.       temp=myPort.read();
  1089.       Arduinokey=temp;
  1090.       println("temp: "+temp);
  1091.   }
  1092.   switch (stage)
  1093.   {
  1094.     case 0 ://游戏开始选择画面
  1095.     {
  1096.       if (Arduinokey=='1')
  1097.       {
  1098.         stage=1;
  1099.         Arduinokey=0;
  1100.         InitGame();
  1101.       }
  1102.       if (Arduinokey=='2')
  1103.       {
  1104.         Arduinokey=0;
  1105.         CreditsPage();
  1106.       }
  1107.       break;
  1108.     }
  1109.     case 1 ://游戏开始
  1110.     {
  1111.       if (Arduinokey=='1')
  1112.       {
  1113.         Arduinokey=0;
  1114.         if (wudishijian>0)
  1115.         {
  1116.             wudishijian=0;
  1117.         }
  1118.         else
  1119.         {
  1120.           wudishijian=2;
  1121.         }
  1122.         
  1123.         EliminateObstacle();
  1124.         println("ok");  
  1125.       }
  1126.       if (Arduinokey=='2')
  1127.       {
  1128.         Arduinokey=0;
  1129.         RobotManJump=1.5;
  1130.       }
  1131.       break;
  1132.     }
  1133.     case 2://制作人员
  1134.     {
  1135.       if(Arduinokey=='1'||Arduinokey=='2')
  1136.       {
  1137.         Arduinokey=0;
  1138.         BeginGame();
  1139.       }
  1140.       break;
  1141.     }
  1142.     case 3://游戏结束
  1143.     {
  1144.       if (Arduinokey=='1')
  1145.       {
  1146.         Arduinokey=0;
  1147.         stage=0;
  1148.         BeginGame();
  1149.       }
  1150.       if (Arduinokey=='2')
  1151.       {
  1152.         Arduinokey=0;
  1153.         exit();
  1154.       }
  1155.       break;
  1156.     }
  1157.     case 4://游戏胜利
  1158.     {
  1159.       if (Arduinokey=='1')
  1160.       {
  1161.         Arduinokey=0;
  1162.         WindowsX=0;
  1163.         WindowsY=0;
  1164.         GameRank++;

  1165.         InitGame();
  1166.       }
  1167.       if (Arduinokey=='2')
  1168.       {
  1169.         Arduinokey=0;
  1170.         exit();
  1171.       }
  1172.       break ;
  1173.     }   
  1174.          
  1175.   }  
  1176. }
  1177. public class xuehua
  1178. {

  1179.   
  1180.   weizhi []xuehuazu=new weizhi[5];
  1181.   public xuehua()
  1182.   {
  1183.      for (int i = 0; i<5; i++)
  1184.      {
  1185.          xuehuazu[i]=new weizhi();
  1186.      }
  1187.      for (int i = 0; i<5; i++)
  1188.      {
  1189.         xuehuazu[i].m_x=(int)random(0, width);
  1190.         xuehuazu[i].m_y=0;
  1191.         xuehuazu[i].m_h=20;
  1192.         xuehuazu[i].m_w=20;  
  1193.      }

  1194.   }
  1195.   void Createxuehua()
  1196.   {
  1197.     for (int i = 0; i<5; i++)
  1198.      {
  1199.         xuehuazu[i].m_x=(int)random(0, width);
  1200.         xuehuazu[i].m_y=0;
  1201.         xuehuazu[i].m_h=30;
  1202.         xuehuazu[i].m_w=30;  
  1203.      }
  1204.   }
  1205.   void displayxiaxue()
  1206.   {
  1207.     for (int i = 0; i<5; i++){
  1208.         image(xuehua3, xuehuazu[i].m_x, xuehuazu[i].m_y, xuehuazu[i].m_w, xuehuazu[i].m_h);
  1209.         xuehuazu[i].m_y+=(int)random(0, 20);
  1210.         if ((int)random(0, 2)>0)
  1211.         {
  1212.            xuehuazu[i].m_x+=(int)random(0, 5);
  1213.         }
  1214.         else
  1215.         {
  1216.           xuehuazu[i].m_x-=(int)random(0, 5);
  1217.         }
  1218.         if (xuehuazu[i].m_y>height+10)
  1219.         {
  1220.           Createxuehua();
  1221.         }
  1222.     }

  1223.   }
  1224. }
  1225. public class weizhi
  1226. {
  1227.   
  1228.   int m_x,m_y;
  1229.   int m_h,m_w;
  1230. }









复制代码

游戏开始界面,本例程实际上是一个用Arduino做控制器的PC小游戏。
下面就是游戏开始界面的截图。

游戏中的截图。

在Arduino 端实际上就是通过发送0,1来代表A,B键,Arduino具体用什么方式是不限制的,之前做的是用红外遥控器。
不过貌似这种做法被江南大学教授评为工作量较低,哈哈,不要在意这些细节。
有通信协议,用什么不都能控制么。
那么这节课就先下课吧。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2013-7-19 16:07:56 | 显示全部楼层
好长。。。。。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-19 14:50 , Processed in 0.052701 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表