跳转到内容

QBasic/示例程序

来自维基教科书,开放的书籍,为开放的世界

计算器

[编辑 | 编辑源代码]

这个程序可以用来制作一个简单、功能完善的计算器,非常简单。

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

select case next$
case "a"
c = a + b
print "sum is:";c
case "s"
c = a - b
print "Difference is:";c
case "m"
c= a*b
print "Product is :";c
case "d"
c = a/b
print "Quotient is:";c
case "e"
c = a^b
print "Exponentification is:"c
case "r"
c = a^ 1/b
print "Root is:";c
case "q"
end

end select
sleep 3
goto 10







sub input_var
input "enter second operand";b
end sub

有关参考,请转到 基本数学


基本游戏

[编辑 | 编辑源代码]

使用动画制作一个简单游戏。

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

        IF TRY_COUNT > 30 THEN
            CLS
            LOCATE 10, 1
            PRINT "Remarks:"
            IF POINT_COUNT < 10 THEN PRINT "OH NO! NICE TRY!"
            IF POINT_COUNT > 10 AND POINT_COUNT < 16 THEN PRINT "GOOD WORK!"
            IF POINT_COUNT > 15 AND POINT_COUNT < 21 THEN PRINT "GREAT!"
            IF POINT_COUNT > 20 AND POINT_COUNT < 26 THEN PRINT "AMAZING!"
            END
        END IF



        SECOND = VAL(RIGHT$(TIME$, 2))
        IF PREVSEC <> SECOND THEN
            COUNT = COUNT + 1
        END IF

        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

        LINE (A, B)-(A + 25, B), 15
        LINE (A, B + 25)-(A + 25, B + 25), 15
        LINE (A, B)-(A, B + 25), 15
        LINE (A + 25, B)-(A + 25, B + 25), 15
        PAINT (A, B), (1), 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

        IF COUNT > 1 THEN
            COUNT = 0
            TRY_COUNT = TRY_COUNT + 1
            GOTO 10

        END IF



        PREVSEC = SECOND
        GOTO 20



    LOOP
LOOP

有关参考,请转到 高级图形

一个与数字时钟非常相似的时钟,没有指针。如果你想用指针,可以使用绘制功能。

REM  Clock
SCREEN 7

CLS
start:
SCREEN 7
_FONT 16
LOCATE 1, 5
PRINT "CLOCK"
PRINT "________________________________________"

LINE (50, 50)-(100, 100), 1, BF
LOCATE 9, 5
PRINT "TIME"
LOCATE 10, 5
PRINT "CONTROL"
LINE (150, 50)-(200, 100), 2, BF
LOCATE 9, 18.5
PRINT "STOP WATCH"



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"





        WHILE close_count <> 10
            close_count = close_count + 1
            LOCATE 7, 5
            PRINT "APPLICATION  ";
            IF close_count MOD 2 = 1 THEN
                PRINT "CLOSING >>>   "
            ELSE
                PRINT "CLOSING   >>> "
            END IF




            SLEEP 1
        WEND

        CLS
        SCREEN 7
        COLOR 10, 0
        END
    END IF


    Mouser mx, my, mb
    IF mb THEN
        IF mx >= 50 AND my >= 50 AND mx <= 100 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 >= 50 AND my >= 50 AND mx <= 100 AND my <= 100 THEN
                GOTO proccess
            END IF
        END IF
    END IF



    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
                time_control = 1
                GOTO proccess
            END IF
        END IF
    END IF
LOOP








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

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
            time_control = 1
            GOTO proccess
        END IF
    END IF
END IF



WHILE INKEY$ <> " "
    SLEEP 1
    count = count + 1
    tc = tc + 1
    BEEP
    CLS
    LOCATE 1, 5
    PRINT "CLOCK"
    PRINT "________________________________________"

    IF time_control = 1 THEN
        LINE (150, 50)-(200, 100), 2, BF
    END IF
    LOCATE 3, 5
    PRINT "CURRENT TIME:"; TIME$
    LOCATE 5, 5
    PRINT "MINUTES:"; minute
    LOCATE 6, 5
    PRINT "SECONDS:"; count
    IF count = 60 THEN
        count = 0
        minute = minute + 1
    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



SUB Mouser (x, y, b)
    mi = _MOUSEINPUT
    b = _MOUSEBUTTON(1)
    x = _MOUSEX
    y = _MOUSEY
END SUB

这是你到目前为止阅读的所有章节的一个小的逻辑组合。

二进制编码器

[编辑 | 编辑源代码]

是的,这是一个二进制解码器,但也是一个二进制编码器。它将任何十进制系统数字转换为二进制。运行这个程序,自己看看。

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 ""



INPUT "Enter Decimal number"; a
CLS
LOCATE 1, 1
PRINT "Binary coder"
PRINT "_____________"
PRINT ""
PRINT ""
WHILE a <> 0
    PRINT a MOD 2;
    IF a MOD 2 = 1 THEN
        a = a \ 2
    ELSE a = a / 2
    END IF

WEND
PRINT ""
PRINT ""
PRINT "Binary code is reversed"
WHILE INKEY$ <> " "
WEND
GOTO start

唯一的问题是:二进制代码是反转的。你可能已经从最后一条 PRINT 语句中猜到了。我还没有想出如何反转它,所以我想你必须自己动手。而且,WHILE 循环有一个 print 语句,末尾有一个分号。这是为了确保下一个数字出现在它后面,而不是下一行。

抛射物游戏

[编辑 | 编辑源代码]

还记得大猩猩吗?好吧,去掉图形,剩下的就是这个。

10
RANDOMIZE TIMER
cor = RND * 150
cor2 = CINT(cor)
IF cor2 < 30 AND cor2 > -30 THEN GOTO 10

PRINT "The object to hit is at coordinates"; cor2

INPUT "enter velocity"; v
INPUT "enter angle"; a
d = ((v ^ 2) * SIN(2 * a)) / 10
PRINT "Hit on:"
PRINT CINT(-d)

IF CINT(-d) < cor2 + 30 AND CINT(-d) > cor2 - 30 THEN
    PRINT "Well Done!"
ELSEIF CINT(-d) < 30 AND CINT(-d) > -30 THEN PRINT "Hey , you hit us!"
ELSE PRINT "Ugh, not on target"
END IF

嗯?好吧,现在数学很复杂,但公式基本上是一样的。在这里,抛射物必须落在目标物 30 个坐标中的任何一个,否则你就输了。有关数学部分的更多信息,请参见 维基百科:抛射运动。提示:添加图形。这将是一大块代码,但最终产品将是惊人的!

检查听力

[编辑 | 编辑源代码]

好吧,现在,你能听到多高频率的声音?用这个程序测试你的听力。

REM ultrasonic_test
CLS
freq = 20000
DO
    PRINT "Frequency is:"; freq

    SOUND freq, 18.2
    INPUT "Can you hear?"; ans$
    IF ans$ = "no" THEN
        freqrange = freq
        GOTO 10
    END IF
    freq = freq + 100
LOOP
10
freq = freqrange
DO
    PRINT "frequency is:"; freq

    SOUND freq, 18.2
    INPUT "can you hear?"; ans$
    IF ans$ = "no" THEN
        PRINT "your max frequency is:"; freq
        END
    END IF
    freq = freq + 2
LOOP

看看这个!

华夏公益教科书