Now that you have your game screen and a ship that can move and shoot, you are now ready for an enemy. Today, you will make the necessary code changes to create an enemy in your game.
1. Type the code below and save the changes in your System Browser to create your Enemy class.
RectangleMorph subclass: #Enemy
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'ShooterGame'
2. Now, for your Enemy class, enter and save the following code for your initialization method.
initialize
super initialize.
self color: Color green.
self borderColor: Color honeydew.
self extent: 32 @ 32
3. Make the following changes to ShooterGame>>initialize.
initialize
super initialize.
self position: 100 @ 100.
self extent: 640 @ 480.
self color: Color black.
self setNameTo: 'Shooter Game'.
self initializeShip.
self initializeEnemies
4. Now add the code and save the changes for ShooterGame>>initializeEnemies.
initializeEnemies
| enemy |
enemy := Enemy new.
enemy position: self position + (100 @ 100).
self addMorph: enemy
5. Even though your shooter game is still running, you can still create an enemy without having to start a new game. Once again, “explore morph” is your friend. Note that you can also use “inspect morph”, which works very similar, but it can seem a little less easy to use as explore morph.
6. If your explore morph window is still open, select ShooterGame (which will be the root). If your window is not open, open a new explore morph window on your running shooter game. Your explorer window should look similar to Figure 1 below.
7. Type the following expression in the explorer workspace window, then select “do it”. If your window is not open, open a new explore morph window on your running shooter game and then follow the directions.
self initializeEnemies.
8. What do you notice? What else needs to be done?
9. Save and Quit your Smalltalk image.