How to create randomly generated, but static background elements within a draw loop

I wanted to draw a night scene with several 100 randomly drawn stars (points). I can create the loop that draws the stars, but when I animated a drawing (a car), the process of everything in the draw loop repeating itself constantly redraws the randomly located stars when I want them to stay in the same place. If I draw them outside the draw loop, the background hides them when it runs inside the draw loop.

Any ideas on how to overcome this?

Mike

Mike, can you post your code so I can help debug the problem?

Here’s a link to my project …

I have created a partial work around by drawing a blue rectangle where my car passes on the screen, but that still doesn’t work well and I know it’s not the right solution. The only real solution I can think of is somehow creating a .jpg or .png with the stars already on it, but that’s not the solution I want either.

thanks for looking at it.

Mike

@mwood
Very creative project.
I made a small change to your code. Take a look. Draw the background inside the draw loop with the stars.

background("darkblue");

Link: https://studio.code.org/projects/gamelab/P3P-wtms3O0j-UV4GD14g4OoQ-INCJgB0M6RJHRfc7g

Thanks for looking at it … I had hoped to draw the stars once and then leave them in their originally drawn position … that’s where the problem is because when it draws the background again, I have to draw the stars again and it draws them in a different random position so it looks more like a snowstorm … maybe I need an array that stores the original position of the stars, but I haven’t learned how to do that yet in GameLab if it’s possible. I’ll keep working on it. :slight_smile:

@mwood an array would be one solution, but the other would be to use sprites.

Array example https://studio.code.org/projects/gamelab/tjfDaHK0vOH6UWnCBMqWCA/view
Sprites example https://studio.code.org/projects/gamelab/NOccI7-JQdvJcTdMA3Yilg/view

I left the random stroke width in the draw loop for the array example to give the stars a twinkle, but you could place those values in an array as well.

Thanks Josh. I like the twinkling affect and I appreciate you showing me two ways to accomplish this. I knew it could be done.

Mike