它为什么运行下一行

时间:2017-10-24 01:06:38

标签: qbasic

当你被吃掉,被摧毁或者你被攻击时,它总是运行下一行脚本,例如,你摧毁怪物,但是然后打印你被吃掉了你得到了药水但是应该说你杀了怪物并获得药水。

570
3 PRINT "welcome to the dungeon, " + name$ + "!"
4 PRINT "monster!!! attack or run away"
5 INPUT co$
6 IF co$ = "attack" GOTO 7 ELSE GOTO 9
7 PRINT "you kill'd the monster"
8 INPUT n$
9 IF n$ = "next" THEN 11
10 PRINT "the monster ate you. Have a fun time in his belly!"
11 PRINT "You won a potion!!!"
12 PRINT "uh oh! You found a dragon"
13 PRINT "Use the potion, attack or run away"
14 INPUT com$
15 IF com$ = "attack" GOTO 18
16 IF com$ = "use potion" THEN 19 ELSE PRINT "fried human for mr dragon!!!"
18 PRINT "bye bye dragon"
19 PRINT "the dragon got to sleep and you got to get away!"
20

3 个答案:

答案 0 :(得分:1)

有多种方法可以改善您的计划。

  • 使用标签代替行号。如果您必须使用行号,至少计算10秒或100秒,以便您可以更轻松地插入新行。
  • 您的IF语句缺失END IF s。
  • 您在一些地方缺少THENGOTO个关键字。
  • 当你完成(被怪物吃掉),然后退出程序。我不确定是否有实际的退出或退出声明,但最后的标签运作良好。
  • 使用空白行将逻辑上相关的代码行组合在一起。

这是一个结合了所有这些的工作示例:

PRINT "welcome to the dungeon, " + name$ + "!"
PRINT "monster!!! attack or run away"
INPUT co$

IF co$ = "attack" THEN
  GOTO ATTACK
ELSE
  GOTO RUNAWAY
END IF

ATTACK:
PRINT "you kill'd the monster"
GOTO POTION

RUNAWAY:
PRINT "the monster ate you. Have a fun time in his belly!"
GOTO ENDPROGRAM

POTION:
PRINT "You won a potion!!!"
PRINT "uh oh! You found a dragon"
PRINT "Use the potion, attack or run away"
INPUT com$

IF com$ = "attack" THEN GOTO ATTACKDRAGON

IF com$ = "use potion" THEN
   GOTO SLEEPDRAGON
ELSE
   PRINT "fried human for mr dragon!!!"
   GOTO ENDPROGRAM
END IF

ATTACKDRAGON:
PRINT "bye bye dragon"
GOTO ENDPROGRAM

SLEEPDRAGON:
PRINT "the dragon got to sleep and you got to get away!"
GOTO ENDPROGRAM

ENDPROGRAM:
PRINT "done"

答案 1 :(得分:1)

当我进入时,你的游戏对我来说很好:

attack
next
attack

然而,你写它的方式非常挑剔。如果我没有准确输入(如果我在所有大写ATTACKattack中输入一个空格),它将与if语句不匹配,它将通过

您可以使用以下命令强制输入小写并修剪任何空格

INPUT co$
co$ = RTRIM$(LTRIM$(LCASE$(co$)))

这会让您接受像" aTTaCK "这样奇怪的输入。

其次,看起来当你逃跑时,怪物应该吃掉你。这是你的意图吗?如果是这样,那么就没有任何东西可以阻止它掉到下一行代码。

6 IF co$ = "attack" GOTO 7 ELSE GOTO 9
7 PRINT "you kill'd the monster"
8 INPUT n$
9 IF n$ = "next" THEN 11
10 PRINT "the monster ate you. Have a fun time in his belly!"
11 PRINT "You won a potion!!!"

在第10行,怪物吃掉你后,你可以:添加另一个GOTO语句跳转到程序的另一部分,或者END程序。以下是END该程序的示例:

6 IF co$ = "attack" GOTO 7 ELSE GOTO 9
7 PRINT "you kill'd the monster"
8 INPUT n$
9 IF n$ = "next" THEN 11
10 PRINT "the monster ate you. Have a fun time in his belly!": END
11 PRINT "You won a potion!!!"

最后,在Qbasic中,您不需要为每一行输入数字。这是一个维护的噩梦,这样的bug会不断弹出,因为当你以这种方式编写代码时很难捕捉到它们。您可以通过使用标签来简化自己。这里是代码,删除了行号并用标签替换(对于GOTO语句):

PRINT "welcome to the dungeon, " + name$ + "!"
PRINT "monster!!! attack or run away"
INPUT co$
IF co$ = "attack" GOTO 7 ELSE GOTO 9
7:
PRINT "you kill'd the monster"
INPUT n$
9:
IF n$ = "next" THEN 11
PRINT "the monster ate you. Have a fun time in his belly!"
END
11:
PRINT "You won a potion!!!"
PRINT "uh oh! You found a dragon"
PRINT "Use the potion, attack or run away"
INPUT com$
IF com$ = "attack" GOTO 18
IF com$ = "use potion" THEN 19 ELSE PRINT "fried human for mr dragon!!!"
18:
PRINT "bye bye dragon"
19:
PRINT "the dragon got to sleep and you got to get away"

您还可以使用字母标签,并形成描述性名称。如:

IF co$ = "attack" GOTO KillMonster ELSE GOTO RunAway
KillMonster:
PRINT "you kill'd the monster"
INPUT n$
RunAway:
IF n$ = "next" THEN GOTO WonPotion:
PRINT "the monster ate you. Have a fun time in his belly!"
END
WonPotion:
PRINT "You won a potion!!!"

阅读代码越容易,理解和查看问题就越容易。你也会有更多的乐趣。

答案 2 :(得分:0)

您可以尝试使用简单的命令菜单:

REM Code snip using menus in QBasic.
AttackMonster:
COLOR 15
PRINT "You encounter a dragon!"
COLOR 14
PRINT "Options:"
PRINT "  (A)ttack"
PRINT "  (R)un away"
PRINT "  (C)ast spell"
PRINT "Enter(A/R/C)";
INPUT P$
P$ = LCASE$(P$)
COLOR 15
IF P$ = "a" THEN PRINT "The dragon died!"
IF P$ = "r" THEN PRINT "You run away screaming!"
IF P$ = "c" THEN PRINT "You cast a spell on the dragon!"
RETURN