Why is a “main” game loop necessary for developing a game?

前端 未结 11 1819
自闭症患者
自闭症患者 2020-12-23 13:52

I find that most game development requires a main game loop, but I don\'t know why it\'s necessary. Couldn\'t we implement an event listener and respond to every user action

11条回答
  •  时光说笑
    2020-12-23 14:29

    A game loop (highly simplified is as follows)

    initialise
    do
         input
         update
         render
    loop
    clean up
    

    This will happen every frame the game is drawn. So for games that run at 60fps the above is performed sixty times every second.

    This means the game runs smoothly, the game stays in sync and the updates/draws per cycle happen frequently enough. Animation is simply a trick of the eye, objects move between locations but when played quickly enough they appear to be travelling between these locations.

    If you were to only update on user input, the game would only react when the user was providing input. Other game components such as A.I game objects would not react on their own. A loop is therefore the easiest and best way of updating a game.

提交回复
热议问题