Home

Video: Arrays 1

Notes for this video are found below.

Code for the demo

int how_many = 200;
int[] xs;
int[] ys;

void setup()
{
  size(500, 500);
  xs = new int[how_many];
  ys = new int[how_many];
  
  // I want a loop that specifies i goes from 0 -> how_many - 1
  for(int i = 0; i < how_many; i++)
  {
    xs[i] = int(random(0, 500));
    ys[i] = int(random(-400, 200));
  }
  
}

void draw()
{
  background(0);
  fill(255);
  
  for(int i = 0; i < how_many; i++)
  {
    ellipse(xs[i], ys[i], 20, 20);
  }

  for(int i = 0; i < how_many; i++)
  {
    ys[i]++;
    
    if(ys[i] > 500)
    {
      // fell past the bottom; reset at top, random x value
      ys[i] = 0;
      xs[i] = int(random(0, 500));
    }
  }
}
CSE 1211 material by Joshua Eckroth is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License. Source code for this website available at GitHub.