## STARFIELD

SIMPLE STARFIELD

STAR / MAIN

Making a Star Class

First we'll need to create a Star class.

class Star

{

float x;

float y;

int size;

Star()

{
x = random(width);

y = random(height);

size = 2;

}

void update()

{
y++;

}

void render()

{

fill(255);

ellipse(x, y, size, size);

}

}

Drawing the Stars

In the Main Tab, we'll need to actually create the stars. Look at how we make an ArrayList of objects, and mimic that syntax. Here's a checklist of places we'll need to add code:

• At the top of the program

• Declare an ArrayList of Stars

• In setup()

• Initialize the ArrayList of Stars

• Write a loop that executes 500 times.

• Inside this loop, add a new star to the list of stars.

• In render()

• Loop through all the stars

• Inside this loop, call render() on each star

MOVING STARFIELD

STAR / MAIN

The Update Method

Add a method called update() to the Star class.

class Star

{

void update()

{
y++;

if(y > height)

{

y = y - height;

}

}

}

Calling Update

Back in main, we'll need to write code to call this method. Go to the act() method in the main tab. Write a loop that calls your update() method on each Star.

Notice that we didn't just set y to zero. Consider what would happen if our stars moved faster than one per frame.

For example, if stars moved by 5 pixels per frame, all stars between height and height - 5 would all be moved into a single "band" at zero.

By subtracting height instead, we avoid this problem.

DEPTH OF FIELD EFFECT

STAR

Different Sizes and Motion

Not all stars are the same distance away. Let's represent that by their size and motion.

• A "nearby" star will have a larger size

• A "nearby" star will move faster relative to our viewpoint

To do so, first we'll need to set size to a random value in the constructor:

size = (int) random(1, 4);

Next, instead of increase y by 1 every frame, we'll increase it by a value related to size.

y = y + size * 0.5;

Brightness

Finally, we may want to make a nearby star appear bright and a far away star appear dim. Let's change one more line of code in render():

fill(size * 60);

Make sure your stars are moving but subtle. If they are too big, too bright, or too fast you run two risks:

• The player may think they're bullets

• It's distracting / a strain on the eyes