Smalltalk Programming
Starry Background

Below is code that you can use to create a starry background in your game.

1. Type and save the code below. Remember not to type the class name and the >> (such as Star>>initialize – you will only type initialize). This is the Smalltalk way of identifying the method of a class.

EllipseMorph subclass: #Star
    
instanceVariableNames: ''
    
classVariableNames: ''
    
poolDictionaries: ''
    
category: 'ShooterGame'

Star>>initialize

    super initialize.
    
self color: Color white.
    
self extent: 4 @ 4

ShooterGame>>initializeStars

    100 timesRepeat: [
        
| star origin anExtent |
        
star := Star new.
        
origin := (self bounds) origin.
        
anExtent := (self extent - star bounds extent) rounded.
        
star position: (origin x + (anExtent x atRandom)) @ (origin y + (anExtent y atRandom)).
        
self addMorphBack: star]

ShooterGame>>initialize

    super initialize.
    
self position: 100 @ 100.
    
self extent: 640 @ 480.
    
self color: Color black.
    
self setNameTo: 'Shooter Game'.
    
self initializeStars.
    
self initializeShip.
    
self initializeScore.
    
self initializeEnemies

2. What a great addition to your game! What else might be nice to have for your shooter game?

3. Save and Quit your Smalltalk image.

Download the PDF version.