Slide 1

Slide 1 text

Smalltalk for Game Development Knoxville Game Design September 2019 Levi D. Smith

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

Squeak • Smalltalk implementation • squeak.org • Windows, Mac, Linux • First released in 1996 • Original Scratch implementation in Squeak / Smalltalk

Slide 4

Slide 4 text

Example Games • Left click > objects > Games • Drag game from panel to Squeak desktop Code Browser > Etoys-Squeakland-Morphic-Games Also “Just for Fun”

Slide 5

Slide 5 text

Stuff to Play With • Projects > New Project > New Morphic Project • World > Flaps > Show Shared Flaps Etoys http://www.squeakland.org/tutorials/guides/

Slide 6

Slide 6 text

Smalltalk “Hello World” In Transcript window • Alt + a (select all) • Alt + d (do it) Code format : .

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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)

Slide 10

Slide 10 text

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”)

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

Adding a Ship • RectangleMorph – a simple GUI rectangle • Custom colors can be defined with Color r: g: b: • 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!

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

Moving the Player • Get square moving around inside parent space • Must do bounds checking with the parent • Add children using self addMorph: . • Child is allowed to move outside the parent bounds • Each Morph has left, right, top, and bottom properites • Must use self position setX: setY: to set position • Can use self width: and self height: to set width and height

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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