0

我一直在看 renpy 的关于如何做出选择的教程,除了一件小事,大部分都已经弄清楚了。

如何正确使用 elif 语句?我已经查看了基本的 python elif 语句,甚至是一个关于如何在 renpy 中使用它的实际站点,但无法让它工作。

(我附上了我的代码截图以及我的错误,非常感谢任何帮助)

这是我的代码片段:

define e = Character("???")
$ mage = False
$ warrior = False
$ archer = False

# The game starts here.

label start:
# Show a background.

scene bg black

# This shows a character sprite. 

show weird orb

# These display lines of dialogue.

e "Welcome human, what is your name?"

python:
    name = renpy.input(_("What's your name?"))
    name = name.strip() or __("John")

define m = Character("[name]")

e "Hmm, [name] is it?"
e "That's a wonderful name!"
m "Where am I?"
e "You'll know in good time, my child."
e "For now, tell me a bit about yourself"
menu:
    e "Which of these do you prefer?"
    "Magic":
        jump magic
    "Brute Force":
        jump force
    "Archery":
        jump archery

label magic:
    e "You chose magic."
    $ mage = True
    jump enter

label force:
    e "You chose brute force."
    $ warrior = True
    jump enter

label archery:
    e "You chose archery."
    $ archer = True
    jump enter

label enter:
    if mage:
        m "I'm a mage."
    elif warrior:
        m "I'm a warrior."
    else:
        m "I'm an archer"
return

这是错误的副本:

I'm sorry, but an uncaught exception occurred.

While running game code:
  File "game/script.rpy", line 66, in script
    if mage:
  File "game/script.rpy", line 66, in <module>
    if mage:
NameError: name 'mage' is not defined

-- Full Traceback ------------------------------------------------------------

Full traceback:
  File "game/script.rpy", line 66, in script
    if mage:
  File "C:\Users\ArceusPower101\Downloads\renpy-7.0.0-sdk\renpy\ast.py", line 1729, in execute
    if renpy.python.py_eval(condition):
  File "C:\Users\ArceusPower101\Downloads\renpy-7.0.0-sdk\renpy\python.py", line 1943, in py_eval
    return py_eval_bytecode(code, globals, locals)
  File "C:\Users\ArceusPower101\Downloads\renpy-7.0.0-sdk\renpy\python.py", line 1936, in py_eval_bytecode
    return eval(bytecode, globals, locals)
  File "game/script.rpy", line 66, in <module>
    if mage:
NameError: name 'mage' is not defined

Windows-8-6.2.9200
Ren'Py 7.0.0.196
Test 1.0
Thu Aug 23 02:06:20 2018

在此处输入图像描述

4

1 回答 1

2

您的代码给了您一个例外,因为这三行永远不会运行:

$ mage = False
$ warrior = False
$ archer = False

它们不会运行,因为它们出现在start:标签上方,这是代码开始运行的地方。

有几种方法可以解决此问题。一种是简单地重新排列代码,使start标签出现在这些行之上。另一种选择是default对每个分配使用一个语句:

default mage = False
default warrior = False 
default archer = False

这些default语句将在游戏开始和加载游戏时运行一次,但前提是尚未定义变量。

于 2018-08-23T07:31:16.257 回答