|
Our little game has some big limitations on it lets improve it. One thing is you only get one guess We need some way of looping back up if you didn't guess it right We'll use a do loop this simply repeats whatever is between the do and the loop command
cls Secret_number = Rnd(100)+1 Print "I know a secret number can you guess it?" do Print "Guess between 1 and 100 :"; Input my_guess If my_guess < secret_number then print "Nope, to low" else If my_guess > secret_number then print "Nope, to high" else If my_guess = secret_number print "Wow, you guessed my secret number" exit endif Loop print "Press any key to exit" Wait key End
I put the loop commands in red and other changes in blue, lets take a look at it We put a do command in where we want the loop to start A loop command where we want it to loop back from And a exit command to exit out of the loop
Notice that we changed the if the statement,why? Because we needed to have 2 event to if the guess was right print right and exit loop We change the last if then else command to a if else endif command the main difference is the you don't use a then statement and everything between the if and the endif is the event. If we needed to check other condition a else command can be placed before the endif. Like so If this Do this And this And this Else Do that And that endif Also note how I indent the events, this isn't nessecary but it helps keep the code readable, good programming habits are important when you have 10000 line of code to debug, trust me on this.
Ok run the program and see what happens. It lets you keep guessing until you get it right, but every time I run the program the answer is always 42? Also what if I want to play again? I shouldn't have to restart the game each time. Lets work on it
Randomize timer() Main: cls Secret_number = Rnd(100)+1 Print "I know a secret number can you guess it?" do Print "Guess between 1 and 100 :"; Input my_guess If my_guess < secret_number then print "Nope, to low" else If my_guess > secret_number then print "Nope, to high" else If my_guess = secret_number print "Wow, you guessed my secret number" exit endif Loop Print "Would you like to try again (y/n)" Wait key If scancode()=21 then goto main else exit prompt "Thank you for playing", "" End
First off we need a randomize command to reseed the random number generator you can enter any number you want but we need something that will change the timer() function returns the current time as a integer, different every time. Put in a label main: this just marks this location it the program Printed a prompt to play again Another if then else command it checks for a return scancode() of 21, 21 is the Y button. If the Y button is pressed then goto main: this is about the only time you want to use a goto command they are consider sloppy in most other loop action, else exit prompt. What the heck is this? The exit prompt pop a message window up when the end command exits the program. A handy little thing. The exit prompt accepts 2 strings "title bar text","in box text".
We now have a nice little program with 15 different keywords. You have learned to do text output, returned input, keypressed input, random variables, do loop, goto loop, and condition checking with the if then process.
|
|