MBED GAME PROJECT: SPACE MISSION (PROCESSING CODE)

PROCESSING LANGUAGE CODE

//library for adding sound effect
import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

Minim intro; //creating object from Minim class
AudioPlayer player;

  import processing.serial.*;    // library for serial communication with arm
   //creating images and fonts objects
   PImage img;
   PImage gun1;
   PImage astd;
   PImage gun;
   PFont font;
   PFont font1;
   PFont font2;
   PFont font3;
   String name1= "";
   int scr1=0;
   String data1="";
   String name="";
   int scr=700;
   String combo="";
   int shoot=0;
   int index1=0;
   int index2=0;
   int index3=0;
  int cont=1;
  int jump=0;
  Serial port;  // Create object from Serial class for player1
  Serial port1;  // Create object from Serial class for player 2
int index4=0;
float val1=0;
int shoot1=0;
String data2="";
float val=0;
String typing="";   //typing name
String saved="";   //saved name
int score;
int astdSize = 60;
  boolean fire = false;
   boolean fire1 = false;
 int energy=100;
   boolean show=false;
  //Initialise gameOver variable
 int gameOver = 0;
  String tos;   // String in form of Name-score to send to arm for saving
int pos=100;
  int getRandomX()      // to generate random no. to control x-coordinate of astroids

  {
    return int(random(800));
  }
   // generate the 8 random x-coordinates for 8 astroids
  int[] astdx={ getRandomX(), getRandomX(), getRandomX(), getRandomX(), getRandomX(), getRandomX(), getRandomX(), getRandomX() };

  int[] astdy = { 0, 0, 0, 0, 0 ,0 ,0 , 0}; //all y-coordinates 0 initially
   
  void setup()
  {
    size (900, 600);
    smooth (); 
          intro = new Minim(this);
       player = intro.loadFile("marcus_kellis_theme.mp3");  //load audio file
  
  // play the audio file
  
  player.play();
  
    port = new Serial(this,"COM17",9600); // serial port for player1
    port.bufferUntil('\n'); //reading data
     port1 = new Serial(this,"COM18",9600); // serial port for player2
     port1.bufferUntil('\n'); //reading data
    vx(); // to check if x coordinate of aestroid is reachable.
    //load images and fonts
    img = loadImage("universe.jpg");
     astd=loadImage("astd.png");
     gun = loadImage("gun.png");
     gun1 = loadImage("gun1.png");
     font = loadFont("Algerian-48.vlw");
     textFont(font);
     font1=loadFont("Astonished-60.vlw");
     font2=loadFont("28_Days_Later-48.vlw");
     font3=loadFont("ArialRoundedMTBold-48.vlw");
     frameRate(30); //frames per second is 30
  }
   
  void draw()
  {
     // save a copy of score in a text file
       String[] line = loadStrings("score.txt");
      int part=line[0].indexOf("-");
      name=line[0].substring(0,part);
      scr=int(line[0].substring(part+1,line[0].length()));
       image(img,0,0);
       //screen design
      if (show ==false)
   {
     fill(255,0,0);
      textFont(font2);
     text("WELCOME TO THE SPACE MISSION",pos,100);
     fill(192,197,198);
    textFont(font3);
    textSize(30);
    text("Make a TEAM of Two players and shoot the Aestroids\nSAVE THE EARTH...BEST OF LUCK!!!",pos,200);

     textSize(60);
      textFont(font);
       fill(245,119,203);
      text("Enter your TEAM name:", pos, 350);
      fill(241,252,0);
      text(typing,pos+575,350);
      textFont(font3);
      fill(222,201,43);
      textSize(30);
       text("INSTRUCTIONS:\nFor Each Bullet you lose energy by 1\nFor each correct shot you gain energy by 5\n"+"HIGHEST SCORE ========> "+scr+ " by "+ name,pos,450);
   }
   else
   {
    fill(255);
    stroke (255);
    fill(255);
    textFont(font3);
    text("SCORE: "+score, 20,40);    //show score
    if (energy<50)
    {
      fill(255,0,0);
    }
    text("ENERGY: "+energy, 550,40);  //show energy
    image(gun1, val1, 550); //loading gun image
//energy should be greater than 0 to shoot
    if (energy>0)
    {
      if (shoot==1)
      {
      fire=true;
      }
       if (shoot1==1)
      {
      fire1=true;
      } 
    }
//energy can't increase 200
    if (energy>200)
    {
      energy=200;
    }
//enegy can't decrease 0
    if (energy<0)
    {
      energy=0;
    }
    if(fire) //player1
    {
      cannon(int(val+40)); //shoot
      energy--; //energy decrease
      fire = false;
    }
     if(fire1) //player2
    {
      cannon1(int(val1+40)); //shoot
      energy--; //energy decrease
      fire1 = false;
    }
     astdFalling();
     gameFinish(); 
   } 
   }
 void astdFalling()        // to increase y-coordinate of asteroids
  {    
    for (int i=0; i<8; i++)
    {
          image(astd,astdx[i], astdy[i]++);      
    }  
  }
   
  void cannon(int shotX)        //bullet from gun for player1
  {
    boolean strike = false;
    for (int i = 0; i < 8; i++)
    {
     if((shotX >= astdx[i]) && (shotX <= (astdx[i]+astdSize))) {
        strike = true;
        line(val+40, 565, val+40, astdy[i]);
        ellipse(astdx[i], astdy[i],
                astdSize+25, astdSize+25);
        astdx[i] = getRandomX();
        vx();
        astdy[i] = 0; 
// update score
        score++;
        energy=energy+5;
      }   
    }
   
    if(strike == false)
    {
      line(val+40, 565, val+40, 0);
    } 
  }
   
    void cannon1(int shotX)    //bullet from gun for player2
  {
    boolean strike = false;
    for (int i = 0; i < 8; i++)
    {
     if((shotX >= astdx[i]) && (shotX <= (astdx[i]+astdSize))) {
        strike = true;
        fill(238,252,41);
        line(val1+40, 565, val1+40, astdy[i]);
        ellipse(astdx[i], astdy[i],
                astdSize+25, astdSize+25);
        astdx[i] = getRandomX();
        vx();
        astdy[i] = 0;
        fill(255,255,255);
// update score
        score++;
        energy=energy+10; // correct shot will increase energy
      }   
    }
   
    if(strike == false)
    {
      line(val1+40, 565, val1+40, 0);
    } 
   
  }

  void gameFinish()     //GameOver
 
  {
    for (int i=0; i< 8; i++)
    {
      if(astdy[i]==height)
      {
 
    textFont(font);
       fill(color(255,255,255));
    textSize(30);
    textAlign(CENTER);
    text("GAME OVER", width/2, height/2);
    text("You scored : "+ score, width/2, height/2 + 50);
    if (score>=scr)
    {
    text("Congrats "+ saved + "! for HIGHEST SCORE", width/2, height/2 + 100);
     tos=saved+"-"+score;
     String[] name_score = {tos};
   saveStrings("score.txt",name_score);
    port1.write(tos);
    }
    else
    {
     text("Sorry! "+ saved + " You could not SCORE HIGHEST : ", width/2, height/2 + 100);
     text("HIGHEST SCORE : "+scr+ " by " + name,width/2, height/2 + 150);
    }
     noLoop(); // no repeat
       }
      }
    }

void player(Serial port)    //serial communication with arm for player 1
{
   data1=port.readStringUntil('\n'); //string in form of acclerometer_data#button_value
  if (data1!=null)
  {
 data1=data1.substring(0,data1.length()-1);
  index2=data1.indexOf("#"); //position of #
  if (index2>=2)
  {
  val=float(data1.substring(0,index2)); //accelerometer data
  shoot=int(data1.substring(index2+1,data1.length())); //button value
   val=constrain(val,250,700); //constraining the acclerometer data
 val=map(val,250,700,0,900);  //mapping the data with width of screen
  }
  }
}

void serialEvent(Serial port1)  // serial communication with arm for player 2
{
  data2=port1.readStringUntil('\n'); //string in form of acclerometer_data#button_value
  if (data2!=null)
  {
    
 data2=data2.substring(0,data2.length()-1);

  
  index4=data2.indexOf("#");  //position of #
  if (index4>=2)
  {
   val1=float(data2.substring(0,index4)); //accelerometer data
  shoot1=int(data2.substring(index4+1,data2.length())); //button value
  
   val1=constrain(val1,250,700); //constraining the acclerometer data
 val1=map(val1,250,700,0,900); //mapping the data with width of screen
  }

}
}

void vx() // for putting the x-coordinate of asteroid to reachable position
{
  for (int i=0;i<8;i++)
  {
    if (astdx[i]<50)
    {
      astdx[i]=55;
    }
  }
}
void keyPressed() // for input name
 {
  if (show==false)
  {
  // If the return key is pressed, save the String and clear it
  if (key == '\n' ) {
    saved = typing;
    // A String can be cleared by setting it equal to ""
    typing = ""; 
   show=!false;
   
   if (saved=="")
   saved="noName";
   println(saved);
  } else {
    // Otherwise, concatenate the String
    // Each character typed by the user is added to the end of the String variable.
    typing = typing + key; 
  }
  }
}


Please log in to post comments.