In the last chapter, we listed the protagonists of the game. Now, we propose a first implementation of the game model with a set of classes representing the involved entities:
SpaceWar
class,
CentralStar
class,
SpaceShip
class,
Torpedo
class.
Before defining these classes in Cuis-Smalltalk, we want to create a dedicated class category to group them there.
In any kind of Cuis-Smalltalk window, pressing right-click on a pane will typically bring up a menu of operations you can apply which are specific to that pane.
With the mouse pointer over the class category pane of the Browser – the left-most one – just do:
...right mouse click → add item
... then
key in Spacewar!
Once our new category is created, the Browser proposes a code template in
the method source code pane – the bottom one – to create a new class in
the Spacewar!
category:
Object subclass: #NameOfSubclass instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Spacewar!'
We replace the symbol #NameOfSubclass
with a symbol
representing the name of the class we want to create. Let’s start with
#SpaceWar
. To save our class, while over the class
declaration code do ...right mouse click →
Accept
... Cuis-Smalltalk will ask for your initials and name
if it hasn’t before. Alternatively, you can just do Ctrl-s (Save).
Then simply repeat the process for each of #SpaceShip
,
#CentralStar
and #Torpedo
. If necessary, to
get another class code template, click the class category
Spacewar!
.
When done, your class category should be filled with four classes as in Figure 2.2.
Another important use case of a class category is to define a package
to save code to a file. A package saves the code of the classes held in
a given class category and a bit more, but more on that last point
later. To create our Spacewar!
package and save our game code
we use the Installed Packages tool.
Open...
→ Installed
Packages
...
new
button → Input Spacewar! → Return...
Spacewar!
package →
save
button...
A file Spacewar!.pck.st is created alongside the Cuis-Smalltalk image file. To install a package in a fresh Cuis-Smalltalk environment, use the File List tool:
Open...
→ File List
...
install package
button
You can also drag and drop the package file from your operating system
over to the Squeak window. Upon dropping the file over the window
Cuis-Smalltalk will ask you what you want to do with this package. To
install it in your environment you can simply press Install
package
.
Or, you can open a Workspace, type in Feature require: 'Spacewar!'
and Ctrl-d Do it.
Now, we have created and saved the package. Whenever you start with a fresh Cuis-Smalltalk environment, you can load the game package.
The classes we defined are empty shells with neither state nor behavior. These will be filled in and refactored in the following chapters.
For an enjoyable game experience, the player ships must follow Newton’s laws of motion. Acceleration, speed and position are computed according to these laws. The ships are subjected to two forces: the acceleration from the gravity pull of the central star and an inner acceleration coming from the ship engines.
Later, we will learn how these equations are easily converted to computer calculations.