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
Below you can ask and answer questions related to this video. If your question is unrelated to the video, please post it at the Processing Forum as I'm unable to help with all of your programs and still have enough free time to eat and sleep :)
comments powered by Disqus