正在学习一个go语言写的仿吃豆人的小程序,如下:
https://github.com/gophergala/golab
在其ctrl/engine.go文件中,遇到一点疑问
```go
func simulate() {
t := time.Now().UnixNano()
for {
// Check if we have to start a new game
select {
case <-model.NewGameCh:
initNew()
default:
}
// First erase target images. We have to do this before handling mouse clicks
// as they may change the target positions
eraseDrawTargetPoss(true)
// Process mouse clicks
clickLoop:
for {
select {
case click := <-model.ClickCh:
handleClick(click)
default:
break clickLoop
}
}
// Next clear moving objects from the lab image:
model.Gopher.EraseImg()
for _, bd := range model.Bulldogs {
bd.EraseImg()
}
model.DrawImgAt(model.ExitImg, model.ExitPos.X, model.ExitPos.X)
if !model.Dead {
// Draw target positions
eraseDrawTargetPoss(false)
}
now := time.Now().UnixNano()
dt = float64(now-t) / 1e9
// Now step moving objects
stepGopher()
stepBulldogs()
// Check if Gopher reached the exit point
if int(model.Gopher.Pos.X) == model.ExitPos.X && int(model.Gopher.Pos.Y) == model.ExitPos.Y {
handleWinning()
}
t = now
// Sleep some time.
// Iterations might not be exact, but we don't rely on it:
// We calculate delta time and calculate moving and next positions
// based on the delta time.
model.Mutex.Unlock() // While sleeping, clients can request view images
if model.Won {
// If won, nothing has to be done, just wait for a new game signal
<-model.NewGameCh // Blocking receive
// Send back value to detect it at the proper place
model.NewGameCh <- 1
}
time.Sleep(time.Millisecond * 50) // ~20 FPS
model.Mutex.Lock() // We will modify model now, labyrinth image might change so lock.
time.Sleep(time.Millisecond * time.Duration(LoopDelay))
model.Mutex.Lock() // We will modify model now, labyrinth image might change so lock.
}
}
```
我的疑问在于clickLoop后面的这个for循环,
在某个时间段,处理完所有的click之后,这个循环不是会退出么?如果退出了,如何再次进入这个循环,再次收集鼠标点击的操作?
有疑问加站长微信联系(非本文作者)