Pen block idea: Explore functions and draw stars
Pen blocks are the perfect way to explore the idea of creating your own functions. If you've written code that draws a square or star (or anything else cool!)—you can create a function that organizes that code into a little reusable package.
First, you create a new function by going to Functions > Create Function. Name it “Draw Star” or whatever shape you’ve chosen. Then define what you want your function to do, using code blocks:
Then you can call your new Draw Star function as many times as you like, and the code will run—call your function only once or call it 50 times! Here’s some examples you can try:
You can create a second function that fills the Stage with the color black. I called it Draw Space.
You can even call functions from within another function! Really?! Let’s try it.
Create another function called Draw Starfield, and let’s call both Draw Stars and Draw Space from within it. First, we fill the screen with black using the Draw Space function, then switch to a white pen, then call the Draw Star function at 200 random locations!
Don’t forget to add a short program that calls your new Draw Starfield function to see it in action. Change it around and make it your own!