yangfanconan 发表于 2013-7-19 11:04:00

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

今天我们继续学习Processing的自带库--Serial库(串口通信库)。
串口通信相对来说一个比较重要的库,因为应用比较多。毕竟Processing+Arduino的应用组合是当前十分受欢迎的一个模式。
下面我就从一个例子中感受一下Serial库带来的震撼效果。
本例中用到了碰撞检测(边界检测),飘雪系统,音效,弧线轨迹等。
实际上用底层手段,粗糙的模拟了一套2d游戏框架(有点吹大了)。不过确实是这样的。
总体来说,技术含量居中,属于介于学年设计,毕业设计的水平。
希望大家有所参考吧。
import ddf.minim.*;
import processing.serial.*;
Table myGameSave;
Serial myPort = new Serial(this,"COM20", 9600);
Minim minim= new Minim(this);
AudioPlayer BKMplayer;
AudioPlayer BZSplayer;
AudioPlayer CJBplayer;
AudioPlayer TTplayer;
AudioPlayer WDplayer;
AudioPlayer XYplayer;

PImage mybackground;
PImage biaoti;
PImage xuehua1;
PImage xuehua2;
PImage xuehua3;
PImage myMan;
PImage myManDao;
PImage myGoldBi;
PImage citie;
PImage danyanguai;
PImage dashouguai;
PImage kuloutou;
PImage lansexiaoguai;
PImage xiaoji;
PImage xiaoxiaoji;
PImage xueren;
PImage youling;
PImage zhadan;
PFont myFont;
int stage=0;
String GameName="饥饿的机器人";
int PlayNowTime;
int GameLiveTime;
int GameRank;
int GoldNum;
int AddGoldNum;
int m_LanFormMove;
int BestGameRank;
int BestGameGoldNum;
int CreateGoldTime;
int CreateObstacleTime;

// float noiseScale=0.005;//地形变化指数
int xspacing = 8;   // How far apart should each horizontal location be spaced
int w;            // Width of entire wave
int maxwaves = 1;   // total # of waves to add together
float theta = 0.0;
float[] amplitude = new float;   // Height of wave
float[] dx = new float;          // Value for incrementing X, to be calculated as a function of period and xspacing
float[] yvalues;                           // Using an array to store height values for the wave (not entirely necessary)
int WindowsX;
int WindowsY;
int RobotManX;
int RobotManY;
int realRobotManX;
int realRobotManY;
int GoldX;
int GoldY;
int ObstacleX;
int ObstacleY;
int xuanyunshijian;
int wudishijian;
intArduinokey;
boolean ArduinoKeyA;
boolean ArduinoKeyB;
float mylinek = 0.0;
booleanIsObstacleisLive;
boolean IsGoldisLive;
floatRobotManJump;
int [][]ThingMap=new int;
xuehua []m_xiaxuehuazu=new xuehua;
void setup()
{
myGameSave=loadTable("Save.csv","header");
println("myGameSave.getRowCount(): "+myGameSave.getRowCount());
if (myGameSave.getRowCount()==1)
{
       TableRow newRow=myGameSave.addRow();
       newRow.setInt("GameRankSave",0);
       newRow.setInt("GameGoldNumSave",0);
       saveTable(myGameSave,"data/Save.csv");
}

size(1024, 768,P3D);
//
BKMplayer=minim.loadFile("bkmusic.mp3");
BZSplayer=minim.loadFile("baozhasheng.wav");
CJBplayer=minim.loadFile("chijinbi.wav");
TTplayer=minim.loadFile("tantiao.wav");
WDplayer=minim.loadFile("wudiyinxiao.wav");
XYplayer=minim.loadFile("xuanyun.wav");
//Ani.init(this);//初始化ani库
w = width +160;
for (int i = 0; i < maxwaves; i++)
    {
    amplitude = random(0,10);
    float period = random(800,1500); // How many pixels before the wave repeats
    dx = (TWO_PI / period) * xspacing;
}
yvalues = new float;
frameRate(60);
WindowsX=0;
WindowsY=0;
myFont=createFont("微软雅黑",32);
mybackground=loadImage("background.jpg");
biaoti=loadImage("biaoti.png");
myMan=loadImage("man.png");
myManDao=loadImage("mandao.png");
myGoldBi=loadImage("goldbi.png");//1,2,3
xuehua1=loadImage("xuehua1.png");
xuehua2=loadImage("xuehua2.png");
xuehua3=loadImage("xuehua3.png");
citie=loadImage("citie.png");//5
danyanguai=loadImage("danyanguai.png");//10
dashouguai=loadImage("dashouguai.png");//15
lansexiaoguai=loadImage("lansexiaoguai.png");//20
xiaoji=loadImage("xiaoji.png");//25
xiaoxiaoji=loadImage("xiaoxiaoji.png");//30
xueren=loadImage("xueren.png");//35
youling=loadImage("youling.png");//40
zhadan=loadImage("zhadan.png");//45
kuloutou=loadImage("kuloutou.png");//50
textFont(myFont);
GameRank=0;
GameLiveTime=12;
GoldNum=0;
m_LanFormMove=0;
RobotManX=150;
for (int i = 0; i<10; i++){
      m_xiaxuehuazu=new xuehua();
}
BKMplayer.loop();
BeginGame();

}

void draw()
{
// image(mybackground, 0, 0, width, height);
// image(myMan, 0, 0, 100, 100);
GetArduinoKey();
switch (stage)
{
    case 0 ://游戏开始选择画面
    {
   
      BeginGame();
      xiaxue();
      break;
    }
    case 1 ://游戏开始
    {
      PlayGame();
      PrintText();
      xiaxue();
      break;
    }
    case 2://制作人员
    {
      CreditsPage();
      xiaxue();
      break;
    }
    case 3://游戏结束
    {
      GameOver();
      xiaxue();
      break;
    }
    case 4 ://游戏胜利
    {
      GameWin();
      xiaxue();
      break;
    }
    default :
    {
      break;
    }

         
}

   
}

void keyPressed()
{
switch (stage)
{
    case 0 ://游戏开始选择画面
    {
      if (key=='a'||key=='A')
      {
      stage=1;
      InitGame();
      }
      if (key=='b'||key=='B')
      {
      CreditsPage();
      }
      break;
    }
    case 1 ://游戏开始
    {
      if (key=='b'||key=='B')
      {
      
      if (wudishijian>0)
      {
            WDplayer.loop(0);
            wudishijian=0;
      }
      else
      {
          WDplayer.loop(0);
          wudishijian=2;
      }
      
      EliminateObstacle();
      println("ok");
      }
      if (key=='a'||key=='A')
      {
      TTplayer.loop(0);
      RobotManJump=5;
      TTplayer.play();
      }
      break;
    }
    case 2://制作人员
    {
      if(keyCode>0)
      {
      BeginGame();
      }
      break;
    }
    case 3://游戏结束
    {
      if (key=='a'||key=='A')
      {
      stage=0;
      BeginGame();
      }
      if (key=='b'||key=='B')
      {
      exit();
      }
      break;
    }
    case 4://游戏胜利
    {
      if (key=='a'||key=='A')
      {
      WindowsX=0;
      WindowsY=0;
      GameRank++;

      InitGame();
      }
      if (key=='b'||key=='B')
      {
      exit();
      }
      break ;
    }   
         
}
}
void BeginGame()//开始游戏界面
{
stage=0;
// image(mybackground, 0,0, width, height);
MoveBackImage(WindowsX++,WindowsY++);
if (WindowsX>mybackground.width-width)
{
    WindowsX=0;
}
if (WindowsY>mybackground.height-height)
{
    WindowsY=0;
}
   textSize(100);
    // fill((millis()/100)%255, (millis()/100)%255, (millis()/100)%255, (millis()/100)%100+155);
    //   text(GameName, 190, 200);
    image(biaoti, 0, 0, width, height);
    noStroke();
    fill((millis()/100)%255, (millis()/100)%255, (millis()/100)%255, (millis()/100)%100+155);
    textSize(40);
    text("输入A 开始游戏",150, 500);
    text("输入B 查看创作人员",150,600);
    GoldNum=0;
    GameRank=0;
}
void InitGame()//初始化游戏状态
{
    stage=1;
    PlayNowTime=millis()/1000;
    GameLiveTime=60;
   
    //CreateSomeThing();
    PlayGame();
    CreateGold();
}
void PlayGame()//游戏开始
{

if (IsGameWin())
{
   GameWin();
}
if (!IsGameOver())
{
    DrawlandForm();
}
else
{
    GameOver();
}
}

void EliminateObstacle()
{
if (wudishijian>0)
{
    fill(248,252,31, 100);
    // rect(realRobotManX, realRobotManY, 100, 100);
    ellipse(realRobotManX+40, realRobotManY+40, 100, 100);
}
   
}
void DrawPlayerGoldMan(int x,int y)//绘制主人公
{
RobotManJump-=((millis()/1000)%2);
xuanyunshijian-=((millis()/1000)%2);
// wudishijian-=((millis()/1000)%1.5);
if (xuanyunshijian>0)
{
      image(myManDao, x, y, 80, 80);
}
else
{
    if (RobotManJump>0)
    {
       realRobotManX=x;
       realRobotManY=y-100;
       image(myMan, x, y-100, 80, 80);
       EliminateObstacle();
       println("(RobotManJump-(millis()/1000)%2): "+(RobotManJump-(millis()/1000)%2));
    }
    else
    {
      realRobotManX=x;
      realRobotManY=y;
      image(myMan, x, y, 80, 80);
      EliminateObstacle();
      RobotManJump=0;
    }
}

}

void MoveBackImage(int x,int y)//绘制动态的背景图片
{
if (X<mybackground.width&&y<mybackground.height)
{
    image(mybackground, -x, -y);
}
else
{
    println("绘制动态的背景图片error!");
}
}
void DrawlandForm()//绘制动态雪景地形
{
// image(mybackground, 0, 0, width, height);
if(xuanyunshijian>0)
{
    MoveBackImage(WindowsX,WindowsY);
}
else
{
    WindowsX=WindowsX+1+GameRank;
    WindowsY=WindowsY+1+GameRank;
    MoveBackImage(WindowsX,WindowsY);
}
if (WindowsX>mybackground.width-width)
{
    WindowsX=0;
}
if (WindowsY>mybackground.height-height)
{
    WindowsY=0;
}
// for (int x = width; x>0; x-=10)
// {
   // float noiseVal = noise((LanFormMove())*noiseScale,100*noiseScale);
   //      //线级渲染 速度慢,但效果细腻
   //      // stroke(222,248,255);
   //      // line(x,noiseVal*100+400, x, height);
   //      // 方块级渲染 速度快,但效果较粗糙
   // noStroke();
   // fill(222, 248, 255,250);
   // rect(x, noiseVal*100+400, 10,noiseVal*100+400);
   // RobotManY=(int)noiseVal*100+400;
   // DrawPlayerGoldMan(RobotManX,RobotManY-40);
   // rect(x, noiseVal*100+400, 80, 80);

   // if (IsGoldisLive)
   // {
   //   DrawAGold((int)noiseVal*100+400);
   // }
   // if (IsObstacleisLive)
   // {
   //   DrawAObstacle((int)noiseVal*100+400);
   // }
   noStroke();
   calcWave();
   renderWave();
   fill(222, 248, 255,250);
   triangle(0,RobotManY-50,0,height,900,height);
   mylinek=(float)(height-(RobotManY-50))/(float)(900-0);
   //println("mylinek: "+mylinek);
   DrawPlayerGoldMan(RobotManX,RobotManY);
   DrawAGold();

// }
}
void calcWave() {//计算主人公滑雪动作
theta += 0.001;
for (int i = 0; i < yvalues.length; i++) {
    yvalues = 0;
}

// Accumulate wave height values
for (int j = 0; j < maxwaves; j++) {
    float x = theta;
    for (int i = 0; i < yvalues.length; i++) {
      // Every other wave is cosine instead of sine
      if (j % 2 == 0)yvalues += sin(x)*amplitude;
      else yvalues += cos(x)*amplitude;
      x+=dx;
    }
}
}

void renderWave() {//输出主人公滑雪动作
noStroke();
fill(255,50);
for (int x = 0; x < yvalues.length; x++)
{
   RobotManY=(int)yvalues+400-80;
}
}
void CreateGold()//创建金币
{
// CreateGoldTime=millis()/1000;
// GoldX=width;
// IsGoldisLive=true;
for (int x = 0; x<10; x++){
    for (int y = 0; y<2; y++){
      ThingMap=(int)random(0, 60);
    }
}
GoldX=RobotManX+800;
GoldY=RobotManY+400;
}
void DrawAGold()//绘制金币
{
if (xuanyunshijian>0)
{
   
}
else
{
    GoldX-=(2+GameRank*2);
    GoldY-=(1+GameRank);
}
if (GoldX+10*80+80<RobotManX)
{
    CreateGold();
}
   for (int x = 0; x<10; x++){
       for (int y = 0; y<2; y++){

         switch (ThingMap)
         {
         case 1 ://绘制创建小金币
         {
             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
            AddGoldNum=2;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
             break ;
         }
         case 2 ://绘制创建中金币
         {
             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
            AddGoldNum=4;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
             break ;
         }
         case 3 ://绘制创建大金币
         {
             image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45,50, 50);
            AddGoldNum=8;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
             break ;
         }
          case 4 ://绘制创建小金币
          {

            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
            AddGoldNum=2;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 5 ://磁铁怪
          {
            image(citie, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            AddGoldNum=-5;
            if (wudishijian>0)
            {
                  WDplayer.loop(0);
                  GoldNum+=AddGoldNum;
                  ThingMap=0;
            }
            else
            {
                XYplayer.loop(0);
                xuanyunshijian=2;
                GameLiveTime-=1;
                ThingMap=0;
            }
            
            }
            AddGoldNum=0;
            break;
          }
          case 6 ://绘制创建小金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
            AddGoldNum=2;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 10 :
          {
            image(danyanguai, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            AddGoldNum=-5;
            if (wudishijian>0)
            {
                  WDplayer.loop(0);
                  GoldNum+=AddGoldNum;
                  ThingMap=0;
            }
            else
            {
                XYplayer.loop(0);
                xuanyunshijian=3;
                GameLiveTime-=1;
                ThingMap=0;
            }
            }
            AddGoldNum=0;
            break;
          }
          case 11://绘制创建小金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
            AddGoldNum=2;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 12 ://绘制创建小金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
            AddGoldNum=2;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 13 ://绘制创建小金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
            AddGoldNum=2;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 14 ://绘制创建小金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 30, 30);
            AddGoldNum=2;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 15 :
          {
            image(dashouguai, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            AddGoldNum=-7;
            if (wudishijian>0)
            {
                  WDplayer.loop(0);
                  GoldNum+=AddGoldNum;
                  ThingMap=0;
            }
            else
            {
                XYplayer.loop(0);
                xuanyunshijian=3;
                GameLiveTime-=1;
                ThingMap=0;
            }
            }
            AddGoldNum=0;
            break;
          }
          case 20 :
          {
            image(kuloutou, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            AddGoldNum=-8;
            if (wudishijian>0)
            {
                  WDplayer.loop(0);
                  GoldNum+=AddGoldNum;
                  ThingMap=0;
            }
            else
            {
                XYplayer.loop(0);
                xuanyunshijian=2;
                GameLiveTime-=5;
                ThingMap=0;
            }
            }
            AddGoldNum=0;
            break;
          }
          case 21 ://绘制创建中金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
            AddGoldNum=4;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 25 :
          {
            image(lansexiaoguai, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            AddGoldNum=-9;
            if (wudishijian>0)
            {
                  WDplayer.loop(0);
                  GoldNum+=AddGoldNum;
                  ThingMap=0;
            }
            else
            {
                XYplayer.loop(0);
                xuanyunshijian=2;
                GameLiveTime-=2;
                ThingMap=0;
            }
            }
            AddGoldNum=0;
            break;
          }
          case 26 ://绘制创建中金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
            AddGoldNum=4;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 30 :
          {
            image(xiaoji, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GameLiveTime+=5;
            ThingMap=0;
            }
            break;
          }
          case 31 ://绘制创建中金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
            AddGoldNum=4;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 35 :
          {
            image(xiaoxiaoji, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GameLiveTime+=3;   
            ThingMap=0;
            }
            break;
          }
          case 36://绘制创建中金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45, 40, 40);
            AddGoldNum=4;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 40 :
          {
            image(xueren, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
         if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            AddGoldNum=-10;
            if (wudishijian>0)
            {
                  WDplayer.loop(0);
                  GoldNum+=AddGoldNum;
                  ThingMap=0;
            }
            else
            {
                XYplayer.loop(0);
                xuanyunshijian=4;
                GameLiveTime-=2;
                ThingMap=0;
            }
            }
            AddGoldNum=0;
            break;
          }
          case 41 ://绘制创建大金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45,50, 50);
            AddGoldNum=8;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 45 :
          {
            image(youling, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            AddGoldNum=-15;
            if (wudishijian>0)
            {
                  WDplayer.loop(0);
                  GoldNum+=AddGoldNum;
                  ThingMap=0;
            }
            else
            {
                XYplayer.loop(0);
                xuanyunshijian=4;
                GameLiveTime-=5;
                ThingMap=0;
            }
            }
            AddGoldNum=0;
            break;
          }
          case 46 ://绘制创建大金币
          {
            image(myGoldBi, GoldX+x*80+80, GoldY-y*80+x*45,50, 50);
            AddGoldNum=8;
            if (IsGetGold(GoldX+x*80+80, GoldY-y*80+x*45))
            {
            CJBplayer.loop(0);
            GoldNum+=AddGoldNum;
            println("is can ");
            ThingMap=0;
            }
            AddGoldNum=0;
            break ;
          }
          case 50 :
          {
            image(zhadan, GoldX+x*80+80, GoldY-y*80+x*45, 50, 50);
            if (IsGetGold(GoldX+x*80+80,GoldY-y*80+x*45))
            {
            AddGoldNum=-30;
            if (wudishijian>0)
            {
                  WDplayer.loop(0);
                  GoldNum+=AddGoldNum;
                  ThingMap=0;
            }
            else
            {
                XYplayer.loop(0);
                xuanyunshijian=3;
                GameLiveTime-=10;
                ThingMap=0;
                CreateGold();
            }
            }
            AddGoldNum=0;
            break;
          }
         default :
            
         break;
         }
         
       }
   }
   if (GoldX<RobotManX-(20*80+80))
   {
   IsGoldisLive=false;
   }
// if (IsGetGold())
// {
//   GoldNum+=AddGoldNum;
//   //IsGoldisLive=false;
//   println("is can ");
// }
   
}
boolean IsGetGold(int x,int y)//主人公是否得到了金币
{

if(x<realRobotManX+80&&(y>realRobotManY-10&&y<realRobotManY+80))
{
    return true;
}
else
{
    return false;
}
   
}
// int LanFormMove()//雪景地形的移动参数计算函数
// {
//   if (m_LanFormMove<width)
//   {
//   m_LanFormMove++;
//   return m_LanFormMove;
//   }
//   else
//   {
//   m_LanFormMove=0;
//   return m_LanFormMove;
//   }
// }
boolean IsGameWin()
{
if(!IsGameOver())
{
    if (GoldNum>30+GameRank*70)
    {
      return true;
    }
    else
    {
      return false;
    }
}
else
{
    return false;   
}
}
boolean IsGameOver()//是否游戏结束 主人公生存时间为零,即游戏结束
{
if ((millis()/1000-PlayNowTime)>GameLiveTime)
{
    return true;
}
else
{
    return false;   
}
}
void CreditsPage()//显示制作人员的窗口
{
      stage=2;
      image(mybackground, 0, 0, width, height);
      textSize(100);
      fill(236, 245, 17);
      text("作者:杨帆",300,120);
      GetGameResult();
      textSize(60);
      text("至今挑战到了:"+BestGameRank+"游戏难度!", 0, 400);
      text("至今获得最大金币数量为:"+BestGameGoldNum, 0, 500);
}
void PrintText()//显示游戏数据,如主人公生存时间等
{
      fill(0,0,0,0);
      rect(450, 0, width, 30);
      textSize(30);
      fill(34,62,196,200);
      String m_time="";
      //m_time+="主人公生存时间:"+(GameLiveTime-(millis()/1000-PlayNowTime))+"S";
      m_time+=""+"游戏难度:"+GameRank;
      m_time+=""+"金币数:"+GoldNum;
      m_time+=""+"过关金币数:"+(30+GameRank*70);
      text(m_time,450,0,width,100);
      text("时间:", 0, 0,100, 100);
      DrawPowerRect();

}
void DrawPowerRect()//画时间条
{
int power;
power=(GameLiveTime-(millis()/1000-PlayNowTime))*5;
noFill();
rect(100, 0, 450, 30);
fill(30, 72, 232,200);
rect(100, 0, power, 30);
}
void GameOver()//游戏结束
{
stage=3;
// background(0);
MoveBackImage(WindowsX++,WindowsY++);
if (WindowsX>mybackground.width-width)
{
    WindowsX=0;
}
if (WindowsY>mybackground.height-height)
{
    WindowsY=0;
}
textSize(30);
text("按 A 键 ", 0, 80);
textSize(100);
text("再来一次!",220, 200);
textSize(30);
text("按 B 键 ", 0, 240);
textSize(100);
text("游戏结束!", 220, 350);
String m_time="金币数:"+GoldNum+"个";
text(m_time,220,500);
SaveGameResult();

}
void GameWin()
{
stage=4;
WindowsX=0;
WindowsY=0;
MoveBackImage(WindowsX++,WindowsY++);
if (WindowsX>mybackground.width-width)
{
    WindowsX=0;
}
if (WindowsY>mybackground.height-height)
{
    WindowsY=0;
}
textSize(30);
text("按 A 键 ", 0, 80);
textSize(100);
text("进行下一关!",220, 200);
textSize(30);
text("按 B 键 ", 0, 240);
textSize(100);
text("游戏结束!", 220, 350);
String m_time="金币数:"+GoldNum+"个";
text(m_time,220,500);
SaveGameResult();

}
void xiaxue()
{

for (int i = 0; i<10; i++){
    m_xiaxuehuazu.displayxiaxue();
}

}
void SaveGameResult()
{
GetGameResult();
if (GameRank>BestGameRank)
{
    myGameSave.setInt(0,0,GameRank);
    println("1BestGameRank: "+GameRank);
}
if (GoldNum>BestGameGoldNum)
{
    myGameSave.setInt(1,0,GoldNum);
    println("2BestGameGoldNum: "+GoldNum);
}
saveTable(myGameSave,"data/Save.csv");
}
void GetGameResult()
{
   BestGameRank=myGameSave.getInt(0,0);
   BestGameGoldNum=myGameSave.getInt(1,0);
   println("BestGameRank: "+BestGameRank);
   println("BestGameGoldNum: "+BestGameGoldNum);
}
void GetArduinoKey()
{
int temp;
while (myPort.available()>0)
{
      temp=myPort.read();
      Arduinokey=temp;
      println("temp: "+temp);
}
switch (stage)
{
    case 0 ://游戏开始选择画面
    {
      if (Arduinokey=='1')
      {
      stage=1;
      Arduinokey=0;
      InitGame();
      }
      if (Arduinokey=='2')
      {
      Arduinokey=0;
      CreditsPage();
      }
      break;
    }
    case 1 ://游戏开始
    {
      if (Arduinokey=='1')
      {
      Arduinokey=0;
      if (wudishijian>0)
      {
            wudishijian=0;
      }
      else
      {
          wudishijian=2;
      }
      
      EliminateObstacle();
      println("ok");
      }
      if (Arduinokey=='2')
      {
      Arduinokey=0;
      RobotManJump=1.5;
      }
      break;
    }
    case 2://制作人员
    {
      if(Arduinokey=='1'||Arduinokey=='2')
      {
      Arduinokey=0;
      BeginGame();
      }
      break;
    }
    case 3://游戏结束
    {
      if (Arduinokey=='1')
      {
      Arduinokey=0;
      stage=0;
      BeginGame();
      }
      if (Arduinokey=='2')
      {
      Arduinokey=0;
      exit();
      }
      break;
    }
    case 4://游戏胜利
    {
      if (Arduinokey=='1')
      {
      Arduinokey=0;
      WindowsX=0;
      WindowsY=0;
      GameRank++;

      InitGame();
      }
      if (Arduinokey=='2')
      {
      Arduinokey=0;
      exit();
      }
      break ;
    }   
         
}
}
public class xuehua
{


weizhi []xuehuazu=new weizhi;
public xuehua()
{
   for (int i = 0; i<5; i++)
   {
         xuehuazu=new weizhi();
   }
   for (int i = 0; i<5; i++)
   {
      xuehuazu.m_x=(int)random(0, width);
      xuehuazu.m_y=0;
      xuehuazu.m_h=20;
      xuehuazu.m_w=20;
   }

}
void Createxuehua()
{
    for (int i = 0; i<5; i++)
   {
      xuehuazu.m_x=(int)random(0, width);
      xuehuazu.m_y=0;
      xuehuazu.m_h=30;
      xuehuazu.m_w=30;
   }
}
void displayxiaxue()
{
    for (int i = 0; i<5; i++){
      image(xuehua3, xuehuazu.m_x, xuehuazu.m_y, xuehuazu.m_w, xuehuazu.m_h);
      xuehuazu.m_y+=(int)random(0, 20);
      if ((int)random(0, 2)>0)
      {
         xuehuazu.m_x+=(int)random(0, 5);
      }
      else
      {
          xuehuazu.m_x-=(int)random(0, 5);
      }
      if (xuehuazu.m_y>height+10)
      {
          Createxuehua();
      }
    }

}
}
public class weizhi
{

int m_x,m_y;
int m_h,m_w;
}










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

游戏中的截图。

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

迷你强 发表于 2013-7-19 16:07:56

好长。。。。。
页: [1]
查看完整版本: Processing之旅-----【给鱼插上翅膀,Processing基础扩展库5】