Loading...
Logo
Processing Forum

Balloon Popper

in Share your Work  •  4 months ago  
Copy code
  1. import ddf.minim.*;
  2. /*
  3.  Pop the balloon game.
  4.  @author Abbas Noureddine
  5.  This is a simple balloon popping game, you must pop the falling balloons 
  6.  before it reaches the bottom, as the level increases, speed increases, balloon size
  7.  decreases (dynamically).
  8.  failing to pop a specific ammount of balloons will restart the game.
  9.  (this is an infrastructure of the game, a demo of how to implement,
  10.  no effort exerted on the interface).
  11.  
  12.  pop sound: https://soundcloud.com/halturpin/bubble-pop
  13.  
  14.  */


  15. //lives describes how many lives left
  16. //level determines speed/ frame rate
  17. //radius of balloon, it changes with level
  18. //interval is the # frames btw generating 
  19. //remaining is for the number of balloons that isn't generated yet
  20. int lives, level, radius, interval, remaining, last_generate, score, frame_rate;

  21. ArrayList<PVector> balloons;
  22. ArrayList<PVector> velocity;

  23. PShape my_triangle, backGround;//use to draw a triangle, although we have a built in triangle method
  24. Minim minim=new Minim(this);
  25. AudioSample pop_sound;


  26. void setup()
  27. {

  28.   int starting_level=7; 

  29.   size(740, 640, P2D);//P2D to use PShape
  30.   frameRate(30);
  31.   textSize(16);
  32.   initialize();
  33.   ellipseMode(CENTER); 
  34.   rectMode(CENTER);

  35.   for (int i=0; i<starting_level-1; i++)
  36.     level_up();

  37.   //get audio
  38.   pop_sound= minim.loadSample("pop.WAV");// make sure to save the audio file in the directory of the applet

  39.   //a very simple use of PShape/ buffers  
  40.   my_triangle=createShape();

  41.   my_triangle.beginShape(TRIANGLE);
  42.   my_triangle.stroke(0);
  43.   my_triangle.fill(100, 150);
  44.   my_triangle.vertex(0, 0);
  45.   my_triangle.vertex(5, -15);
  46.   my_triangle.vertex(10, 0);
  47.   my_triangle.endShape(CLOSE);

  48.   backGround=createShape();

  49.   backGround.beginShape(TRIANGLE_FAN);
  50.   backGround.noStroke();
  51.   backGround.fill(130,130,128);
  52.   backGround.vertex(width/2, height/2);
  53.   backGround.fill(0,0,0);
  54.   backGround.vertex(0, 0);
  55.   backGround.fill(255,0,0);
  56.   backGround.vertex(0, height);
  57.   backGround.fill(0,255,0);
  58.   backGround.vertex(width, height);
  59.   backGround.fill(0,0,255);
  60.   backGround.vertex(width, 0);
  61.   backGround.fill(0,0,0);
  62.   backGround.vertex(0, 0);
  63.   backGround.endShape(CLOSE);
  64. }

  65. void initialize()
  66. {

  67.   score=0;
  68.   frame_rate=30;
  69.   lives=10;
  70.   level=1;
  71.   radius=45;
  72.   interval=int(1.7*30);//this stays CONST, but the game gets faster
  73.   last_generate=interval;
  74.   remaining=8+int(level*1.75);

  75.   balloons=new ArrayList<PVector>();
  76.   velocity=new ArrayList<PVector>();
  77. }

  78. void level_up()
  79. {
  80.   level++;
  81.   radius=int(45-level*1.5);
  82.   remaining=8+int(level*1.75);
  83.   last_generate=interval;
  84.   frame_rate+=7;

  85.   if (radius<3)
  86.     initialize();//start new turn
  87. }

  88. void generate()//generate balloon
  89. {
  90.   if (remaining==0)
  91.     return;
  92.   else
  93.   {
  94.     last_generate=interval;//reset generating timer

  95.     //start at y from above the window
  96.     balloons.add(new PVector(int(random(width-100))+50, -radius+10));
  97.     //velocity doesn't have much of an effect, since we 
  98.     //control speed with frame rate, but it control direction.
  99.     int x_direction= int(random(2))==0?1:-1;
  100.     velocity.add(new PVector(int(2+random(level)*0.35)*x_direction, int(2+random(level)*0.35)));
  101.     velocity.get(velocity.size()-1).mult(frame_rate/frameRate);

  102.     remaining--;
  103.   }
  104. }

  105. void move()
  106. {
  107.   for (int i=0; i<balloons.size(); i++)
  108.   {
  109.     //if balloon reached the spikes
  110.     if (balloons.get(i).y+radius>height-15)//out, pop it 
  111.     {
  112.       //play pop sound
  113.       pop_sound.trigger();
  114.       //last draw of the popped balloon
  115.       fill(#BF0D9F, 100);
  116.       noStroke();
  117.       ellipse(balloons.get(i).x, balloons.get(i).y, radius*2*1.75, radius*2*1.75);

  118.       lives--;
  119.       balloons.remove(i);
  120.       velocity.remove(i);
  121.       i--;//so that the loop won't miss the next balloon, since it will take this index
  122.     }
  123.     else
  124.     {
  125.       //check right and left boundary
  126.       if (balloons.get(i).x+radius>width || balloons.get(i).x-radius<0)
  127.       {
  128.         balloons.get(i).x= balloons.get(i).x<radius?radius:(width-radius);
  129.         velocity.get(i).x*=-1;
  130.       }

  131.       balloons.get(i).add(velocity.get(i));//add velocity vector to position vector
  132.     }
  133.   }
  134. }

  135. void mousePressed()
  136. {
  137. /*}

  138. void check_mouse()
  139. {
  140.   if (mousePressed)
  141.   {*/
  142.     float distance;
  143.     for (int i=0; i<balloons.size(); i++)
  144.     {
  145.       distance=sqrt(pow((mouseX-balloons.get(i).x), 2) + pow((mouseY-balloons.get(i).y), 2) );
  146.       if (distance<radius+4)//add 4 to give the player a helping edge
  147.       {
  148.         //play pop sound
  149.         pop_sound.trigger();
  150.         //last draw of the popped balloon
  151.         fill(#BF0D9F, 100);
  152.         noStroke();
  153.         ellipse(balloons.get(i).x, balloons.get(i).y, radius*2*1.75, radius*2*1.75);

  154.         score++;
  155.         balloons.remove(i);
  156.         velocity.remove(i);
  157.         i--;//so that the loop won't miss the next balloon, since it will take this index
  158.       }
  159.     }
  160.   //}
  161. }

  162. void draw()
  163. {
  164.   background(220);
  165.   shape(backGround);

  166.   last_generate-=frameRate/30;

  167.   if (lives==0)
  168.   {
  169.     initialize();//restart game
  170.     return;//don't continue
  171.   }

  172.   if ( balloons.size()==0 && remaining==0)
  173.   {
  174.     level_up();
  175.     return;
  176.   }

  177.   if (last_generate<=0)
  178.     generate();//add a new balloon
  179.   
  180.   //check_mouse();
  181.   move();

  182.   //draw balloooons
  183.   stroke(50);
  184.   fill(190,15,160, 160);
  185.   for (int i=0; i<balloons.size(); i++)
  186.   {
  187.     ellipse(balloons.get(i).x, balloons.get(i).y, radius*2, radius*2);
  188.   }

  189.   //draw spikes
  190.   pushMatrix();//to make sure that the translates doesn't affect the texts (below)
  191.   translate(0, height);
  192.   for (int i=0; i<width; i+=10)
  193.   {
  194.     shape(my_triangle);
  195.     translate(10, 0);
  196.   }
  197.   popMatrix();

  198.   fill(0);
  199.   text("Score: "+score+"\nLives: "+lives+"\nLevel: "+level+"\nRemaining: "+remaining+"\nFrame Rate: "+int(frameRate), width-220, 30);
  200. }

Replies(3)

Re: Balloon Popper

4 months ago
Hey! Very nice game! Loved the popped effects!  
I had to remove PShape to make it work @ v1.5.1.
Hope I'm not missing so much. ^_^

Re: Balloon Popper

4 months ago
thanks,
and the PShapes were included just as a demonstration how to make use of a pshape, pshape myTriangle draws a triangle (which doesn't do any thing different than what processing's triangle does)-used to draw needles/spikes at the bottom of the screen, and pshape backGround is used to draw a nice background 




have fun :)

Re: Balloon Popper

4 months ago
Oh, I was wondering why they'd pop when reached the bottom! Nasty spikes/needles!!!