SHARE
TWEET

Untitled

a guest Feb 26th, 2014 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Fast close of missle command: https://en.wikipedia.org/wiki/Missile_Command
  2.  
  3. int points = 0;
  4. int lives = 3;
  5.  
  6. int framesTillAnotherDrop = 200;
  7.  
  8. //KINDS OF THINGS
  9. // bomb
  10.  
  11. //TBD
  12. // make 3 things fall from sky at regular intervals
  13. // add mouse click that shows radius around faller
  14. //
  15. //
  16.  
  17.  
  18. ArrayList<Thing> things;
  19. float particlesPerFirework = 1;
  20.  
  21. Bomb bomb;
  22.  
  23. //Add a bunch of things and "place them" somewhere
  24. void populateThings()
  25. {
  26.   things = new ArrayList<Thing>();
  27.   //start in the midle position
  28.   float x = width/2;
  29.   float y = 0;//height/2;
  30.  
  31.  
  32.  
  33.   //Use mouse position for x and y.
  34.   //////  x = mouseX;
  35.   //////  y = mouseY;
  36.  
  37.   float numberOfParticles = particlesPerFirework;
  38.   float theHue = random(100);
  39.   for (int i=0; i<numberOfParticles; i+=1)
  40.   {
  41.     //x = i*(width/numberOfParticles);
  42.     //float dy = noise(i)*100;
  43.     Thing aThing = new Thing(x, y);
  44.     aThing.hue = theHue;
  45.     things.add(aThing);
  46.   }
  47. }
  48.  
  49.  
  50.  
  51. class Thing
  52. {
  53.   float x;
  54.   float y;
  55.  
  56.   float vx;
  57.   float vy;
  58.  
  59.   float size;
  60.   float hue;
  61.  
  62.   Thing(float x, float y)
  63.   {
  64.     this.x = x;
  65.     this.y = y;
  66.     size = 6;
  67.     hue = random(100);//random hue
  68.  
  69.     vx = random(5) - 2.5;
  70.     vy = random(1);//always fall down
  71.   }
  72.  
  73.   boolean isTouching(Thing other)//simple distance based hit test
  74.   {
  75.     double distToOther = Math.sqrt((x-other.x)*(x-other.x) +(y-other.y)*(y-other.y));
  76.     if (distToOther <= (.5*this.size + .5*other.size))
  77.       return true;
  78.     return false;
  79.   }
  80.  
  81.   //Draws the "thing"
  82.   void drawMe()
  83.   {
  84.     fill(hue, 100, 100);
  85.     ellipse(x, y, size, size);
  86.     stroke(255,0,0,50);//red X
  87.     line(x-10,y-10,x+10,y+10);
  88.     line(x-10,y+10,x+10,y-10);
  89.    
  90.     //System.out.println("Drawing at "+x+" "+" "+y);
  91.   }
  92.  
  93.   void fall()
  94.   {
  95.     vy += .1*.25;
  96.   }
  97.  
  98.   void moveByVel()
  99.   {
  100.     x += vx;
  101.     y += vy;
  102.   }
  103.  
  104.   //brings to the top, but only sometimes.
  105.   void maybeJump()
  106.   {
  107.     if (random(1000) < 1)
  108.       y = 0;
  109.   }
  110. }
  111.  
  112. class Bomb extends Thing
  113. {
  114.   float timeToLive;
  115.   Bomb(float x, float y, float timeToLive, float rad)
  116.   {
  117.     super(x, y);
  118.     this.size = rad;
  119.     this.timeToLive = timeToLive;
  120.   }
  121.  
  122.   void decay()
  123.   {
  124.     timeToLive -= 1;
  125.     size += 1;
  126.   }
  127.  
  128.   void drawMe()
  129.   {
  130.     if (timeToLive < 0)
  131.     {
  132.     }
  133.     else
  134.     {
  135.       super.drawMe();
  136.     }
  137.   }
  138. }
  139.  
  140.  
  141. //Not great code
  142. void mouseBomb()
  143. {
  144.   if (bomb == null)
  145.     bomb = new Bomb(mouseX, mouseY, 55, 10);
  146. }
  147.  
  148.  
  149.  
  150. void draw() {
  151.  
  152.   background(100);
  153.  
  154.   if (frameCount % framesTillAnotherDrop == 0)
  155.   {
  156.     populateThings();
  157.   }
  158.   stroke(0);
  159.   fill(0);
  160.   textSize(32);
  161.   text("points "+points, 32, 60);
  162.   if (things == null) //Stop if there are no things
  163.     return;
  164.   for (Thing thing: things)
  165.   {
  166.  
  167.  
  168.  
  169.     if (bomb != null)//A bomb is there
  170.     {
  171.       if (thing.isTouching(bomb))
  172.         thing.vy = -10;
  173.     }
  174.  
  175.     thing.drawMe();
  176.     thing.moveByVel();
  177.     thing.fall();
  178.  
  179.  
  180.  
  181.     //if(thing.y > -10+height)
  182.     //  thing.maybeJump();
  183.     //    p.run();
  184.     //    if (p.isDead()) {
  185.     //      it.remove();
  186.   }
  187.   if (bomb != null)
  188.   {
  189.     bomb.drawMe();
  190.     bomb.decay();
  191.     if (bomb.timeToLive < 0)
  192.     {
  193.       bomb = null;//make the bomb go away
  194.       points += 1;
  195.     }
  196.   }
  197. }
  198.  
  199. void mouseClicked()
  200. {
  201.   //background(0); // clear screen
  202.   //populateThings(); //start over
  203.   mouseBomb();
  204. }
  205.  
  206. void setup() {
  207.   size(640, 360);
  208.   colorMode(HSB, 100);
  209.   //mono = loadFont("AndaleMono-32.vlw");
  210.   //populateThings();
  211. }
RAW Paste Data
Top