Advertisement
MrsMcLead

Snowflakes

Feb 10th, 2016
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.75 KB | None | 0 0
  1. /** Your header comments go here to describe the class */
  2. class Snowflake {
  3.  
  4.   // the object's x-coordinate
  5.   int x;
  6.  
  7.   // the object's y-coordinate
  8.  
  9.  
  10.   // a parameter for tracking the ellapsed lifetime of the object, which can be used for animating it
  11.   int t;
  12.  
  13.   /** Constructor
  14.    * @param xCoordinate the initial x coordinate of the object
  15.    * @param yCoordinate the initial y coordinate of the object
  16.    */
  17.   Snowflake (int xCoordinate) {
  18.    x = xCoordinate;
  19.    t = 0;
  20.   }
  21.  
  22.   /** Draw the object.  
  23.    *  This function should be called every iteration of draw() to display the object.
  24.    */
  25.   void draw() {
  26.  
  27.     // drawing code for your object, with an animation based on t
  28.     //background(#EB74AC);
  29.     fill(#A0FFC5);
  30.     ellipse(x,t/2,5,5);
  31.     step();
  32.   }
  33.  
  34.   /** Advance the object's animation.  
  35.    *  This function should be called every iteration of draw() to advance it's animation.
  36.    */
  37.   void step() {
  38.     // increment the time counter
  39.     t++;  
  40.    
  41.   }
  42. }
  43.  
  44.  
  45. // The maximum number of objects that can be displayed at once
  46. int MAX_NUM_OBJECTS = 100;
  47.  
  48. // An array of all the created objects
  49. Snowflake[] myObjects = new Snowflake[MAX_NUM_OBJECTS];
  50.  
  51. // An index into the myObjects array to show where to save the next new object.  
  52. // Note that this index cannot ever be allowed to exceed MAX_NUM_OBJECTS;
  53. // instead it should cycle from 0 to MAX_NUM_OBJECTS and then be reset to begin again at 0.
  54.  
  55. int idxMyObjects = 0;
  56.  
  57. /** Processing's setup function */
  58. void setup() {
  59.   size(800,600);
  60.  
  61. }
  62.  
  63. /** The main drawing loop for Processing */
  64. void draw() {
  65.  
  66.   background(#A0FFC5);  // NOTE:  you are welcome to change this background
  67.  
  68.   // use a for loop to display all of the objects and advance their animations
  69.   // NOTE:  When writing this loop, you will need to test each element
  70.   //        of the myObjects array to see if it is an object or is empty.  
  71.   //        Use the following code to test for an object:
  72.   //        if (myObjects[i] != null) { ... }
  73.   //        This example uses i as the loop variable; your implementation may
  74.   //        use something different.
  75. for(int i = 0; i < MAX_NUM_OBJECTS; i++)
  76. {
  77.   if(myObjects[i] != null)
  78.   {
  79.    myObjects[i].draw();
  80.   }
  81.   }
  82. }
  83.  
  84. void mousePressed() {
  85.   // create a new object based on the location clicked
  86.   // and store it into myObjects[idxMyObjects]
  87.   myObjects[idxMyObjects] = new Snowflake(mouseX);
  88.  
  89.  
  90.   // increment the index into myObjects, keeping it in the range 0...MAX_NUM_OBJECTS
  91.   // NOTE:  Make sure you understand how this line of code works!  
  92.   // It is complete as-is.  You don't need to do anything else
  93.   // to make the idxMyObjects counter work correctly.
  94.  
  95.   idxMyObjects = (idxMyObjects+1) % MAX_NUM_OBJECTS;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement