我在为AP Computer Science校长项目制作的基于文本的游戏时遇到问题。应该发生的是以下代码行:
def pI(options):
#PI stands for Player Input
playerInput = (input("==> "))
if playerInput not in options or commands:
print(playerInput + " is not a possible option")
print("Do /options for a list of acceptable options")
playerInput = (input("==> "))
if playerInput == "/commands":
print(commands)
elif playerInput == "/options":
print("Acceptable inouts are" + options)
elif playerInput == "/help":
gHelp()
return(playerInput)
应该检查玩家的输入是否可以完成。但是,使用此系统,解释器甚至不会检查playerInput。我没有收到任何错误,但是没有按照我想要的方式工作。逻辑对我来说很有意义,但对解释器却没有意义。有人知道为什么是这样吗?如果有帮助,这里是我到目前为止用于游戏的其余代码:
#Made by -----------------------------------------------------------#
import sys
import os
import time
import random
os.system("mode con: cols=45 lines=80")
#Commands-----------------------------------------------------------#
commands = ["/options, /Help"]
#Functions----------------------------------------------------------#
def save():
print("This is supposed to save, but it doesn't work yet")
def clear():
os.system('cls')
def back():
pCS
def pI(options):
#PI stands for Player Input
playerInput = (input("==> "))
if playerInput not in options or commands:
print(playerInput + " is not a possible option")
print("Do /options for a list of acceptable options")
playerInput = (input("==> "))
if playerInput == "/commands":
print(commands)
elif playerInput == "/options":
print("Acceptable inouts are" + options)
elif playerInput == "/help":
gHelp()
return(playerInput)
#Graphics-----------------------------------------------------------#
#All Graphic functions start with "g" so that i don't take any
#names I might need later
def gLine():
#Function Draws lines
ps2("******************************************************************************************************************")
def gHeader():
gLine()
ps2("""
___________.__ __ __ __ .__ _____ __________ .__
\__ ___/| |__ ____ / \ / \___________ _/ |_| |__ _____/ ____\ \____ /____ | | ____ ____
| | | | \_/ __ \ \ \/\/ |_ __ \__ \\ __\ | \ / _ \ __\ / /\__ \ | | / ___\ / _ \
| | | Y \ ___/ \ / | | \// __ \| | | Y \ ( <_> ) | / /_ / __ \| |_/ /_/ > <_> )
|____| |___| /\___ > \__/\ / |__| (____ /__| |___| / \____/|__| /_______ (____ /____|___ / \____/
\/ \/ \/ \/ \/ \/ \/ /_____/ """)
gLine()
def gExit():
clear()
save()
gLine()
ps2("""
___________ .__ __ .__
\_ _____/__ __|__|/ |_|__| ____ ____
| __)_\ \/ / \ __\ |/ \ / ___\
| \> <| || | | | | \/ /_/ >
/_______ /__/\_ \__||__| |__|___| /\___ / /\ /\ /\ /\ /\ /\ /\
\/ \/ \//_____/ \/ \/ \/ \/ \/ \/ \/""")
gLine()
sys.exit
def gHelp():
clear()
gLine()
p2("""
___ ___ .__ _____
/ | \ ____ | | ______ / \ ____ ____ __ __
/ ~ \/ __ \| | \____ \ / \ / \_/ __ \ / \| | \
\ Y | ___/| |_| |_> > / Y \ ___/| | \ | /
\___|_ / \___ >____/ __/ \____|__ /\___ >___| /____/
\/ \/ |__| \/ \/ \/ """)
gLine()
print("Welcome to the help menu!")
print("1. Go Back")
options = ["1"]
PI()
if PI == 1:
(back())
def gNorthernLights():
gLine()
ps2(""" ` : | | | |: || : ` : | |+|: | : : :| . `
` : | :| || |: : ` | | :| : | : |: | . :
.' ': || |: | ' ` || | : | |: : | . ` . :.
`' || | ' | * ` : | | :| |*| : : :|
* * ` | : : | . ` ' :| | :| . : : * :.||
.` | | | : .:| ` | || | : |: | | ||
' . + ` | : .: . '| | : :| : . |:| ||
. . ` *| || : ` | | :| | : |:| |
. . . || |.: * | || : : :|||
. . . * . . ` |||. + + '| ||| . ||`
. * . +:`|! . |||| :.||`
+ . ..!|* . | :`||+ |||`
. + : |||` .| :| | | |.| ||` .
* + ' + :|| |` :.+. || || | |:`|| `
. .||` . ..|| | |: '` `| | |` +
. +++ || !|!: ` :| |
+ . . | . `|||.: .|| . . `
' `|. . `:||| + ||' `
__ + * `' `'|. `:
"' `---''''----....____,..^---`^``----.,.___ `. `. . ____,.,-
___,--''''`---'' ^ ^ ^ ^ """'---,..___ __,..---'''
"' ^ ``--..,__ """)
gLine()
#Text Functions-----------------------------------------------------#
def ps(string):
#This function allows the game to type like a real person
#PS is short for "Print Slow"
typing_speed = 70
count = 0
space = False
#This aspect of the function works with the autotext wrap
#To make sure that it only wraps after full words, not
#midway through
for letter in string:
if letter == " " or letter == "":
space = True
else:
space = False
if count >= 50 and space == True:
print('\n')
count = 0
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(random.random()*10.0/typing_speed)
print('')
def ps2(str):
#This function is the same as PS1 but without the Text Wrapping
typing_speed = 600
#Ask Mr. Ortiz how to make previous function based of line len.
for letter in str:
sys.stdout.write(letter)
sys.stdout.flush()
time.sleep(random.random()*10.0/typing_speed)
print('')
#Mechanics----------------------------------------------------------#
#Game Loop----------------------------------------------------------#
def titleScreen():
gHeader()
ps("1. Start")
ps("2. Load")
ps("3. Exit")
ps("4. Help")
options = ["1","2","3","4"]
pI(options)
if pI == 1:
dream0()
pCS = dream0()
if pI == 2:
load
if PI == 3:
gExit()
if PI == 4:
gHelp()
def dream0():
clear()
gNorthernLights()
ps("[???] Your name my son..what do the mortals use to summon you?")
pName = str(input("==> "))
ps(pName + "? I'm just as amused by the lower realm's creativity, as their ability to destroy themselves.")
ps("The void..she calls to you" + pName + "She favors you, just as her divinity does to me. You..shall be my avatar. It is time. Her prophecy shall be fullfilled. Awaken, my child!")
time.sleep(3)
clear()
#dream0()
titleScreen()
#Player Data--------------------------------------------------------#
pName = "Yorick"
pTrueName = "kliros"
pCS = titleScreen()
#pCS stands for Player Current Scene
答案 0 :(得分:2)
这里有几个问题:
if playerInput not in options or commands:
如果playerInput不在选项中,或者定义了命令,它将激活。您的意思是说,如果playerInput不在选项中并且playerInput不在命令中,则应该激活它。
pI(options)
if pI == 1:
dream0()
pCS = dream0()
if pI == 2:
load
if PI == 3:
gExit()
if PI == 4:
gHelp()
在这里,您希望pI(options)返回一些值,但是您从不保存它。当您检查pI == 1时,您正在检查函数是否等于1,这是没有意义的。此外,pI返回一个字符串,而不是数字。相反,您应该具有以下条件:
playerInput = pI(options)
if playerInput == "1":
dream0()
pCS = dream0()
if playerInput == "2":
load
if playerInput == "3":
gExit()
if playerInput == "4":
gHelp()