Smalltalk Programming
Twinkling Stars

If you find that your stars have become boring, you can make them fancy by adding the code changes from this lesson. This code will turn your regular stars into stars that twinkle.

1. Type and save the code below.

Star>>twinkle

    self color: (Color white alpha: (#(0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1) atRandom))

ShooterGame>>stars

    ^ self submorphs select: [:morph | morph isMemberOf: Star]

ShooterGame>>twinkleStars

    | starrySky totalStars amountToTwinkle |
    
starrySky := self stars.
    
totalStars := starrySky size.
    
amountToTwinkle := ((1 to: totalStars) size / 8) asNonFraction rounded.
    
amountToTwinkle timesRepeat: [(starrySky at: totalStars atRandom) twinkle]

ShooterGame>>step

    self twinkleStars.
    
    
self enemies isEmpty
        
ifTrue: [self initializeEnemies]

2. What a great addition to your game! You have made your game look even more fancy with stars that twinkle. What else might be nice to have for your shooter game?

3. Save and Quit your Smalltalk image.

Download the PDF version.