2.6 Spacewar! models

2.6.1 First classes

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:

  1. the game play ⇒ SpaceWar class,
  2. a central star ⇒ CentralStar class,
  3. two space ships ⇒ SpaceShip class,
  4. torpedoes ⇒ 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.

ch02-spacewarClassCategory

Figure 2.2: Spacewar! class category

2.6.2 Spacewar! package

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.

  1. Open the Installed Packages tool ...World menu → Open...Installed Packages...
  2. On the Installed Packages window, do ...click new button → Input Spacewar!Return...
  3. Do ...select Spacewar! package → save button...
ch02-InstalledPackages

Figure 2.3: Installed Package window

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:

  1. Do ...World menu → Open...File List...
  2. Search for the file Spacewar!.pck.st and click the 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 enviroment 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.

2.6.3 The Newtonian model

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.

motionEquations

Figure 2.4: Equations of the accelerations, speed and position