@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 missing repeat 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!