RainbowBubble.pde 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2021 UltrasonicMadness
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. // Array of 256 bubbles
  17. Bubble[] bubbles = new Bubble[256];
  18. // The bubbles can be turned off.
  19. boolean bubblesOn = true;
  20. // Which stage the rainbow is at currently (0-1535, i.e. 256 * 6)
  21. int rainbowFrame = 0;
  22. // If true, hold on a steady color
  23. boolean rainbowPaused = false;
  24. // If true, generate rainbow-colored bubbles
  25. boolean bubbleTeaMode = false;
  26. void setup()
  27. {
  28. size(800,600);
  29. noCursor();
  30. /*
  31. * Initialize the bubbles with a random x position, a y position
  32. * below the screen and a radius between 15 and 25 pixels.
  33. */
  34. for (int bubbleCounter = 0; bubbleCounter < bubbles.length; bubbleCounter++)
  35. {
  36. bubbles[bubbleCounter] = new Bubble(int(random(0, width)), height, int(random(15,25)), bubbleTeaMode);
  37. }
  38. }
  39. void draw()
  40. {
  41. if (!rainbowPaused)
  42. {
  43. if (rainbowFrame >= 1535)
  44. {
  45. rainbowFrame = 0;
  46. }
  47. else
  48. {
  49. rainbowFrame++;
  50. }
  51. }
  52. genBackground();
  53. genRainbow(196);
  54. // Update each bubble
  55. for (int bubbleCounter = 0; bubbleCounter < bubbles.length; bubbleCounter++)
  56. {
  57. // Small chance of activating the bubble if the bubbles are on.
  58. if (int(random(0,64)) == 3 && bubblesOn)
  59. {
  60. bubbles[bubbleCounter].activate(bubbleTeaMode);
  61. }
  62. bubbles[bubbleCounter].advance();
  63. bubbles[bubbleCounter].draw();
  64. }
  65. }
  66. void genBackground()
  67. {
  68. fill(255);
  69. noStroke();
  70. rect(0,0,width,height);
  71. }
  72. void genRainbow(int alpha)
  73. {
  74. int counter = rainbowFrame % 256;
  75. int colorTransitionId = (rainbowFrame / 256) % 6;
  76. noStroke();
  77. switch (colorTransitionId)
  78. {
  79. case 0: // red to yellow, red at 255, green ascending, blue at 0
  80. fill(255, counter, 0, alpha);
  81. break;
  82. case 1: // yellow to green, red descending, green at 255, blue at 0
  83. fill(255 - counter, 255, 0, alpha);
  84. break;
  85. case 2: // green to cyan, red at 0, green at 255, blue ascending
  86. fill(0, 255, counter, alpha);
  87. break;
  88. case 3: // cyan to blue, red at 0, green descending, blue at 255
  89. fill(0, 255 - counter, 255, alpha);
  90. break;
  91. case 4: // blue to pink, red ascending, green at 0, blue at 255
  92. fill(counter, 0, 255, alpha);
  93. break;
  94. case 5: // pink to red, red at 255, green at 0, blue descending
  95. fill(255, 0, 255 - counter, alpha);
  96. break;
  97. }
  98. rect(0,0,width,height);
  99. }
  100. void keyPressed()
  101. {
  102. switch (key)
  103. {
  104. case ' ':
  105. bubblesOn = !bubblesOn;
  106. break;
  107. case 'P':
  108. case 'p':
  109. rainbowPaused = !rainbowPaused;
  110. break;
  111. case 'T':
  112. case 't':
  113. bubbleTeaMode = !bubbleTeaMode;
  114. break;
  115. case '1':
  116. rainbowFrame = 0;
  117. break;
  118. case '2':
  119. rainbowFrame = 256;
  120. break;
  121. case '3':
  122. rainbowFrame = 512;
  123. break;
  124. case '4':
  125. rainbowFrame = 768;
  126. break;
  127. case '5':
  128. rainbowFrame = 1024;
  129. break;
  130. case '6':
  131. rainbowFrame = 1280;
  132. break;
  133. }
  134. }