Coding Education STEM

Creating Magic 8 Ball Game in Scratch

Welcome back. Today, I’m sharing my version of the Magic 8 Ball game…you know, the one where you concentrate, and ask a question and shake the 8 ball to get the answer? I’m going to share its implementation in various languages (remember, I’m not a “programmer”, I’m a computer scientist…that means, I don’t like to remember syntaxes and nuances of each language since I don’t type them all day, but I instead focus on the logic and reference the syntaxes as needed)…namely, Scratch (this post), Python, C/C++, JavaScript. Look for those implementations coming soon.

In Scratch, because it abstracts us from language syntax, we focus mainly on logic and flow. And because it’s designed around visual aspects, creating image/sprites, audio etc. are much easier than in the other languages I mentioned. So let’s get started! I’ll use a wizard sprite, and will call the app  Magik8Wiz.

Play Here: https://scratch.mit.edu/projects/281516203/   You’ll hear audio, and see animations in the game and should work on any device with/without keyboard. The full source code is also there (click See inside button on that page).

Gameplay: A quick demo of in-game interaction…

Explanation: This is just a fun game to code (in any language). You set up the answers typically in an array (in Scratch, I do in a variable of type List). The variable Answers is hidden but in memory (see source) and contains the series of text lines that are the answers. The questions are entered using ask function in Scratch. The user-entry is saved in a variable called answer. I put in 12 possible answers (text lines) in the Answers variable, and randomly pick a number 1-12 (inclusive) to grab the string/text from Answers after each input. Obviously, there’s no input check 🙂 to keep things simple. Once the text is picked randomly from memory, I pass it on to a Text-to-speech engine extension (see my other Scratch blog on how to do this) so that the wizard can say it aloud. To load the strings into the custom List variable Answers, there are 2 ways to do it:

  1. Import it during development from a simple text file (that’s what I’ve done for this app)
  2. Load each string at run-time (that’s what I’ve done in this previous app)

Everything else such as background animation, sound effects, sprite animation are just optional and you should make your own (see source). Instructions are described in game page mentioned above.

Challenge: There’s a slight glitch on quitting. Can you identify it? Can you fix it?

P.S. Look for my implementations of this in other languages in my other posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top