Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. Spot[] spots; // Declare array
  2. void setup() {
  3. size(700, 100);
  4. int numSpots = 70; // Number of objects
  5. int dia = width/numSpots; // Calculate diameter
  6. spots = new Spot[numSpots]; // Create array
  7. for (int i = 0; i < spots.length; i++) {
  8. float x = dia/2 + i*dia;
  9. float rate = random(0.1, 2.0);
  10. // Create each object
  11. spots[i] = new Spot(x, 50, dia, rate);
  12. }
  13. noStroke();
  14. }
  15. void draw() {
  16. fill(0, 12);
  17. rect(0, 0, width, height);
  18. fill(255);
  19. for (int i=0; i < spots.length; i++) {
  20. spots[i].move(); // Move each object
  21. spots[i].display(); // Display each object
  22. }
  23. }
  24. class Spot {
  25. float x, y; // X-coordinate, y-coordinate
  26. float diameter; // Diameter of the circle
  27. float speed; // Distance moved each frame
  28. int direction = 1; // Direction of motion (1 is down, -1 is up)
  29.  
  30. // Constructor
  31. Spot(float xpos, float ypos, float dia, float sp) {
  32. x = xpos;
  33. y = ypos;
  34. diameter = dia;
  35. speed = sp;
  36. }
  37.  
  38. void move() {
  39. y += (speed * direction);
  40. if ((y > (height - diameter/2)) || (y < diameter/2)) {
  41. direction *= -1;
  42. }
  43. }
  44.  
  45. void display() {
  46. ellipse(x, y, diameter, diameter);
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement