Smalltalk Programming
Lesson 8

In the last lesson you created your Shooter Game screen. Your game would not be much fun without a ship to move and shoot with. Today you will begin coding your ship.

1. We will start by creating the class Ship. Remember from your last lesson how you can create a class. First, open a System Browser. You can do this from the menu at the top and selecting Tools→Browser. Or, you can left click on the background and select Browser.

2. In your browser, left select on your ShooterGame class twice. This should bring up the code that you used to create the ShooterGame class. If you have your ShooterGame class selected, you can also press the “instance” button which will take you back to its class code. You do not have to create your Ship class this way, but it helps you to be lazy by not having to retype “ShooterGame” – and programmers are all about being lazy!

3. In the bottom pane, make the changes below that are in yellow. When you are done save your changes by pressing Ctrl-s. You can also right click your mouse in the code pane and select “accept (s)”.

RectangleMorph subclass: #Ship
    
instanceVariableNames: ''
    
classVariableNames: ''
    
poolDictionaries: ''
    
category: 'ShooterGame'

5. When you saved your code you created a new class called Ship. Ship is a subclass of RectangleMorph (which is also a subclass of Morph). This means that Ship inherits the things that RectangleMorph can do (and also Morph). The category remains ShooterGame.

6. Now add the initialization method for Ship. This is a method that is used when a new instance of the Ship class is created. To do this, left click on “-- all --”.

7. In the bottom pane, type the following code:

initialize

    
super initialize.
    
self color: Color silver.
    
self borderColor: Color lightOrange.
    
self extent: 32 @ 32

8. After you are done save your changes.

9. Look at each line of code. Can you figure out what each line of code is doing?

10. What do you notice about the ship and your shooter game? What else needs to be done?

11. Save and Quit your Smalltalk image.

Download the PDF version.