Squeak Smalltalk: Game Cycle

In many languages, you can do something like the following:

while true: handle events like keyboard input update game world draw screen (optional: delay execution) 

while this is far from optimal, this is enough for simple games.

How do you do it in Squeak Smalltalk?

I can read keyboard input and respond to it as described in wiki.squeak.org . But if I try to do something like

 1 to: 10 do: [ :i | game updateAndDraw ] 

all events are processed only after the cycle is completed.

+5
source share
3 answers

Morphic already provides this main loop. It is in the MorphicProject class>>spawnNewProcess :

 uiProcess := [ [ world doOneCycle. Processor yield ] repeat. ] newProcess ... 

And if you dig in doOneCycle , you will find it

  • (optional) makes a delay ( interCyclePause:
  • checks screen size
  • handles events
  • step processes
  • displays the world

Your code should connect to these steps by adding mouse / keyboard event handlers, step methods for animation, and drawing methods for re-rendering. All this should be methods in your own game. You can find examples throughout the system.

+5
source

To perform an action a fixed number of times:

 10 timesRepeat: [game updateAndDraw] 

Use while semantics:

 i := 5 [i > 0] whileTrue: [ i printNl. i := i - 1. ] 

To create an eternal cycle using semantics,

 [true] whileTrue: [something do] 
0
source

You should be able to use the Morphic event loop using the message Object >> #when:send:to:

0
source

All Articles