Smalltalk for Game Development

2b81e34e6f1c272752161742f44790ed?s=47 Levi D. Smith
September 08, 2019

Smalltalk for Game Development

Presentation for the September 2019 Knoxville Game Design meeting.
https://www.youtube.com/watch?v=3qtqZ9z_5Cw

This month’s topic is Creating Games in Smalltalk. We will look at how it is possible to create games in the Smalltalk language using the Squeak environment. Smalltalk is one of the first object oriented languages, where everything is an object.

2b81e34e6f1c272752161742f44790ed?s=128

Levi D. Smith

September 08, 2019
Tweet

Transcript

  1. Smalltalk for Game Development Knoxville Game Design September 2019 Levi

    D. Smith
  2. Smalltalk History / Overview • Smalltalk-80 • First version was

    in 1972, but public version was released in 1980 • Alan Kay at Xerox PARC • Based on Simula • “Everything is an object” • No destroy • garbage collection – objects are free’d when nothing points to them • delete sets the object’s owner to nil • Similar to Objective-C
  3. Squeak • Smalltalk implementation • squeak.org • Windows, Mac, Linux

    • First released in 1996 • Original Scratch implementation in Squeak / Smalltalk
  4. Example Games • Left click > objects > Games •

    Drag game from panel to Squeak desktop Code Browser > Etoys-Squeakland-Morphic-Games Also “Just for Fun”
  5. Stuff to Play With • Projects > New Project >

    New Morphic Project • World > Flaps > Show Shared Flaps Etoys http://www.squeakland.org/tutorials/guides/
  6. Smalltalk “Hello World” In Transcript window • Alt + a

    (select all) • Alt + d (do it) Code format <class> <message>: <parameters>.
  7. API (Application Programming Interface) Browser • Built In - “Browser”

    in menu • First column – categories • Not significant for coding, but organizes your classes • Second column – classes • Third column – message categories • Fourth column - messages Right click class and select browse hierarchy to see superclass messages
  8. Making New Classes • Replace the following line with your

    own class name • Object subclass: #NameOfSubclass • Can replace Object with some other class name to make subclass of another class • Apply change with Alt + s
  9. Adding Messages (procedures / functions / methods) • Click class

    name • Add code below (replace default code) • Save with Alt + s (accept) • To return a value, use ^ • initialize is a special message that is called when the object is created (constructor)
  10. Executing new messages • Variables must be declared between pipes

    • Use “new” keyword to create object • Use := for assignment • Usually written as ← in books • Remember • class messages can be called using the class name • Instance messages can only be called from an object instance of the class (created with “new”)
  11. Saving • Saving the workspace will save your changes locally

    • Export your code changes with fileOut to save to a text file • Saves a file in the Squeak folder with the category/class name with .st extension • Why export your code with fileOut? • Send to other Squeak users • Export to source code control • Disaster recovery
  12. Morphic • Used to create Squeak “World” (GUI) objects •

    Similar to Java AWT/Swing, GTK, etc • Middle click to display “ring” icons • Morph properties • position • extent (aka size) • color • name
  13. Adding a Ship • RectangleMorph – a simple GUI rectangle

    • Custom colors can be defined with Color r: <red> g: <green> b: <blue> • When creating a subclass, you must explicitly call “super initialize.” in subclass initialize • Errors will probably display if you override initialize without calling super initialize. • Position is not relative to parent • Everything is in Workspace coordinates • It is possible to position a child outside of its parent!
  14. Handing Keyboard Events • See KeyPressMorph for example code •

    Use the following messages: handleListenEvent, handleMouseEvent, handleKeyboardEvent, intoWorld, outOfWorld, registerToEvents, unregisterToEvents • Implement handleKeyboardEvent Pressed key is in anEvent keyString asLowercase.
  15. Moving the Player • Get square moving around inside parent

    space • Must do bounds checking with the parent • Add children using self addMorph: <object>. • Child is allowed to move outside the parent bounds • Each Morph has left, right, top, and bottom properites • Must use self position setX: <x> setY: <y> to set position • Can use self width: <width> and self height: <height> to set width and height
  16. Game Loop(s) • Implement step and stepTime • step –

    update code • stepTime – set return value to milliseconds to delay (33 for 30 FPS) • Add shoot dealy to ship
  17. Enemies • Can use intersect method against enemy’s bounds to

    determine collision • Use same basic concepts from other space shooter examples • Make list of enemies • listEnemies := Array new: 3. • Add each enemy to the array
  18. Real Time • Use stepTime to return the frequency (in

    milliseconds) that code is executed • Put “update” code in the step message • step may be called before initialize, so check if your variables are set • Performing an arithmetic operation of an unset (nil) variable will likely cause errors • Call owner changed on parent Morph to ensure that background is redrawn
  19. References • Squeak documentation • https://squeak.org/documentation/ • Coding guide •

    https://squeak.org/documentation/terse_guide/ • Morphic for beginners • https://wiki.squeak.org/squeak/1870 • Introduction to Morphic • http://stephane.ducasse.free.fr/FreeBooks/CollectiveNBlueBook/morphic.final.pdf • Squeak by Example • https://hal.inria.fr/inria-00441576/file/SBE.pdf