Balloon Popper
in
Share your Work
•
4 months ago
- import ddf.minim.*;
- /*
- Pop the balloon game.
- @author Abbas Noureddine
- This is a simple balloon popping game, you must pop the falling balloons
- before it reaches the bottom, as the level increases, speed increases, balloon size
- decreases (dynamically).
- failing to pop a specific ammount of balloons will restart the game.
- (this is an infrastructure of the game, a demo of how to implement,
- no effort exerted on the interface).
- pop sound: https://soundcloud.com/halturpin/bubble-pop
- */
- //lives describes how many lives left
- //level determines speed/ frame rate
- //radius of balloon, it changes with level
- //interval is the # frames btw generating
- //remaining is for the number of balloons that isn't generated yet
- int lives, level, radius, interval, remaining, last_generate, score, frame_rate;
- ArrayList<PVector> balloons;
- ArrayList<PVector> velocity;
- PShape my_triangle, backGround;//use to draw a triangle, although we have a built in triangle method
- Minim minim=new Minim(this);
- AudioSample pop_sound;
- void setup()
- {
- int starting_level=7;
- size(740, 640, P2D);//P2D to use PShape
- frameRate(30);
- textSize(16);
- initialize();
- ellipseMode(CENTER);
- rectMode(CENTER);
- for (int i=0; i<starting_level-1; i++)
- level_up();
- //get audio
- pop_sound= minim.loadSample("pop.WAV");// make sure to save the audio file in the directory of the applet
- //a very simple use of PShape/ buffers
- my_triangle=createShape();
- my_triangle.beginShape(TRIANGLE);
- my_triangle.stroke(0);
- my_triangle.fill(100, 150);
- my_triangle.vertex(0, 0);
- my_triangle.vertex(5, -15);
- my_triangle.vertex(10, 0);
- my_triangle.endShape(CLOSE);
- backGround=createShape();
- backGround.beginShape(TRIANGLE_FAN);
- backGround.noStroke();
- backGround.fill(130,130,128);
- backGround.vertex(width/2, height/2);
- backGround.fill(0,0,0);
- backGround.vertex(0, 0);
- backGround.fill(255,0,0);
- backGround.vertex(0, height);
- backGround.fill(0,255,0);
- backGround.vertex(width, height);
- backGround.fill(0,0,255);
- backGround.vertex(width, 0);
- backGround.fill(0,0,0);
- backGround.vertex(0, 0);
- backGround.endShape(CLOSE);
- }
- void initialize()
- {
- score=0;
- frame_rate=30;
- lives=10;
- level=1;
- radius=45;
- interval=int(1.7*30);//this stays CONST, but the game gets faster
- last_generate=interval;
- remaining=8+int(level*1.75);
- balloons=new ArrayList<PVector>();
- velocity=new ArrayList<PVector>();
- }
- void level_up()
- {
- level++;
- radius=int(45-level*1.5);
- remaining=8+int(level*1.75);
- last_generate=interval;
- frame_rate+=7;
- if (radius<3)
- initialize();//start new turn
- }
- void generate()//generate balloon
- {
- if (remaining==0)
- return;
- else
- {
- last_generate=interval;//reset generating timer
- //start at y from above the window
- balloons.add(new PVector(int(random(width-100))+50, -radius+10));
- //velocity doesn't have much of an effect, since we
- //control speed with frame rate, but it control direction.
- int x_direction= int(random(2))==0?1:-1;
- velocity.add(new PVector(int(2+random(level)*0.35)*x_direction, int(2+random(level)*0.35)));
- velocity.get(velocity.size()-1).mult(frame_rate/frameRate);
- remaining--;
- }
- }
- void move()
- {
- for (int i=0; i<balloons.size(); i++)
- {
- //if balloon reached the spikes
- if (balloons.get(i).y+radius>height-15)//out, pop it
- {
- //play pop sound
- pop_sound.trigger();
- //last draw of the popped balloon
- fill(#BF0D9F, 100);
- noStroke();
- ellipse(balloons.get(i).x, balloons.get(i).y, radius*2*1.75, radius*2*1.75);
- lives--;
- balloons.remove(i);
- velocity.remove(i);
- i--;//so that the loop won't miss the next balloon, since it will take this index
- }
- else
- {
- //check right and left boundary
- if (balloons.get(i).x+radius>width || balloons.get(i).x-radius<0)
- {
- balloons.get(i).x= balloons.get(i).x<radius?radius:(width-radius);
- velocity.get(i).x*=-1;
- }
- balloons.get(i).add(velocity.get(i));//add velocity vector to position vector
- }
- }
- }
- void mousePressed()
- {
- /*}
- void check_mouse()
- {
- if (mousePressed)
- {*/
- float distance;
- for (int i=0; i<balloons.size(); i++)
- {
- distance=sqrt(pow((mouseX-balloons.get(i).x), 2) + pow((mouseY-balloons.get(i).y), 2) );
- if (distance<radius+4)//add 4 to give the player a helping edge
- {
- //play pop sound
- pop_sound.trigger();
- //last draw of the popped balloon
- fill(#BF0D9F, 100);
- noStroke();
- ellipse(balloons.get(i).x, balloons.get(i).y, radius*2*1.75, radius*2*1.75);
- score++;
- balloons.remove(i);
- velocity.remove(i);
- i--;//so that the loop won't miss the next balloon, since it will take this index
- }
- }
- //}
- }
- void draw()
- {
- background(220);
- shape(backGround);
- last_generate-=frameRate/30;
- if (lives==0)
- {
- initialize();//restart game
- return;//don't continue
- }
- if ( balloons.size()==0 && remaining==0)
- {
- level_up();
- return;
- }
- if (last_generate<=0)
- generate();//add a new balloon
- //check_mouse();
- move();
- //draw balloooons
- stroke(50);
- fill(190,15,160, 160);
- for (int i=0; i<balloons.size(); i++)
- {
- ellipse(balloons.get(i).x, balloons.get(i).y, radius*2, radius*2);
- }
- //draw spikes
- pushMatrix();//to make sure that the translates doesn't affect the texts (below)
- translate(0, height);
- for (int i=0; i<width; i+=10)
- {
- shape(my_triangle);
- translate(10, 0);
- }
- popMatrix();
- fill(0);
- text("Score: "+score+"\nLives: "+lives+"\nLevel: "+level+"\nRemaining: "+remaining+"\nFrame Rate: "+int(frameRate), width-220, 30);
- }