在许多语言中,您可以执行以下操作:
while true:
handle events like keyboard input
update game world
draw screen
(optional: delay execution)
尽管这远非最佳,但对于简单的游戏来说已经足够了。
您如何在Squeak Smalltalk中做到这一点?
我可以阅读键盘输入并对它作出 react ,如wiki.squeak.org所述。但是如果我尝试执行类似
1 to: 10 do: [ :i | game updateAndDraw ]
所有事件仅在执行完循环后才被处理。
最佳答案
Morphic已经提供了该主循环。在MorphicProject class>>spawnNewProcess
中:
uiProcess := [
[ world doOneCycle. Processor yield ] repeat.
] newProcess ...
如果您深入研究
doOneCycle
,就会发现它interCyclePause:
)step
方法您的代码应通过添加鼠标/键盘事件处理程序,动画的步进方法以及重新显示的draw方法来进入这些阶段。所有这些都应该是您自己的游戏变形中的方法。您可以在整个系统中找到示例。