<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Game AI for Flappy Bird (Difficulty: 2)]]></title><description><![CDATA[<hr />
<h2><a class="anchor-offset" name="introduction"></a>Introduction</h2>
<hr />
<p dir="auto"></p>
<p dir="auto">Flappy Bird is a simple and fun game, where the player controls the bird to fly through a few obstacles to reach its nest. There is only one control: press the <strong>SPACE</strong> key to make the bird flap.</p>
<p dir="auto">In this tutorial, you will learn to build a simple AI program to control the bird instead of manually.</p>
<p dir="auto">Note that this AI will be based on <strong>programming logic</strong>, rather than large language models (LLMs). That is due to 3 reasons:</p>
<ul>
<li><strong>LLMs are too slow</strong> for such tasks: it usually takes a few seconds for LLMs to respond to each request, but we need to make the bird react and flap with a precision of milliseconds.</li>
<li><strong>LLMs are too unpredictable</strong>: LLMs are inherently random, and it is often hard to control them to generate precisely the same output every single time. In contrast, programming logic is much more predictable and reliable.</li>
<li><strong>LLMs are too expensive</strong>: It takes a lot of computation, and hence energy, to generate even a very simple output. To avoid such waste, when we can use programming logic to make a decision, we should do so.</li>
</ul>
<p dir="auto">The same argument also applies in many other situations, and that’s why a significant portion of AI systems will continue to be implemented using programming logic.</p>
<p dir="auto">Now let’s get started with this simple AI program.</p>
<h2><a class="anchor-offset" name></a></h2>
<hr />
<h2><a class="anchor-offset" name="step-1-remix-the-starter-project"></a>Step 1 - Remix the Starter Project</h2>
<hr />
<p dir="auto"></p>
<p dir="auto">Open this link and remix the project:</p>
<p dir="auto"><a href="http://play.creaticode.com/projects/684dbd401c49cae4509533bd" target="_blank" rel="noopener noreferrer nofollow ugc">play.creaticode.com/projects/684dbd401c49cae4509533bd</a></p>
<h2><a class="anchor-offset" name></a></h2>
<hr />
<h2><a class="anchor-offset" name="step-2-read-the-existing-code-for-ai"></a>Step 2 - Read the Existing Code for “AI”</h2>
<hr />
<p dir="auto"></p>
<p dir="auto">In the “AI” sprite, there are 2 stacks of code.</p>
<p dir="auto">First, when the green flag is clicked, 3 parameters are set:</p>
<ul>
<li>How many columns (green pipes) are placed before the nest.</li>
<li>The gravity for the bird’s falling speed;</li>
<li>How much the bird rises up on each flap.</li>
</ul>
<h2><a class="anchor-offset" name="img-src-https-cdncreaticodecom-b-cdn-net-scratch-gui-projects-forum-f324fcb1-d0cb-4f65-a86b-e5e0eaf6f563-png-alt-ai-parameters-class-img-responsive-img-markdown-width-385-height-375"></a><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f324fcb1-d0cb-4f65-a86b-e5e0eaf6f563.png" alt="AI parameters" class=" img-responsive img-markdown" width="385" height="375" /></h2>
<p dir="auto">Below that, when the “start” message is received, this AI program will simply send out the “flap” message every 0.3 seconds. Each “flap” message will make the bird flap once. This is the most basic AI program, which we will improve in the next few steps.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/47ca0488-972b-44ad-863d-facb079933bc.png" alt="Flap loop" class=" img-responsive img-markdown" width="249" height="260" /></p>
<h2><a class="anchor-offset" name></a></h2>
<hr />
<h2><a class="anchor-offset" name="step-3-adjust-the-game-settings"></a>Step 3 - Adjust the Game Settings</h2>
<hr />
<p dir="auto"></p>
<p dir="auto">When you click the green flag, you will find that the bird will fall to the ground. The falling speed will depend on your computer, but it will look similar to this:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/494a11fd-de85-4c18-82bb-257150420cc8.gif" alt="Bird falls" class=" img-responsive img-markdown" width="595" height="498" /></p>
<hr />
<p dir="auto">This is because the “<code>gravity</code>” and “<code>flap strength</code>” are not <strong>fine-tuned</strong> yet. As an exercise, please adjust these 2 values. In general, “gravity” should be a number between 0 and -2 (such as -0.5), and “flap strength” should be a number between 4 and 20. Your goal should be to make the bird fly horizontally with a nice waveform trail like this:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b3532a40-918b-42d0-a3d9-5b8a69a7758a.gif" alt="Wave flight" class=" img-responsive img-markdown" width="478" height="266" /></p>
<h2><a class="anchor-offset" name></a></h2>
<hr />
<p dir="auto"> </p>
<p dir="auto"> </p>
<h2><a class="anchor-offset" name="step-4-a-shorter-wait-interval"></a>Step 4 - A Shorter Wait Interval</h2>
<p dir="auto"> </p>
<p dir="auto"></p>
<p dir="auto">Currently, in the forever loop, we send out the “flap” message every 0.3 seconds, and the bird will stay at the same height. Suppose we want the bird to fly higher, then we need to use a shorter interval.</p>
<p dir="auto">Instead of 0.3 seconds, let’s use an interval of 0.03 seconds. This may be necessary if the bird needs to rise up very quickly.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/f7a89ddb-6516-4ecd-80ad-864b5bdb72c5.png" alt="Change delay" class=" img-responsive img-markdown" width="263" height="264" /></p>
<p dir="auto">As a result, when you run again, the bird should reach the ceiling very quickly and stay there:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/e87cec82-0d1e-4c71-9b0f-019a5797d39e.gif" alt="Bird at top" class=" img-responsive img-markdown" width="384" height="203" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-5-skip-some-flaps"></a>Step 5 - Skip some flaps</h2>
<p dir="auto"> </p>
<p dir="auto"></p>
<p dir="auto">Obviously, the current AI always makes the bird fly too high. A simple solution is to skip some flaps, so the bird will do a free fall to reach nests that are below its level.</p>
<p dir="auto">In general, to make smart decisions, an AI needs relevant sensing data. In this case, to determine whether the bird needs to fall a bit, we can compare these 2 variables:</p>
<ul>
<li>The bird’s Y position</li>
<li>The Y position of the nest</li>
</ul>
<p dir="auto">Your AI can access these 2 values using these 2 reporter blocks:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/97fe6cda-13e0-44df-8ce8-1f568adcfcf0.png" alt="Y sensing" class=" img-responsive img-markdown" width="352" height="60" /><br />
 </p>
<p dir="auto">Now, can you change the forever loop so that the bird would not flap if it is already above the nest?</p>
<p dir="auto"> <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 </p>
<p dir="auto"></p>
<p dir="auto">Here is one way to do it: we put the broadcast block inside an “if-then” block, so only when the nest is above the bird would the bird make a flap:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/2391e44c-aec5-4404-b910-1ffe551ef258.png" alt="If nest is above" class=" img-responsive img-markdown" width="515" height="349" /></p>
<p dir="auto">Try to run the program a few times. The nest is placed at random heights, but the bird will almost always rise or fall to its level quickly:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/0670de4d-06b3-4a96-93f5-8898a9d95392.gif" alt="Bird matches nest" class=" img-responsive img-markdown" width="597" height="498" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-6-add-a-few-columns"></a>Step 6 - Add a few columns</h2>
<p dir="auto"> </p>
<p dir="auto">Now let’s move on to the next challenge: make the bird fly through the columns (pipes). Change the “columns” variable to 4, which will add 4 columns:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/58aef34b-0df6-4795-9ae0-d85b3db30205.png" alt="Set to 4 columns" class=" img-responsive img-markdown" width="379" height="174" /></p>
<p dir="auto">Obviously the bird will crash with the columns and fail. What can we do?</p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="step-7-fly-through-all-columns"></a>Step 7 - Fly through all columns</h2>
<p dir="auto"> </p>
<p dir="auto"></p>
<p dir="auto">Similar to above, we need to provide data to the AI. Specifically, here are information about the columns:</p>
<ol>
<li>Each column has an opening at its center, and the Y positions of these openings are stored in the list named “columnYs”.</li>
<li>The number of columns is given in the variable “columns”.</li>
<li>The upcoming column has an index of “nextColumnIndex” in the list. For example, when the bird is facing the first column, nextColumnIndex is equal 1. After the bird flies through it, nextColumnIndex will automatically increase to 2.</li>
</ol>
<p dir="auto">With these information, can you try to make the bird fly through all the columns? Don’t worry about the nest for now.</p>
<p dir="auto"> <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 </p>
<p dir="auto">In fact, all that we need to do is to replace “nestY” with the Y position of the upcoming column:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/b057ad68-a648-4153-a19f-aeb36487b3c5.png" alt="Use columnY" class=" img-responsive img-markdown" width="840" height="365" /></p>
<p dir="auto"> </p>
<p dir="auto">The bird should have no problem flying through all columns:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/9cd8dea6-bd55-4645-b8c1-14ddbbf565e9.gif" alt="Success with pipes" class=" img-responsive img-markdown" width="597" height="498" /></p>
<p dir="auto"> </p>
<p dir="auto"> </p>
<h2><a class="anchor-offset" name="step-8-fly-to-the-nest-after-all-the-columns"></a>Step 8 - Fly to the nest after all the columns</h2>
<p dir="auto"> </p>
<p dir="auto">For our last step, we need to combine our solution for columns and the nest. When the bird has passed the last column, the <code>nextColumnIndex</code> will be more than the <code>columns</code> variable, and that’s when we should target the nest instead. Can you implement this change?</p>
<p dir="auto"> <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 <br />
 </p>
<p dir="auto">Here is an example solution:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/855c92f5-e409-4445-8fc5-5d5ba7b821d9.png" alt="Switch to nest after pipes" class=" img-responsive img-markdown" width="858" height="631" /></p>
<p dir="auto">Here is the final demo:</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/5e6cf722-3d5c-49f3-a009-457d1ecd2803.gif" alt="Final full run" class=" img-responsive img-markdown" width="597" height="498" /></p>
<p dir="auto"> <br />
 </p>
<h2><a class="anchor-offset" name="extra-practices"></a>Extra Practices</h2>
<p dir="auto"> </p>
<p dir="auto">To practice what you have learned, here is an additional challenge for you.</p>
<p dir="auto">Currently, the bird would only start flapping if it is below the target height (of the nest or the center of the opening), so it might fall too much below the target. If the target is very low, then the bird might touch the ground before flying back up.</p>
<p dir="auto"><img src="https://cdncreaticodecom.b-cdn.net/scratch-gui-projects/forum/80b7d647-56fd-4240-be7a-9a2a543f8ad8.png" alt="Bird too low" class=" img-responsive img-markdown" width="264" height="430" /></p>
<p dir="auto">To ensure the bird is always safe, <strong>it should start to flap if it is too close to the ground (which is at Y of -160)</strong>. You should first try to reproduce the issue by setting <code>nextY</code> to a low value like -140 in the <code>setup</code> sprite, then improve the AI logic.</p>
]]></description><link>https://forum.creaticode.com/topic/2010/game-ai-for-flappy-bird-difficulty-2</link><generator>RSS for Node</generator><lastBuildDate>Sat, 14 Mar 2026 08:51:55 GMT</lastBuildDate><atom:link href="https://forum.creaticode.com/topic/2010.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 15 Jun 2025 02:34:51 GMT</pubDate><ttl>60</ttl></channel></rss>