Easy to Make Qbasic Games

Techwalla may earn compensation through affiliate links in this story. Learn more about our affiliate and product review process here.

BASIC, which stands for Beginner's All-Purpose Symbolic Instruction Code, is still the most well-known programming language out there for absolute beginners, and QBasic is a simple interpreter for it through which you can write and run programs. There's still a sizable community of QBasic hobbyists out there, and learning to make QBasic games teaches you programming fundamentals and is a lot of fun.


Getting Started with QBasic

If you don't have QBasic on your system, you can download the original version at Pete's QB Site (see Resources), or a more recent version called QB64 which is designed to be compatible with the original. Once you open it, you'll be presented with a blue screen. Type in the following:


Video of the Day

CLS PRINT "Hello, world!" INPUT "What's your name?"; name$ PRINT "Hello, "; name$; "!" END

Then press Shift + F5 to run the program. Here's what's happening:


"CLS" stands for "clear screen" and makes sure your program starts with a blank screen; otherwise, you'll see remnants from the previous program.

"PRINT" simply prints a string of text to the screen. If you're just printing a number, you don't need the quotes.


"INPUT" is a way to ask for a user's input. The end of the line, "name$", is a variable: it's where the user's answer will be stored. You can call it whatever you like; variables that contain text should end with "$", and variables that contain only numbers should end with "%".


The second "PRINT" line prints text along with whatever the user inputted.


"END", predictably, ends the program.

QBasic Guessing Games

Enter the following code into QBasic:


CLS chances% = 1 RANDOMIZE TIMER number% = INT(RND * 10) + 1 PRINT "I'm thinking of a number from 1 to 10. You have three chances to get it right."

DO WHILE chances% < 4 PRINT PRINT "You're on chance #"; chances%; "." PRINT INPUT "Guess the number"; guess% IF guess% = number% THEN GOTO youwin ELSEIF guess% < number% THEN PRINT "Too low." ELSEIF guess% > number% THEN PRINT "Too high." ELSE PRINT "There's been an error. Try again." END IF chances% = chances% + 1 LOOP


PRINT PRINT "You ran out of chances! Sorry!" END

youwin: PRINT PRINT "You guessed the number! The number was"; number%; "!" END


In this game, the program creates a random number from 1 to 10 and offers you three chances to get it right, giving you clues after the first two hints. It includes a lot of programming concepts.


After clearing the screen, "chances% = 1" creates a number variable and sets it to 1. This will track all of the chances the player has to guess the number.

The next two lines create the random number. "RANDOMIZE TIMER" is simply the method QBasic uses to come up with to randomize its numbers. The next line, "number% = INT(RND * 10) + 1", creates a random number from 1 to 10 and stores it in the number% variable. It looks complicated, but basically it's just saying "Make it one of the first ten positive integers." The "+ 1" at the end makes sure that the number will be from 1 to 10 and not from 0 to 9.


The "DO WHILE..." line creates a loop. Whenever you see a line that starts with "DO," scroll down a bit until you find a line that says "LOOP." Everything in between that line will continue to loop over and over again until a certain condition is met--in this case, until chances% is no longer less than four (or "< 4" in programming-speak).



The program then asks for a guess with the INPUT command, stores it in guess%, and proceeds to process it with the "IF...THEN...ELSE" method. As you can see, it works logically: "IF a statement is true, THEN do this; ELSEIF another statement is true, then do this; ELSE anything else is true, then do this."

The only extra aspect is the GOTO command. In this case, if the number is correct, the program goes to the section of the document labeled "youwin".

Right before the end of the loop, there's the "chances% = chances% + 1" line. That's to assure the loop only runs three times.

If the loop runs three times and the player has never been sent to the "youwin" section, the player loses, so right after the loop is a line informing the player of the loss. Below it is the "youwin" section, declared just by writing the name of the section with a colon.

More Game Ideas

With PRINT, INPUT, IF...THEN...ELSE, and GOTO, you have everything you need to create an easy-to-make text adventure. Here's some sample code:

house: INPUT "You're standing in a house. What would you like to do"; choice$ IF choice$ = "go upstairs" THEN GOTO upstairs ELSE GOTO house

Try to make it as complex as you can based on what you know. And for more inspiration, the web offers plenty of free QBasic game downloads. Play them and examine the code behind them to learn new techniques and get ideas.



references & resources

Report an Issue

screenshot of the current page

Screenshot loading...