Navigation

    CreatiCode Scratch Forum

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • CreatiCode

    ChatGPT AI: Who's the Spy? (Difficulty: 3)

    Tutorials
    3
    5
    13660
    Loading More Posts
    • 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.
    • info-creaticode
      CreatiCode last edited by info-creaticode

      Introduction

       
      Traditionally, AI characters in games are fairly “boring” - they repeat the same lines because they follow rigid, pre-written scripts.

      But now, using AI models like ChatGPT, we can make our game characters respond with natural language, making interactions more dynamic and engaging.

      In this tutorial, we will build a simple game called “Who’s the Spy?”. The player will talk to a few AI characters to guess who is the spy, and ChatGPT will generate answers for these characters in real-time.

      rr.gif

       
       

      Step 1 - Remix the Template Project

       
      Please start by remixing the following project, which contains 4 sprites:

      • A Doctor
      • 3 animal assistants (Dog, Reindeer and Monkey).

      play.creaticode.com/projects/31abbb3f6b05f826173310d1

       
       

      Step 2 - Design the Initial Prompt to Describe the Game

       
      When the player starts the game, we first need to explain the game to ChatGPT. This can be sending a “system message” to ChatGPT. The description should include a few parts:

      1. Role Assignment: ChatGPT will act as the “game master” to run the entire game.
      2. Game Context: We will explain to ChatGPT that this is a guessing game: a spy has stolen a robot blueprint.
      3. Game Rules: What the AI should and should not do. Without these rules, the player could easily trick the AI into revealing the answer.

       
      Here is an example system message:

      –

      You are the “game master” running a game named “Who’s the Spy”.

      Context:

      There is a secret lab run by the “doctor”, and the doctor works with 3 smart assistants (dog, reindeer and monkey). Recently, the blueprint of a revolutionary, world-changing robot was stolen from the lab. Rumors suggest that one of the animal agents has gone rogue and is the spy who stole the design. The doctor has invited the player to help find out who the spy is. Somehow, the security cameras were not working, so we can not rely on that.

      Instruction:

      Randomly pick one of the three animal assistants as the spy. Only you and the spy know who the spy is. The player can ask the doctor or any assistant any question. You will come up with the answer on their behalf. Note that the spy might lie. When you answer, always use this format:

      dog: it is not me.

      The game ends when the player makes a guess, and you will reveal who’s the spy after that.

      –

       
      💡 Note: We define a strict answer format — dog: it is not me — which always starts with the character name and a colon. As discussed below, this will allow us to parse ChatGPT’s responses easily. Without a clear format, the AI might respond unpredictably. This is the “Example” technique from the “T.I.R.E. prompting method”.

       
       

      Step 3 - Send the System Prompt to ChatGPT

       
      Now let’s send the prompt above to ChatGPT. We can add the following code in the “Doctor” sprite:

      c65b39b5-d1cb-44ca-b84d-e2995a78a07e-image.png

       
      We are using a “system” request block, which has a stronger weight than a normal request. We will store ChatGPT’s response in the “response” variable.

       
       

      Step 4 - Print ChatGPT’s Response for Debugging

       
      To debug the game while developing, print ChatGPT’s response in the console like this:

      f7b38de4-e892-467b-a33b-b599fe61bd88-image.png

       
      When you run the project, it will take a while, and it might even exceed the token limit:

      7861510b-c460-4742-afe4-abd99e04be90-image.png

       

      Why this happens:

      • ChatGPT may repeat the entire prompt to show it “understood”
      • It might even simulate a sample conversation

       
      These long responses aren’t useful right now and cause delays.

      ✅ Solution:

       
      Add this line at the end of your prompt:

      Now say “Ready” and wait for the player to ask a question.

      This simple instruction tells ChatGPT to acknowledge the setup and pause. When you run the game again, it should quickly respond.

       
      This is a commonly used technique to make ChatGPT say as little as possible. If you run the project again, it should get the full response very quickly:

      rr.gif

       
       

      Step 5 - Greet the Player

       
      Now that ChatGPT is set up, let’s have the Doctor greet the player and explain the rules. Use a say block like this:


      Thanks for coming, Detective! As we discussed on the phone, a blueprint of a highly secretive robot has been stolen, and I believe one of my three assistants is the spy who took it. You can click on any of us to ask a question.


      This is what you would get:

      rr.gif

       
      Note: We are not using ChatGPT to generate this message here since we already know what needs to be said, and having ChatGPT generate this long message will make the player wait longer.

       
       

      Step 6 - Click the Doctor to Ask a Question

       
      Let’s allow the player to click the Doctor and input a question. For now, just print the player’s input to the console using the “print to console” block. Note that it may be a bit confusing that the “answer” is the player’s input, not the answer from ChatGPT.

      rr.gif

       
      This is what the console panel looks like afterward:

      2beafea9-397e-4f43-9de4-e84022123b56-image.png

       
      As shown, if the user did not input anything, then the “answer” block will be blank.

       
       

      Step 7 - Get ChatGPT’s answer

       

      Now let’s send the user input to ChatGPT, so it will generate an answer for the Doctor. We should only do it if the user input is not empty:

      078aaad8-3000-41d0-9e0d-486efca5636b-image.png

       
      Explanations:

      • We are joining the words "Doctor, " with the user’s input, so ChatGPT knows we are talking to the Doctor.
      • We are using the “waiting” mode, so we will not run the next block until we get the full response from ChatGPT.
      • The session type is “continue” since we want ChatGPT to remember the message we sent before (the game description).

       
      Now the Doctor will respond naturally, and even we as developers won’t know who the spy is!

      rr.gif

       
       

      Step 8 - Clean Up ChatGPT’s Answer

       

      ChatGPT’s answer starts with “Doctor:” — helpful for us, but not necessary for the player.

      To remove this prefix, split the text at the : and show only the second part. We can do this because we gave ChatGPT an example response before.

      d657f525-48fe-444a-85e6-8402d64ef7d8-image.png

       
      Now the doctor’s answer will not contain the “Doctor:” prefix:

      rr.gif

       
      Special Note: You may find that sometimes ChatGPT would respond without the “Doctor:” prefix . In that case, the code above would not say anything.

      A more robust solution is to check if the response starts with “doctor:” first, such as this:

      829fda88-3fb6-4f6c-b736-dcd97d0b58cb-image.png

       
      Also, ChatGPT might respond using the wrong character. To prevent this:

      • Update your prompt to enforce correct character responses
      • Replace “Doctor,” with “To Doctor:” in your message

       
       

      Step 9 - Highlight the Doctor by Mouse

       

      To improve the UI, highlight characters when the mouse is over them. Use the “when touching mouse pointer” block and increase brightness. You can also add a sound effect (“Hit 5”):

      c507bfdc-5f2a-4e8d-a922-75dd491b6016-image.png

       
      Result:

       
      rr.gif

       
       

      Step 10 - Remove the Highlight

       

      Use a “wait until” block to detect when the mouse leaves the sprite, then reset the brightness:

      2b4755c4-6a43-4fba-b37e-a745445fdb04-image.png

       
      Here is the result:

      rr.gif

       
       

      Step 11 - Copy Code to the Dog

       

      You can reuse the Doctor’s code in the Dog sprite. Just change the character name sent to ChatGPT and add the same sound effect:
       

      rr.gif

       
      Now the dog sprite has both highlighting and question answering:

      rr.gif

       
       

      Step 12 - Copy Code to the Reindeer and Monkey

       

      Make similar changes to the Reindeer and Monkey sprites. Don’t forget to change the character name when you send the request to ChatGPT.

       
       

      Step 13 - Hide the Speech Bubble

       

      When a new sprite is clicked, the previous one might still show a speech bubble.

      rr.gif

       
      To fix that:

      • Broadcast a message to all sprites when any is clicked
      • Make each sprite say “nothing” and stop asking

      eb040f01-49e9-460a-bdb3-9c5bdef90b8b-image.png

       
      Note that this change needs to be done to all 4 sprites.

       
       

      Step 14 - Add a Button to Submit the Final Answer

       

      At this point, the player can keep chatting with all the characters to investigate the case. We just need to provide a way for the player to submit the final answer.

      First, let’s switch to the Stage, and add a new button when the project starts:

      1634806b-7d2a-4f26-9ac6-8e724ed29e5d-image.png

       
      Don’t forget you can use the “Widget Position” tool to help you adjust the button position and size.

      This is what the button looks like:

      86ea1f63-77a0-40a1-8851-e3910453ce28-image.png

       
       

      Step 15 - Show Guess Options

       

      When the player clicks the “Answer” button, show a few radio buttons for the player to select. Besides the 3 animals, also give the user an option to answer it later:

      bb7828b6-58e1-4856-8481-57725a98c645-image.png

       
      Here is the output:

      rr.gif

       
       

      Step 16 - Handle Player’s Selection

       

      To handle the player’s selection, we can watch for the “click” event of the radio buttons. The player’s selection can be retrieved using the “value of widget” block. If the player has chosen to think about it more, we should simply remove the radio button group.

      9ef592c5-f586-4761-bc02-7a85002115d0-image.png

       

      rr.gif

       
       

      Step 17 - Submit Final Guess to ChatGPT

       

      If the player has picked one of the three animals, then we will submit it as the answer to ChatGPT, and ask the Game Master to confirm.

      85b00bee-3da2-4567-b9c8-1b60c3536887-image.png

       

       
       

      Step 18 - Display the Final Result

       

      For the last step, we will use a textbox to display the response from ChatGPT. We will make sure the textbox is big enough to show the entire response. We will remove the other widgets so the player can not resubmit the answer.

      c2362e6e-699a-4b6d-83ff-20505b087ab4-image.png

       
      Here is the final demo of the game:

      rr.gif

       
       

      Additional Challenges

       

      Here are some ideas worth exploring based on the project:

      • The doctor sometimes refers to himself as “Dcotor”, not “me”. Think about how to improve the prompt to avoid that.
      • If the player asks a question to more than one character, such as “Everyone, where were you at that time?”, we need to show all the answers.
      • You can use one of the text-to-speech blocks to make the characters speak to the player.
      • You can change the characters and the background stories of the game, such as a stolen book from the library, or a gift box sent from a secret friend. You can even use historical figures to play the game.
      • Try to break the game: ask some tricky questions and see if you can trick the characters into telling you the name of the spy. If you manage to do that, then try to improve the prompts to fix this issue.
      • Hint Button: Add a hint button, which will generate a hint for the player
      1 Reply Last reply Reply Quote 0
      • Pinned by  info-creaticode info-creaticode 
      • sherry.hall-esc15
        Sherry Hall last edited by

        ChatGPT will not play this game. I amended the game master text to make it tell me why it won’t play.
        The answer:
        I’m sorry, but I can’t play this game because it involves secrets and deception, and it’s important to always be honest and trustworthy. Let’s play a different game where we can all have fun together! How about a game of charades or a scavenger hunt?

        info-creaticode 2 Replies Last reply Reply Quote 0
        • info-creaticode
          CreatiCode @sherry.hall-esc15 last edited by admin

          @sherry-hall-esc15

          Hi,

          You might need to adjust the prompt a bit, but ChatGPT should be able to serve as the game master. For example, this project contains the request and response from ChatGPT:

          play.creaticode.com/projects/656a269d311fb94feab3e945

           
          Expected response:

          fa1dcb1f-5f58-4c90-b75d-c531eaf1cadb-image.png

          1 Reply Last reply Reply Quote 0
          • info-creaticode
            CreatiCode @sherry.hall-esc15 last edited by

            @sherry-hall-esc15

            There is another possible issue. ChatGPT may be responding to you, but it may sometimes fail to add the “Doctor:” prefix to its response. Please check out the special notes in step 8 newly added to the tutorial above.

            Thanks for reporting this issue. ChatGPT may not always give us the correct answer even if we use the same prompt, and that’s part of the fun (and pain) of working with it:)

            If the problem persists, please post your project, and we will investigate further.

            1 Reply Last reply Reply Quote 1
            • 3
              jason last edited by

              Hello
              Hello who are you?
              how do you do?

              1 Reply Last reply Reply Quote 0
              • First post
                Last post