A Text-Based Guessing Game

[ LiB ]

Now, let's put all of what we've learned in this chapter and create our first guessing game! Basically, the user will enter a number, and we will tell him if he is too high or too low. We will allow him to guess until he figures it out. In order to make this game work, we will be using a loop. If you cannot understand what the loop's function is, it will be explained in the next chapter.

First we need to create an initialization section. It will look something like this.

 ;demo02-11.bb - Try to guess the number Print "Welcome to the Guessing Game!" AppTitle "Guessing Game!" ;Seed the random generator...don't worry, it will be explained later SeedRnd MilliSecs() ;Pick a number between 1 and 100 numbertoguess = Rand(1,100) ;The num of guesses the user has used numofguesses = 0 

The first line, after Print , calls the function AppTitle . This changes the name in the head-ing bar, so that instead of the program being named "Blitz Runtime Window," it will be named "Guessing Game!"

The randomizer works like this: numbertoguess is assigned to a random number, which is returned by Rand . Rand returns a number between what is given; here, it returns a number between 1 and 100. This section prints out introduction text, sets up the guessing number, and declares some variables .

Next we set up the loop and the test to make sure the player guessed a number between 1 and 100.

 ;set the beginning of loop label .loopbegin     ;Find out the user's guess     guess = Input$("Guess a number ")     ;If player guesses outside of range, tell him to guess again     If guess > 100 Or guess < 1         Print "Pick a number between 1 and 100, silly!"         ;Go back to the beginning         Goto loopbegin     EndIf 

The first line of this code sets up a label to go back to the loop later. Next, the loop begins, the player is asked for input, and the number is tested to see if it is within the correct range. If not, the player is sent back to the beginning of the loop.

Now, we insert the code to test if the player has guessed correctly.

 ;Add a guess to the guess counter numofguesses = numofguesses + 1 ;If the guess is too low, go back to beginning If guess < numbertoguess Then     Print "The number was too low."     Goto loopbegin ;If guess is too high, go back to the beginning Else If guess > numbertoguess Then     Print "The number was too high."     Goto loopbegin EndIf 

The first line adds 1 to the user's number of guesses. Then, the code is tested to see if the user has guessed too high, too low, or just right. If he has guessed just right, the code just continues through to the end of the program without going back to the beginning of the loop.

Finally, we enter the last section of code.

 Print "You guessed the number " + numbertoguess + " in " + numofguesses  + " tries!." WaitKey 

This program can be run off the CD. It is named demo02-11.bb. Figure 2.14 shows the out put of the complete Guessing Game.

Figure 2.14. The complete Guessing Game.


[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net