8.3 Spacewar! Events

Obviously our Spacewar! game handles events. Firstly we want to control the ships with the keyboard. Secondly, we want the game to pause when the mouse cursor moves out of the game play, and resume when it enters again.

In our design, a unique morph, SpaceWar instance, models the game play. Therefore we want this instance to handle the events described above.

8.3.1 Mouse event

Mouse cursor enters game play

We want to catch events when the mouse cursor moves over our SpaceWar morph.

 CuisLogo Which method should return true to let the game play be notified with a dedicated messages that the mouse cursor enters or leaves? In which class should we implement this method?

Exercise 8.1: Get notified of mouse move-over event

Once we make explicit we want the game play to receive mouse move-over events, we need to set the behavior accordingly with dedicated methods.

Each time the mouse cursor enters the game play, we want to:

 CuisLogo Which message is sent to the game play to be notified the mouse cursor enters the game play area? How should the matching method be written?

Exercise 8.2: Handle mouse enter event

Mouse cursor leaves game play

We also want to be informed when the mouse cursor leaves our SpaceWar morph. Thanks to the work done in Exercise 8.1, we already informed Cuis-Smalltalk we want to be notified of mouse movement over the game play. However we need to code the behavior when the mouse cursor leaves the game play:

 CuisLogo Which message is sent to the game play to be notified the mouse cursor leaves the game play area? How should we write the overridden method?

Exercise 8.3: Handle mouse leave event

In graphic user interface, a visual effect is often used to inform the user the keyboard focus changed. In Spacewar! we change the game play background depending on the sate of the keyboard focus.

In Figure 8.1, at the left keyboard focus is on the game; at the right keyboard focus is not on the game, it is paused and we can see underneath.

ch08-Focus

Figure 8.1: Spacewar! effect depending on the keyboard focus

In the Morph framework, the #keyboardFocusChange: message is sent to the morph losing or gaining the keyboard focus, its parameter is a Boolean. Therefore we implement the Figure 8.1 behavior in the matching SpaceWar’s method keyboardFocusChange:

SpaceWar>>keyboardFocusChange: gotFocus
   gotFocus 
      ifTrue: [color := self defaultColor]
      ifFalse: [color := self defaultColor alpha: 0.5].
   self redrawNeeded

Example 8.1: Spacewar! keyboard focus effect

8.3.2 Keyboard event

To control the space ships, we use the keyboard. Therefore we want the game play to be notified of keyboard events.

 CuisLogo Find out which method should return true to let the game be notified of keyboard event.

Exercise 8.4: Get notified of keyboard event

We can decide to be notified of the key down or key up event and also key down then up event (key stroke). As long as our SpaceWar morph responds true to the #handlesKeyboard message, it receives the messages #keyUp:, #keyDown: and #keyStroke:. By default, the matching methods in the Morph class do nothing.

The argument of these messages is a KeyboardEvent object to which, among other things, you can ask the #keyCharacter of the pressed key or test about some special keys as the keyboard arrows. The first player ship – the green one – is controlled with the keyboard arrows when there are stroked:

SpaceWar>>keyStroke: event
   event isArrowUp ifTrue: [^ ships first push].
   event isArrowRight ifTrue: [^ ships first right].
   event isArrowLeft ifTrue: [^ ships first left].
   event isArrowDown ifTrue: [^ ships first fireTorpedo].
   ...

Example 8.2: Keystroke to control the first player ship

To control the second player ship, we use another classic arrangement in QWERTY keyboard controlled game: WASD26.

 CuisLogo Append the additional code to Example 8.2 to control the second player ship with the keys WASD. As a reminder, in Smalltalk the character code for q can be written as $q.

Exercise 8.5: Keys to control the second player ship


Footnotes

(26)

https://en.wikipedia.org/wiki/Arrow_keys#WASD_keys