Create a sick game with Processing

Posted: 03/28/2009

I did a little experimenting with Processing, a language written for artists, graphics designers, and anyone interested in learning something new. I was very impressed with how easy it was to get started. The download comes with tons of simple example projects, and the API reference is easy to understand. I had hopes of Processing opening a whole, previously undiscovered, artistic side of my brain — it didn’t. But I did make a killer “blow up the falling spheres with my triangle’s laser” game. Check it out:

Wow, that screen shot makes it look even more lame than it really is (hard to do).

I know, I know, you want the code so you can try this game out for yourself. Here it is, paste into Processing and hit play:

  PFont fontA;
  int sphereDiameter = 10;
  boolean shoot = false;
  
  int randx()
  {
    return int(random(600));
  }
  
  int[] sphereXCoords = { randx(), randx(), randx(), randx(), randx() };
  int[] sphereYCoords = { 0, 0, 0, 0, 0 };
  
  void setup()
  {
    size(600,620);
  }
  
  void draw()
  {
    background(0);
    fill(color(0,255,0));
    stroke(color(0,255,0));
    triangle(mouseX-8, 580, mouseX+8, 580, mouseX, 565);
    fill(color(255,0,0));
    stroke(color(255,0,0));
  
    if(shoot==true)
    {
      sphereKiller(mouseX);
      shoot = false;
    }
  
    sphereDropper();
    gameEnder();  
  }
  
  void mousePressed()
  {
    shoot = true;
  }
  
  void sphereDropper()
  {  
    stroke(255);
    fill(255);
    for (int i=0; i<5; i++)
    {
      ellipse(sphereXCoords[i], sphereYCoords[i]++,
              sphereDiameter, sphereDiameter);
    }
  }
  
  void sphereKiller(int shotX)
  {
    boolean hit = false;
    for (int i = 0; i < 5; i++)
    {
      if((shotX >= (sphereXCoords[i]-sphereDiameter/2)) && 
         (shotX <= (sphereXCoords[i]+sphereDiameter/2)))
      {
        hit = true;
        line(mouseX, 565, mouseX, sphereYCoords[i]);
        ellipse(sphereXCoords[i], sphereYCoords[i],
                sphereDiameter+25, sphereDiameter+25);
        sphereXCoords[i] = randx();
        sphereYCoords[i] = 0;
      }    
    }
  
    if(hit == false)
    {
      line(mouseX, 565, mouseX, 0);
    }  
  
  }
  
  void gameEnder()
  {
    for (int i=0; i< 5; i++)
    {
      if(sphereYCoords[i]==600)
      {
        fill(color(255,0,0));
        noLoop();
      }
    }
  }

About Me

I'm a skier, web developer, entrepreneur, freelancer, and all around stand-up guy living in Manhattan. This is me, repping one of my favorite shirts...

Follow me on twitter or send me an email.

All Posts