2 Commits 8b21f3c56f ... 164983d58e

Author SHA1 Message Date
  UltrasonicMadness 164983d58e Pressing space now toggles the bubbles on and off 5 years ago
  UltrasonicMadness e462779463 Updated copyright year and changed from GNU to Allman indentation 5 years ago
2 changed files with 74 additions and 68 deletions
  1. 28 40
      RainbowBubble/Bubble.pde
  2. 46 28
      RainbowBubble/RainbowBubble.pde

+ 28 - 40
RainbowBubble/Bubble.pde

@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 UltrasonicMadness
+ * Copyright 2018 UltrasonicMadness
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -15,7 +15,7 @@
  */
 
 public class Bubble
-  {
+{
     // X and Y position of bubble
     private int xPos;
     private int yPos;
@@ -31,7 +31,7 @@ public class Bubble
     private boolean active;
   
     public Bubble(int initXPos, int initYPos, int initSize)
-      {
+    {
         xPos = initXPos;
         yPos = initYPos + initSize;
         size = initSize;
@@ -45,71 +45,59 @@ public class Bubble
     
         // The speed is between a quarter and a half of the bubble's size
         speed = int(random(initSize * 0.25, initSize * 0.5));
-      }
-  
-    public void update()
-      {
-        // Small chance of activating the bubble.
-        if (int(random(0,64)) == 3)
-          {
-            this.activate();
-          }
+    }
     
-        this.advance();
-        this.draw();
-      }
-  
-    private void draw()
-      {
+    public void draw()
+    {
         fill(196,196,255,128);
         stroke(0,0,0,128);
         ellipse(xPos + xOffset, yPos, size, size);
-      }
+    }
   
-    private void advance()
-      {
+    public void advance()
+    {
         if (active)
-          {
+        {
             // Move bubble up
             yPos = yPos - speed;
     
             // If X offset is over (or under) a certain value, swap direction
             if (xOffset <= -(xOffsetMax))
-              {
+            {
                 movingRight = true;
-              }
+            }
             else if (xOffset >= xOffsetMax)
-              {
+            {
                 movingRight = false;
-              }
+            }
       
             // Move bubble 1-3 pixels left or right
             if (movingRight)
-              {
+            {
                 xOffset += int(random(1,3));
-              }
+            }
             else
-              {
+            {
                 xOffset -= int(random(1,3));
-              }
+            }
     
             // Deactivate bubble if it is off screen
             if (yPos < -size)
-              {
+            {
                 active = false;
-              }
-          }
-      }
+            }
+        }
+    }
   
-    private void activate()
-      {
+    public void activate()
+    {
         // If this bubble is not currently active.
         if (!active)
-          {
+        {
             active = true;
     
             xPos = int(random(-size, width + size));
             yPos = height + size;
-          }
-      }
-  }
+        }
+    }
+}

+ 46 - 28
RainbowBubble/RainbowBubble.pde

@@ -1,5 +1,5 @@
 /*
- * Copyright 2017 UltrasonicMadness
+ * Copyright 2018 UltrasonicMadness
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -17,8 +17,11 @@
 // Array of 256 bubbles
 Bubble[] bubbles = new Bubble[256];
 
+// The bubbles can be turned off.
+boolean bubblesOn = true;
+
 void setup()
-  {
+{
     size(800,600);
     noCursor();
     
@@ -27,63 +30,78 @@ void setup()
      * below the screen and a radius between 15 and 25 pixels.
      */
     for (int bubbleCounter = 0; bubbleCounter < bubbles.length; bubbleCounter++)
-      {
+    {
         bubbles[bubbleCounter] = new Bubble(int(random(0, width)), height, int(random(15,25)));
-      }
-  }
+    }
+}
 
 void draw()
-  {
+{
     genBackground();
     genRainbow(196);
     
     // Update each bubble
     for (int bubbleCounter = 0; bubbleCounter < bubbles.length; bubbleCounter++)
-      {
-        bubbles[bubbleCounter].update();
-      }
-  }
+    {
+        // Small chance of activating the bubble if the bubbles are on.
+        if (int(random(0,64)) == 3 && bubblesOn)
+        {
+            bubbles[bubbleCounter].activate();
+        }
+      
+        bubbles[bubbleCounter].advance();
+        bubbles[bubbleCounter].draw();
+    }
+}
 
 void genBackground()
-  {
+{
     fill(255);
     noStroke();
     
     rect(0,0,width,height);
-  }
+}
 
 void genRainbow(int alpha)
-  {
+{
     int counter = frameCount % 256;
     int colorTransitionId = (frameCount / 256) % 6;
     noStroke();
     
     switch (colorTransitionId)
-      {
+    {
         case 0: // red to yellow, red at 255, green ascending, blue at 0
-          fill(255, counter, 0, alpha);
-          break;
+            fill(255, counter, 0, alpha);
+            break;
       
         case 1: // yellow to green, red descending, green at 255, blue at 0
-          fill(255 - counter, 255, 0, alpha);
-          break;
+            fill(255 - counter, 255, 0, alpha);
+            break;
       
         case 2: // green to cyan, red at 0, green at 255, blue ascending
-          fill(0, 255, counter, alpha);
-          break;
+            fill(0, 255, counter, alpha);
+            break;
     
         case 3: // cyan to blue, red at 0, green descending, blue at 255
-          fill(0, 255 - counter, 255, alpha);
-          break;
+            fill(0, 255 - counter, 255, alpha);
+            break;
     
         case 4: // blue to pink, red ascending, green at 0, blue at 255 
-          fill(counter, 0, 255, alpha);
-          break;
+            fill(counter, 0, 255, alpha);
+            break;
       
         case 5: // pink to red, red at 255, green at 0, blue descending 
-          fill(255, 0, 255 - counter, alpha);
-          break;
-      }
+            fill(255, 0, 255 - counter, alpha);
+            break;
+    }
   
     rect(0,0,width,height);
-  }
+}
+
+void keyPressed()
+{
+    if (key == ' ')
+    {
+        bubblesOn = !bubblesOn;
+    }
+}