QBasic - Sample Programs - Wikibooks, Open Books For An Open World
QBasic - Sample Programs - Wikibooks, Open Books For An Open World
< QBasic
Calculator
This program can be used to make a simple, functioning calculator, very simply.
Rem calculator
cls
10
print "input first operand"
input a
print "select operation"
input b
print "addition(a)"
print "subtraction(s)"
print "multiplication(m)"
print "division(d)"
print "exponentification(e)"
print "rooting(r)"
print "Quit(q)"
do
next$ = inkey$
loop until next$ <> ""
gosub input_var2
sub input_var
input "enter second operand";b
end sub
Basic Game
SCREEN 7
COLOR 15, 1
_MOUSEHIDE
CLS
LOCATE 5, 1
PRINT "GUNSHOTS"
DO
NEXT$ = INKEY$
LOOP UNTIL NEXT$ <> ""
CLS
LOCATE 5, 1
PRINT "In this game, you require to bring"
PRINT ""
PRINT "the crosshairs to the box"
PRINT ""
PRINT " , which is the target ,"
PRINT ""
PRINT " and click to shoot it."
PRINT ""
PRINT " In this game , you control"
PRINT ""
PRINT "the crosshairs with your mouse."
PRINT ""
PRINT " You will be given a"
PRINT ""
PRINT " fixed number of tries."
PRINT ""
PRINT " The number of times you hit the target,"
PRINT ""
PRINT " you will be given a point "
DO
NEXT$ = INKEY$
LOOP UNTIL NEXT$ <> ""
CLS
LOCATE 5, 1
PRINT "Get Ready!"
DO
NEXT$ = INKEY$
LOOP UNTIL NEXT$ <> ""
10
A = INT(RND * 100)
B = INT(RND * 100)
DO: K$ = INKEY$
20
DO WHILE _MOUSEINPUT
CLS
LOCATE 25, 25
PRINT POINT_COUNT
X = _MOUSEX: Y = _MOUSEY
LINE (X - 10, Y)-(X + 10, Y), 15
LINE (X, Y - 10)-(X, Y + 10), 15
IF _MOUSEBUTTON(1) THEN
IF X > A AND X < A + 25 AND Y > B AND Y < B + 25 THEN
POINT_COUNT = POINT_COUNT + 1
TRY_COUNT = TRY_COUNT + 1
GOTO 10
END IF
END IF
END IF
PREVSEC = SECOND
GOTO 20
LOOP
LOOP
Clock
A clock which is quite like a digital clock,with no hands.Use draw to make them if you want.
REM Clock
SCREEN 7
CLS
start:
SCREEN 7
_FONT 16
LOCATE 1, 5
PRINT "CLOCK"
PRINT "________________________________________"
DO
exit$ = INKEY$
IF exit$ = "e" OR exit$ = "E" THEN
CLS
SCREEN 7
COLOR 2, 1
LOCATE 5, 5
PRINT "YOU HAVE ABORTED THE CLOCK"
SLEEP 1
WEND
CLS
SCREEN 7
COLOR 10, 0
END
END IF
proccess:
IF time_control = 0 THEN
time_enter:
LOCATE 12, 6
INPUT "enter time"; t
IF t > 1800 THEN
mistake = mistake + 1
IF mistake > 3 THEN
PRINT "BLOCKED"
END
END IF
GOTO time_enter
END IF
END IF
IF time_control = 0 THEN
LOCATE 8, 5
PRINT "TIME LEFT:"; (t - tc) \ 60; ":"; (t - tc) MOD 60
IF tc = t THEN
BEEP
BEEP
BEEP
BEEP
END
END IF
END IF
IF time_control = 1 THEN
Mouser mx, my, mb
IF mb THEN
IF mx >= 150 AND my >= 50 AND mx <= 200 AND my <= 100
THEN 'button down
DO WHILE mb 'wait for button release
Mouser mx, my, mb
LOOP
'verify mouse still in box area
IF mx >= 150 AND my >= 50 AND mx <= 200 AND my <=
100 THEN
END
END IF
END IF
END IF
LOCATE 10, 10
PRINT "PRESS BUTTON TO END"
END IF
WEND
GOTO start
This is a little logical combination of all the chapters you have read so far.
Binary Coder
Yes, this is YES a binary decoder, but a binary Coder. This takes any decimal system number and
converts it to binary. Run this program to see for yourself.
REM binary
SCREEN 7
COLOR 1, 2
_FONT 16
LOCATE 7, 10
PRINT "Binary Coder"
SLEEP 5
start:
CLS
LOCATE 1, 1
PRINT "Binary coder"
PRINT "_____________"
PRINT ""
PRINT ""
PRINT ""
PRINT ""
Just the trouble is: the binary code is reversed. You might have guessed it by looking at the last
PRINT statement. I still haven't figured out how to reverse it, so I guess you have to do it yourself.
And , the WHILE loop has a print statement with the semicolon at the end. That is used to ensure
that the next number comes after it, not on the next line.
Projectile Game
Remember Gorillas? Well, take out the graphics and what you get is this:
10
RANDOMIZE TIMER
cor = RND * 150
cor2 = CINT(cor)
IF cor2 < 30 AND cor2 > -30 THEN GOTO 10
Huh? Ok, now the maths is complicated , but the formula is basically the same. Here, the
projectile has to land between 30 coordinates out of the hit object, or else you lose. See
Wikipedia:Projectile motion for more information on the maths part. Tip: Add graphics. It will be a
big , fat piece of code, but the final product will be AMAZING!
Check hearing
Ok, now , how high a frequency sound can you hear? Test your hearing with this program:
REM ultrasonic_test
CLS
freq = 20000
DO
PRINT "Frequency is:"; freq