1

我的代码如下所示:

            IF INKEY$ <> "" THEN CALL notitle
        PLAY "MST170o1e8o0b8o1e8"
        IF INKEY$ <> "" THEN CALL notitle
        PLAY "e8e4f#8g4f#8"
        IF INKEY$ <> "" THEN CALL notitle
        PLAY "g4e8d2o0b8o1d2 "
        IF INKEY$ <> "" THEN CALL notitle
        PLAY "o1e8o0b8o1e8e8e4f#8g4f#8g4a8b2g8b2MLg16a16"
        IF INKEY$ <> "" THEN CALL notitle
        PLAY "MSb4b8b8a8g8a4a8a4f#8g4g8MLg8f#8"
        IF INKEY$ <> "" THEN CALL notitle
        PLAY "MSe8f#4f#8f#8g8a8b4.a4.g4.f#4.o0b8o1e8e8e4d8e2."

这似乎相当乏味。有没有办法更简洁地重写这个?我相信代码试图为用户提供一种跳过音乐的方式。

4

2 回答 2

2

哇,在 2012 年编程 QBasic 嗯?那会是一个什么样的项目?

无论如何,我会将音乐字符串放入一个数组中,并在检查 INKEY$ 时遍历该数组。我没有安装 QB,所以我不确定这是否会起作用,但是像这样:

DIM mymusic$(6)
FOR i = 1 TO 6
    READ S$
    mymusic$(i) = S$
NEXT i

' loop while user doesn't press any key
i = 0
WHILE INKEY$ = ""
  ' i will be 0..5 so play i+1 to get 1..6
  PLAY mymusic$(i+1)

  ' use MOD to loop i through 0..5
  i = (i + 1) MOD 6
WEND

' user pressed a key
CALL notitle

' music strings in data segment
DATA "MST170o1e8o0b8o1e8"
DATA "e8e4f#8g4f#8"
DATA "g4e8d2o0b8o1d2"
DATA "o1e8o0b8o1e8e8e4f#8g4f#8g4a8b2g8b2MLg16a16"
DATA "MSb4b8b8a8g8a4a8a4f#8g4g8MLg8f#8"
DATA "MSe8f#4f#8f#8g8a8b4.a4.g4.f#4.o0b8o1e8e8e4d8e2."

顺便说一句,您可以将字符串分解为较短的位,以便更频繁地检查用户输入。

于 2012-11-05T16:29:42.273 回答
0

此片段在 Qbasic 中循环播放数据中的音乐:

' loop until keypress
i = 1
WHILE INKEY$ = ""
    RESTORE
    FOR j = 1 TO i
        READ p$
    NEXT
    PLAY p$
    i = i + 1
    IF i > 6 THEN i = 1
WEND

' user pressed a key
CALL notitle
' music strings in data segment
DATA "MST170o1e8o0b8o1e8"
DATA "e8e4f#8g4f#8"
DATA "g4e8d2o0b8o1d2"
DATA "o1e8o0b8o1e8e8e4f#8g4f#8g4a8b2g8b2MLg16a16"
DATA "MSb4b8b8a8g8a4a8a4f#8g4g8MLg8f#8"
DATA "MSe8f#4f#8f#8g8a8b4.a4.g4.f#4.o0b8o1e8e8e4d8e2."
于 2016-12-18T04:59:07.287 回答