简体   繁体   English

Squeak Smalltalk:游戏循环

[英]Squeak Smalltalk: Game loop

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 it should suffice for simple games. 虽然这远非最佳,但它应该足够简单的游戏。

How do you do this in Squeak Smalltalk? 你是怎么在Squeak Smalltalk做到这一点的?

I can read keyboard input and react to it as described on wiki.squeak.org . 我可以阅读键盘输入并对其做出反应,如wiki.squeak.org所述 But if I try to execute something like 但是,如果我尝试执行类似的东西

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

all the events are only ever handled after the loop has executed. 所有事件只在循环执行后才被处理。

Morphic already provides that main loop. Morphic已经提供了主循环。 It's in MorphicProject class>>spawnNewProcess : 它在MorphicProject class>>spawnNewProcess

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

And if you dig into doOneCycle you will find it 如果你深入了解doOneCycle你会发现它

  • (optionally) does a delay ( interCyclePause: ) (可选)做延迟( interCyclePause:
  • checks for screen resize 检查屏幕大小调整
  • processes events 处理事件
  • processes step methods 处理step方法
  • re-displays the world 重新展示世界

Your code should hook into these phases by adding mouse/keyboard event handlers, step methods for animation, and draw methods for redisplaying. 您的代码应该通过添加鼠标/键盘事件处理程序,动画步骤方法和绘制重新显示方法来挂钩这些阶段。 All of these should be methods in your own game morph. 所有这些应该是你自己的游戏变形中的方法。 You can find examples throughout the system. 您可以在整个系统中找到示例。

To perform an action a fixed number of times: 要执行固定次数的操作:

10 timesRepeat: [game updateAndDraw]

To use while semantics: while语义上使用:

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

To create a perpetual loop using while semantics, 使用语义创建永久循环,

[true] whileTrue: [something do]

您应该能够通过使用Object >> #when:send:to: message来利用Morphic事件循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM