Learn creative coding writing simple programs

58. Travel through space, use an array to move stars

This time we are building a moving star field. The stars are just points drawn with point(). Each star has three properties: x, y and speed. Since we are drawing 100 stars, we use three arrays to hold all the information, otherwise the program would be hundreds of lines long.
Each array hold 100 values. That means we have 100 "x" values, 100 "y" values and 100 "speed" values.
x[0], y[0] and speed[0] are the properties of the first star. x[1], y[1] and speed[1] are the properties of the second star, and so on.
Each time draw() is called we substract a few pixels from each x. How many pixels? It depends on the speed that was randomly assigned to each star when setup() was called when the program started. So maybe we move the first star 3 pixels to the left, and the second star 1 pixel to the left. The amount of pixels is stored in the speed[] array, so each star has a constant speed.

Tags: stars, animation, field, star, moving, motion, array

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```