I Need Help On My Eye Test Game
-
@kat I thiiiiink I did everything you said but if I get it right or wrong it always shows an error message :(
-
@turtlegamer-0 What does the message say?
-
@kat there was an error on line 90 and the error code says: "String index out of bounds. (String index length: 6, index: 17)" I have no idea what it's trying to say 😓
-
@turtlegamer-0 Ah ok, there's something I didn't spot. Both your array of answers and the variable that stores the player's response are both named "answer" and that's giving you the problem. Try changing lines 89 and 90 to say something like
playerAnswer = input( "What does it say?" ) if playerAnswer == answer[num] then
-
@kat Thank you so much! Now my eye test is ready to play! 😁 Here's the NEW ID!
ID: VQ173MND1Q -
@turtlegamer-0 You're welcome :) Well done, it's a very nice program! Excited to see what you do next!
-
@kat Wait, I actually think it's not finished. 😢 I've noticed that if you get it right, everything is perfect, but if you get it wrong, it makes you still type it twice. So I did an experiment where I added another question, and if you get it wrong it makes you type it 3 times, so it makes you type it again the same amount of questions that there are... If that makes sense
-
@turtlegamer-0 Ah, I think I see the problem. The first question doesn't have an
else
, so if you get it wrong there's noclear()
orupdate()
and you keep seeing the same word. To fix it you can copy the wholeelse
section from line 130 to 148 and paste it before theendif
on line 105 and it should work properly :) -
@kat For some reason... Still nope. If I do that it still says you get it wrong, but instead of restarting like it should, (New update) it just goes on to the next question... And also if you want to try out the newer version with the restarting and SFX I updated the ID.
-
@TurtleGamer Taking a look at the program, it seems you're guaranteed to keep running into confusing issues like this until you realise a couple of things - so here's my advice! This is going to be a long answer, but I hope you take the time to read it carefully and consider starting a new program which you make even more neat and tidy - this process is a big part of improving your coding skills!
- Most importantly - there is a lot of code which you should move out of your main loop, since it only needs to happen once and not constantly.
// Load all the things that only need to be loaded once BEFORE you start your loop music = loadAudio(blah blah blah) victorymusic = loadAudio(la la la) sadmusic = loadAudio(etc etc) correct = loadAudio(etc etc) wrong = loadAudio(etc etc) // Declare all the variables which you want access to from anywhere in the program answer = [ "turtle", "cold", "ice", etc etc ] XP = 0 num = 0 numAnswers = len(answer) // it's useful to have this is a variable so we don't have to keep using len() again and again etc... // Play the music before starting the loop - the first number is the Audio Channel. This is using 0, so let's avoid using that in future so we can keep the music playing playAudio(0, music, 1, 0.5, 1, -1) // NOW we start our main game loop loop // main game stuff repeat
- Make sure to indent your code properly. It makes it much, much easier to read for yourself and will prevent silly bugs from taking ages to fix. Let me show you an example - this might be a little confusing at first, but do your best to understand since it is crucially important!
a = 10 // everything inside this loop we add a tab at the start of the line, to show that the code within belongs to the context of that loop. loop clear() c = controls(0) // when we change context, like with an IF or another loop, we add another tab at the start. if c.a_pressed then b = 10 // this variable belongs to the context of this if, and cannot be accessed anywhere else else c = 20 // same applies for this variable endif print(a) // we can print a here because 'a' was created in the GLOBAL context (outside of any loops, ifs or functions) print(b) // this line causes an error, because b doesn't exist now (it was created inside the if - same for the 'c' variable) update() repeat
Notice that when we end a context (using endif for ifs and repeat for loops), we return back to the previous indentation. This means you can immediately see the program flow, and things like a missing
endif
or missingrepeat
are much easier to spot.- The third piece of advice I have is to use
functions
to make your life easier! This is a perfect chance for you to realise just how helpful they are and you'll want to use them all the time!
When you have a piece of code that you want to repeat many times, maybe with some slight variations, you can make a function to do it - this saves lots of copying and pasting, and a lot of wasted time changing multiple sections.
Here's an example. There are lots of times that you use the same couple of lines to play the correct/incorrect sounds. This could easily be a couple of functions:
function playCorrectSound() setVolume(0, 0) // first turn off the sound for audio channel 0 (where the background music is playing) playAudio(1, correct, 1, 0.5, 1, 0) // play the sound effect on channel 1 (so it doesn't replace the background music), and use a 0 at the end so that it doesn't keep looping sleep(.5) // Normally I would not recommend to use sleep, but for this it's okay as a delay to make sure the sound finishes setVolume(0, 1) // Turn the background music back on again! (Now it picks up where it left off!) return void function playIncorrectSound() setVolume(0, 0) playAudio(1, wrong, 1, 0.5, 1, 0) sleep(.5) setVolume(0, 1) return void
With these at the bottom of your program (outside of any thing else), you could just them instead of copying and pasting all that code:
if playerAnswer == answer[num] then score += 1 playCorrectSound() else playIncorrectSound() endif
Isn't that much easier? But this could be even more simple - we could combine them into one function and just give the function the sound effect to play:
function playSound(soundEffect) setVolume(0, 0) playAudio(1, soundEffect, 1, 0.5, 1, 0) sleep(.5) setVolume(0, 1) return void
In this example we're saying we're going to put something in the function brackets - now we could just do this:
if playerAnswer == answer[num] then score += 1 playSound(correct) else playSound(wrong) endif
- I'd like to give you an example of how to use functions to really make your program much, much cleaner. Any time you want to print something to the screen and make it wait for a bit until the next thing happens, you are doing something like:
ink(whatever colour you want) textSize(whatever size you want) print(whatever you want) update() // so it shows on the screen sleep(something) // to make it wait
You could make all this into a function which would really help. Something like this:
function superPrint(text, colour, size, delay) ink(colour) textSize(size) print(text) update() sleep(delay) return void
With that function in your program, you could just do this:
superPrint("Answer questions correct to earn xp! \n", lime, 30, 1) // Use a \n at the end of a bit of text to add a new line underneath! superPrint("You got this! \n", white, 50, 0.5)
This would print both the lines in different colours, and different sizes and for different lengths of time with only two lines of code instead of like 10!
Finally, to take this idea to the best level we need to recognise that every question is basically the same process.. This could be a function too!
function doQuestion() clear() num = rnd(numAnswer) superPrint(answer[num], magenta, 100, 0.2) playerAnswer = input("What does it say?") if playerAnswer == answer[num] then score += 1 playSound(correct) clear(lime) superPrint("Correct!", black, 100, 0.5) else playSound(wrong) clear(red) superPrint("Incorrect!", black, 100, 0.5) endif clear() superPrint("Loading...", white, 100, 1) return void
I think if you made these changes, you would find this program much easier to expand! Please let me know if I can help explain these things in a different way. It's important stuff and I want to help you to experience that level up moment!
-
@dave Thank you both! I still am a little confused, but I think I can figure it out. It will probably be a little longer until the program is done, so I'll work on it! Thank you again!
-
@dave Also sorry, but the thing I'm conFUZEd on is functions and how to use them... 🫤
-
@turtlegamer-0 Try your best! You already use functions all the time - the blue instructions in your code are all functions. They always have a pair of round brackets at the end, sometimes with stuff inside.
When you use a Fuze function like print(), Fuze behind the scenes takes the stuff in the brackets and gives them to some more complicated code we wrote which then does a particular task, in this case making text appear.
But you can make your own! Here's a simple way to think about it:
A function is like a container you make for a section of code - this means you can just write the container name instead of copying and pasting all the code inside it:
// This is called defining a function function showLoadingScreen() clear() textSize(100) print("Loading...") update() return void
With this in our code, we can just say
showLoadingScreen()
instead of copying and pasting those 4 lines every time.The thing to remember is that function definitions must be outside of any other loops/functions/ifs etc. I like to put them at the bottom of the program with a bookmark for the ones I'll be coming back to a lot.
But the really awesome thing you can do with functions is give them things, and receive them back. For a silly example:
// Defining the function: function isNameDave(name) isDave = false if name == "Dave" or name == "dave" then isDave = true endif return isDave // Using (also called 'calling') the function: playerName = input("What is your name?") if isNameDave(playerName) then print("hi dave") else print("you're not dave") endif
This very silly function tells us if a name we give it is "Dave" or "dave" - now we can use just one line instead of having to copy and paste a whole if all the time!
Of course that exampe isn't very helpful really, but it's just to show you how they work!
Let me know if it would be helpful to show you an example quiz program that uses functions and arrays of questions/answers.