Advanced
Now that you know all the main bits of BASIC we can move on to some more advanced stuff.
So first we will be learning about generating random numbers as variables
Our first program will generate a random number and if the user types in the right number they will win!
The code is:
start:
Random = math.getrandomnumber(10) <------- The max number that can be generated in this case 10
textwindow.writeline("guess my number between 1 and 10")
guess = textwindow.read()
If (guess = random) then
textwindow.writeline("Hurray!")
else
textwindow.writeline("hahahahahahahahahahahahaha")
endif
goto start
It should look like this
So first we will be learning about generating random numbers as variables
Our first program will generate a random number and if the user types in the right number they will win!
The code is:
start:
Random = math.getrandomnumber(10) <------- The max number that can be generated in this case 10
textwindow.writeline("guess my number between 1 and 10")
guess = textwindow.read()
If (guess = random) then
textwindow.writeline("Hurray!")
else
textwindow.writeline("hahahahahahahahahahahahaha")
endif
goto start
It should look like this
As you can see i got my number right on the second guess
So, How does it work?
Well the variable random is generated at the start and guess variable is whatever you put in and if guess = random then it says hurray! else it will say hahahahahahahahahahahahaha.
You are most likely bored of the boring white and black, so lets jazz it up a little!
For this we need the code:
Textwindow.ForegroundColor = "green" <------------ You may change this
Textwindow.Writeline("Wow, what a great color")
It should look like this:
So, How does it work?
Well the variable random is generated at the start and guess variable is whatever you put in and if guess = random then it says hurray! else it will say hahahahahahahahahahahahaha.
You are most likely bored of the boring white and black, so lets jazz it up a little!
For this we need the code:
Textwindow.ForegroundColor = "green" <------------ You may change this
Textwindow.Writeline("Wow, what a great color")
It should look like this:
I am quite sure you understand how that works it is quite simple.
Now we will move on to loops, a loop means that a program will run round and round and round forever.
This program will count up forever
code:
start:
i = i + 1
Textwindow.Writeline(i)
goto start
It should look like this:
Now we will move on to loops, a loop means that a program will run round and round and round forever.
This program will count up forever
code:
start:
i = i + 1
Textwindow.Writeline(i)
goto start
It should look like this:
So how does it work?
Well everytime it goes to start i = 1 more and then were telling the Textwindow to say i, wich is 1 more each time so it will count up
If you get that you will get this one, Were going to make it say a random number each time e.g. 1, 67, 15, 12, 9, 43 and so on
code:
Start:
number = math.getrandomnumber(20000)
Textwindow.Writeline(number)
goto start
It should look like this:
Well everytime it goes to start i = 1 more and then were telling the Textwindow to say i, wich is 1 more each time so it will count up
If you get that you will get this one, Were going to make it say a random number each time e.g. 1, 67, 15, 12, 9, 43 and so on
code:
Start:
number = math.getrandomnumber(20000)
Textwindow.Writeline(number)
goto start
It should look like this:
Next we will be learning about subroutines
For this we need the code:
Textwindow.Writeline("What do you want a calculator or a counter")
Wanted = Textwindow.read()
if (wanted = "calculator") then
calculator()
Else
counter()
endif
Sub calculator
Textwindow.Writeline("Ask me + question")
number1 = textwindow.read()
Textwindow.Writeline("+")
number2 = textwindow.read()
number3 = number1 + number2
Textwindow.Writeline("the answer is " + number3)
endsub
Sub counter
start:
i = i + 1
Textwindow.Writeline(i)
goto start
endsub
When you run it should look like this:
For this we need the code:
Textwindow.Writeline("What do you want a calculator or a counter")
Wanted = Textwindow.read()
if (wanted = "calculator") then
calculator()
Else
counter()
endif
Sub calculator
Textwindow.Writeline("Ask me + question")
number1 = textwindow.read()
Textwindow.Writeline("+")
number2 = textwindow.read()
number3 = number1 + number2
Textwindow.Writeline("the answer is " + number3)
endsub
Sub counter
start:
i = i + 1
Textwindow.Writeline(i)
goto start
endsub
When you run it should look like this:
Or it might look like this:
So how in the world does it all work.
Well, When we ask if we want a calculator or a counter we put wanted = TextWindow.read() And if wanted = "counter" Then it goes to the sub counter and in that sub we put a counter. But if its anything else it will go to the sub calculator and in that sub we have a calculator.
Tip always make the subs before you call them in a different part of your program
Well, When we ask if we want a calculator or a counter we put wanted = TextWindow.read() And if wanted = "counter" Then it goes to the sub counter and in that sub we put a counter. But if its anything else it will go to the sub calculator and in that sub we have a calculator.
Tip always make the subs before you call them in a different part of your program
Ok so you know how to make a + calculator lets make a x calculator.
We need the code:
Textwindow.Writeline("Ask me a x question one number hit enter and then another and hit enter")
number1 = TextWindow.read()
TextWindow.Writeline("x")
number2 = TextWindow.read()
number3 = number1 * number2
Textwindow.Writeline(number1 + " x " + number2 + " = " + number3)
When you run it should look like this:
We need the code:
Textwindow.Writeline("Ask me a x question one number hit enter and then another and hit enter")
number1 = TextWindow.read()
TextWindow.Writeline("x")
number2 = TextWindow.read()
number3 = number1 * number2
Textwindow.Writeline(number1 + " x " + number2 + " = " + number3)
When you run it should look like this:
It works simply like the first calculator but this time its number1 * number2