{"id":307,"date":"2025-07-07T14:12:40","date_gmt":"2025-07-07T04:12:40","guid":{"rendered":"https:\/\/nonlinearexperience.com\/?p=307"},"modified":"2025-08-10T16:05:59","modified_gmt":"2025-08-10T06:05:59","slug":"game-loop","status":"publish","type":"post","link":"https:\/\/nonlinearexperience.com\/index.php\/2025\/07\/07\/game-loop\/","title":{"rendered":"Game loop"},"content":{"rendered":"<h1 class=\"wp-block-post-title\">Game loop<\/h1>\n\n\n<p class=\"wp-block-paragraph\">See also: <a href=\"https:\/\/nonlinearexperience.com\/index.php\/2025\/07\/07\/game-states\/\" data-type=\"post\" data-id=\"378\">Game states<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/nonlinearexperience.com\/index.php\/2025\/07\/07\/optimisation-tips\/\" data-type=\"post\" data-id=\"381\">optimisation<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A game loop should be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>perform async?<\/li>\n\n\n\n<li>have separate rendering loop and logic loop should be host frame rate independent &#8211; delta time &#8211; but never need to exceed the display refresh rate&#8230;<\/li>\n\n\n\n<li>Pause rendering loop and logic loop<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh7-rt.googleusercontent.com\/docsz\/AD_4nXf65bPwElwsBMe2vHOek3NNUZtDBk9PEpWgGcjPZKCX1mGTbM_TZvzC0yE1IGKQ5jHZYcdIybgXzMNonfOlF9qcjeaws-yCm-p9wxiWDDjj15ZA9uYaXxGxsluD-XZvGOlSuQI5Ir6bjGg6jcklEUAG5ss?key=qInBJpW04kC1Sm63MHgkfg\" alt=\"\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">SOURCE: <a href=\"https:\/\/gameprogrammingpatterns.com\/game-loop.html\">https:\/\/gameprogrammingpatterns.com\/game-loop.html<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Javascript &#8211; best practices for performant gameloops<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Performance is based upon how many things a game has to do each iteration of the loop. If you can defer a task, good. If you can store and reuse something, good. Consider if things could go in an alternative loop at a different refresh rate<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>logic loop step (alternate fps)\n<ul class=\"wp-block-list\">\n<li><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>render loop step (60fps)\n<ul class=\"wp-block-list\">\n<li>clear screen<\/li>\n\n\n\n<li>draw screen<\/li>\n\n\n\n<li>end<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">a method called &#8220;step&#8221; that calls &#8216;requestAnimationFrame()<br>By having the &#8216;step&#8217; called in a loop in the request animation, there is a chance for other processes to run, so it won&#8217;t crash via an infinite loop. Good!<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nstartGameLoop() {\n    const step = () =&gt; {\n        this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);\n        requestAnimationFrame(() =&gt; {\n            step();\n         })\n    }\n    step();\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">OPTIMIZATION<br>We currently now have a few loops running at the same time, so if we are having hundreds of objects at once, this could be an issue\u2026<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Work with the language and platform you are targeting &#8211; e.g. the javascript event loop is built into the browser&#8230; and the fact that it is a single threaded language&#8230;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Make the data do its thing, then render<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> &#8211; such as the logic&#8230;<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Start by clearing the screen<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is usual to clear the screen at the start of the game loop, then redraw the stuff.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A basic javascript game loop<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>window.requestAnimationFrame(gameLoop);\n\nfunction gameLoop() {\n    draw();\n    window.requestAnimationFrame(gameLoop);\n}\n\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also, having a timestamp when the loop runs &#8211; then can keep the game to run evenly\u2026<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">delta time<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">time diff between now and how long ago since the last frame ran\u2026<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">rendering fps vs game loop fps<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Separating the rendering and game loop means they can run at different speeds &#8211; e.g. game loop can quite easily run at \u00bd or \u00bc the rendering frame rate of 60 fps\u2026<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">see: github.com\/kittykatattack\/smoothie<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">general<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/gameprogrammingpatterns.com\/game-loop.html\">https:\/\/gameprogrammingpatterns.com\/game-loop.html<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">javascript<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>performant game loops in JavaScript: <a href=\"https:\/\/www.aleksandrhovhannisyan.com\/blog\/javascript-game-loop\/\">https:\/\/www.aleksandrhovhannisyan.com\/blog\/javascript-game-loop\/<\/a><\/li>\n\n\n\n<li>HTML5 game tutorial: <a href=\"https:\/\/spicyyoghurt.com\/tutorials\/html5-javascript-game-development\/create-a-proper-game-loop-with-requestanimationframe\">https:\/\/spicyyoghurt.com\/tutorials\/html5-javascript-game-development\/create-a-proper-game-loop-with-requestanimationframe<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/isaacsukin.com\/news\/2015\/01\/detailed-explanation-javascript-game-loops-and-timing\">https:\/\/isaacsukin.com\/news\/2015\/01\/detailed-explanation-javascript-game-loops-and-timing<\/a><\/li>\n\n\n\n<li><\/li>\n\n\n\n<li><a href=\"https:\/\/www.aleksandrhovhannisyan.com\/blog\/javascript-game-loop\">https:\/\/www.aleksandrhovhannisyan.com\/blog\/javascript-game-loop<\/a><\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-codez-up wp-block-embed-codez-up\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"uKLHcw2pgD\"><a href=\"https:\/\/codezup.com\/javascript-game-loop-tutorial\/\">Building a JavaScript Game Loop from Scratch: Tips and Tricks<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Building a JavaScript Game Loop from Scratch: Tips and Tricks&#8221; &#8212; Codez Up\" src=\"https:\/\/codezup.com\/javascript-game-loop-tutorial\/embed\/#?secret=Re33SLAR8K#?secret=uKLHcw2pgD\" data-secret=\"uKLHcw2pgD\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/jsforgames.com\/game-loop\">https:\/\/jsforgames.com\/game-loop<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.isaacsukin.com\/news\/2015\/01\/detailed-explanation-javascript-game-loops-and-timing\">https:\/\/www.isaacsukin.com\/news\/2015\/01\/detailed-explanation-javascript-game-loops-and-timing<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/medium.com\/@kristenrogers.kr75\/how-to-make-a-game-loop-in-javascript-e7d2838bbe33\">https:\/\/medium.com\/@kristenrogers.kr75\/how-to-make-a-game-loop-in-javascript-e7d2838bbe33<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">http:\/\/creativejs.com\/resources\/requestanimationframe\/<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/stackoverflow.com\/questions\/1955687\/best-way-for-simple-game-loop-in-javascript\">https:\/\/stackoverflow.com\/questions\/1955687\/best-way-for-simple-game-loop-in-javascript<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.sitepoint.com\/quick-tip-game-loop-in-javascript\">https:\/\/www.sitepoint.com\/quick-tip-game-loop-in-javascript<\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>See also: Game states optimisation A game loop should be able to: SOURCE: https:\/\/gameprogrammingpatterns.com\/game-loop.html Javascript &#8211; best practices for performant gameloops Performance is based upon how many things a game has to do each iteration of the loop. If you can defer a task, good. If you can store and reuse something, good. Consider if [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"wp-custom-template-nlx-the-knowledge-page","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-307","post","type-post","status-publish","format-standard","hentry","category-the-knowledge"],"_links":{"self":[{"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/posts\/307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/comments?post=307"}],"version-history":[{"count":10,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":738,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/posts\/307\/revisions\/738"}],"wp:attachment":[{"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/media?parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/categories?post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nonlinearexperience.com\/index.php\/wp-json\/wp\/v2\/tags?post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}