A bubble tube sketch written in Processing. http://www.ultrasonicmadness.org/software/rainbow-bubble/

[[ 🗃 ^zrebo rainbow-bubble ]] :: [📥 Inbox] [📤 Outbox] [🐤 Followers] [🤝 Collaborators] [🛠 Commits]

Clone

HTTPS: git clone https://vervis.peers.community/repos/zrebo

SSH: git clone USERNAME@vervis.peers.community:zrebo

Branches

Tags

master :: RainbowBubble /

Bubble.pde

/*
 * 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.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Bubble
{
    // X and Y position of bubble
    private int xPos;
    private int yPos;
  
    // X offset, max X offset and whether bubble is moving to the right
    private int xOffset;
    private int xOffsetMax;
    private boolean movingRight; 
  
    // Size, speed and whether bubble is active
    private int size;
    private int speed;
    private boolean active;
  
    public Bubble(int initXPos, int initYPos, int initSize)
    {
        xPos = initXPos;
        yPos = initYPos + initSize;
        size = initSize;
        active = false;
    
        xOffset = 0;
        xOffsetMax = int(random(3,11));
        
        // Randomly true or false
        movingRight = (int(random(0,1)) == 1);
    
        // 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 draw()
    {
        fill(196,196,255,128);
        stroke(0,0,0,128);
        ellipse(xPos + xOffset, yPos, size, size);
    }
  
    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;
            }
        }
    }
  
    public void activate()
    {
        // If this bubble is not currently active.
        if (!active)
        {
            active = true;
    
            xPos = int(random(-size, width + size));
            yPos = height + size;
        }
    }
}

[See repo JSON]