N次迭代后屏幕不会更新

我在我的excel上运行了一个非常简单的“Snake”类游戏,这意味着我需要在我的main-while循环内不断更新屏幕。 “蛇”(基本上是一个彩色细胞)在20×20桌子内随机移动。

它运行好几秒钟,然而,一些点屏幕停止更新; ans excel停止响应,同时仍然运行代码。 这导致崩溃。

我用For循环取代了循环,避免了崩溃,但仍然停止更新,直到循环结束。 循环停止时,屏幕最后一次更新。 我还在每个函数之后join了“睡眠”语句,但这并没有帮助。 这里是主要的代码:

Sub main() Dim table(20, 20) As Integer Dim index_move As Integer 'Position class contains only X and Y parameters Dim index_actual_head_pos As Position Set index_actual_head_pos = New Position Dim i As Long index_actual_head_pos.x = 5 index_actual_head_pos.y = 15 Application.ScreenUpdating = True For i = 1 To 50 'this remplaces the initial while loop 'next line generates a random movement, bounded by a 20x20 matrix index_move = generer_movement(index_actual_head_pos) 'next line updates the "snake" position params Call update_position(index_actual_head_pos, index_move) 'next line deletes previous snake and draws a new one Call draw(index_actual_head_pos) Sleep (200) Next i End Sub 

请注意,问题是真正的屏幕更新。 如前所述,我不知道如何强制excel不断更新屏幕

非常感谢,

丹尼斯

你应该放弃Sleep(200) ,让Excel应用程序对象做一些200毫秒的处理。

 dim dTill as double 'should be at the top with the other var declarations dtill = Timer + 0.200 do while timer < dTill and timer > 0.200: DoEvents: loop 

timer > 0.200是重要的,因为定时器在午夜重置,如果你在¹/ 5秒期间午夜越过,你不希望它在24小时内全部失速。

    Interesting Posts