Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • CreatiCode
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo

CreatiCode Scratch Forum

  1. CreatiCode Forum
  2. Knowledge Base
  3. Tutorials
  4. Game AI for Flappy Bird (Difficulty: 2)

Game AI for Flappy Bird (Difficulty: 2)

Scheduled Pinned Locked Moved Tutorials
1 Posts 1 Posters 8.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • CreatiCodeI Offline
    CreatiCodeI Offline
    CreatiCode
    wrote on last edited by info-creaticode
    #1

    Introduction


     

    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 SPACE key to make the bird flap.

    In this tutorial, you will learn to build a simple AI program to control the bird instead of manually.

    Note that this AI will be based on programming logic, rather than large language models (LLMs). That is due to 3 reasons:

    • LLMs are too slow 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.
    • LLMs are too unpredictable: 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.
    • LLMs are too expensive: 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.

    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.

    Now let’s get started with this simple AI program.

     
     


    Step 1 - Remix the Starter Project


     

    Open this link and remix the project:

    play.creaticode.com/projects/684dbd401c49cae4509533bd

     
     


    Step 2 - Read the Existing Code for “AI”


     

    In the “AI” sprite, there are 2 stacks of code.

    First, when the green flag is clicked, 3 parameters are set:

    • How many columns (green pipes) are placed before the nest.
    • The gravity for the bird’s falling speed;
    • How much the bird rises up on each flap.

    AI parameters

     
    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.

    Flap loop

     
     


    Step 3 - Adjust the Game Settings


     

    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:
     

    Bird falls


    This is because the “gravity” and “flap strength” are not fine-tuned 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:

    Wave flight

     
     


     

     

    Step 4 - A Shorter Wait Interval

     

     

    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.

    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.

    Change delay

     
    As a result, when you run again, the bird should reach the ceiling very quickly and stay there:

    Bird at top

     
     
     
     

    Step 5 - Skip some flaps

     

     

    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.

    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:

    • The bird’s Y position
    • The Y position of the nest

    Your AI can access these 2 values using these 2 reporter blocks:

    Y sensing
     

     
    Now, can you change the forever loop so that the bird would not flap if it is already above the nest?

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

     

    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:

    If nest is above

     
    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:

    Bird matches nest

     
     
     
     

    Step 6 - Add a few columns

      

    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:

    Set to 4 columns

     
    Obviously the bird will crash with the columns and fail. What can we do?

     
      
     

    Step 7 - Fly through all columns

     

     

    Similar to above, we need to provide data to the AI. Specifically, here are information about the columns:

    1. Each column has an opening at its center, and the Y positions of these openings are stored in the list named “columnYs”.
    2. The number of columns is given in the variable “columns”.
    3. 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.

     
    With these information, can you try to make the bird fly through all the columns? Don’t worry about the nest for now.

     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    In fact, all that we need to do is to replace “nestY” with the Y position of the upcoming column:

    Use columnY

      

    The bird should have no problem flying through all columns:

    Success with pipes

     
     

     

    Step 8 - Fly to the nest after all the columns

      

    For our last step, we need to combine our solution for columns and the nest. When the bird has passed the last column, the nextColumnIndex will be more than the columns variable, and that’s when we should target the nest instead. Can you implement this change?

      
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     

    Here is an example solution:

    Switch to nest after pipes

     
    Here is the final demo:

    Final full run

     
     
     
     

    Extra Practices

     

    To practice what you have learned, here is an additional challenge for you.

    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.

    Bird too low

     
    To ensure the bird is always safe, it should start to flap if it is too close to the ground (which is at Y of -160). You should first try to reproduce the issue by setting nextY to a low value like -140 in the setup sprite, then improve the AI logic.

    1 Reply Last reply
    0
    • CreatiCodeI CreatiCode pinned this topic on

    Hello! It looks like you're interested in this conversation, but you don't have an account yet.

    Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.

    With your input, this post could be even better 💗

    Register Login
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode