我最近几天前开始使用Python,并且已经决定构建一个小型文本冒险游戏。在论坛上阅读之后,我开始在课程中包含尽可能多的信息,以便保持简洁明了。
我的问题是我在我的类中存储的变量正在被执行,而不管我的“changeTo”函数中它们的相对“if”语句。这里例如,程序立即“改变”“p1SelectClass”而不给予用户访问主菜单的机会。以下是相关代码:
import time
from random import randrange
def changeTo(part):
print(part.desc)
time.sleep(1)
print(part.op1)
print(part.op2)
print(part.op3)
print(part.op4)
print(part.op5)
print("")
print(part.extra)
choice = input("> ")
if choice == "1":
part.exeOne
if choice == "2":
part.exeTwo
if choice == "3":
part.exeThree
if choice == "4":
part.exeFour
if choice == "5":
part.exeFive
tips = ["Enter 'funcHelp()' at any time for a list of commands", "The command
'checkInv()' will list all of your inventory items", "Entering 'vers()' will list
current version information", "The command 'skillGet()' will list each level in its
respective skill", "Want to check your hit points? Try 'player.hp'"]
randomTip = randrange(0, (len(tips)))
class player:
hp = (100)
inventory = [ "100 gold" ]
skills = [0, 0, 0, 0] #strength, endurance, dexterity, wisdom
class p1SelectClass:
desc = ("Welcome to Anguem, a fully fledged Python adventure game. Please select
Of the four major classes below\n")
op1 = ("Knight: 6 STR, 9 END, 4 DEX, 5 WIS")
op2 = ("Barbarian: 9 STR, 6 END, 5 DEX, 4 WIS")
op3 = ("Ranger: 6 STR, 4 END, 9 DEX, 5 WIS")
op4 = ("Wizard: 4 STR, 5 END, 6 DEX, 9 WIS")
op5 = ("")
extra = ("")
#execute
class p0MainMenu:
desc = ("Would you like to...")
op1 = ("1. Start a new game")
op2 = ("2. Load game")
op3 = ("3. Quit game")
op4 = ("")
op5 = ("")
extra = ("Tips of the play: " + tips[randomTip])
exeOne = changeTo(p1SelectClass)
exeTwo = ("Loading and saving is currently unsupported in Anguem 0.2")
exeThree = ("quit goes here") #placeholder
exeFour = ("Please select a valid option")
exeFive = ("Please select a valid option")
print("Hello user, welcome to Anguem 0.2\n")
time.sleep(2)
changeTo(p0MainMenu)
这可能是新手错误的谜团,但有没有人对如何正常工作有任何建议?
答案 0 :(得分:2)
你需要缩进。新的行,缩进,制表符等在Python中非常重要。
if choice == "1":
part.exeOne
elif choice == "2":
part.exeTwo
elif choice == "3":
part.exeThree
elif choice == "4":
part.exeFour
elif choice == "5":
part.exeFive
else:
# default stuff
答案 1 :(得分:0)
你有非常基本的错误(确实是“菜鸟”)。我建议你在继续之前先阅读更多关于Python的类。
话虽如此,我试图做出尽可能少的改变,保留你想做的事情(我认为)。我希望这有助于你前进。
import time
from random import randrange
def changeTo(part):
print(part.desc)
time.sleep(1)
print(part.op1)
print(part.op2)
print(part.op3)
print(part.op4)
print(part.op5)
print("")
print(part.extra)
choice = input("> ")
if choice == 1: # input returns an integer if you type an integer
part.exeOne() # to execute a function, you need the parentheses
if choice == 2:
part.exeTwo()
if choice == 3:
part.exeThree()
if choice == 4:
part.exeFour()
if choice == 5:
part.exeFive()
tips = ["Enter 'funcHelp()' at any time for a list of commands", "The command 'checkInv()' will list all of your inventory items", "Entering 'vers()' will list current version information", "The command 'skillGet()' will list each level in its respective skill", "Want to check your hit points? Try 'player.hp'"]
randomTip = randrange(0, (len(tips)))
class player:
hp = (100)
inventory = [ "100 gold" ]
skills = [0, 0, 0, 0] #strength, endurance, dexterity, wisdom
class p1SelectClass:
def __init__(self): # I am changing these from class variables to instance variables
# mostly to show you how it's done
# __init__() is called when you create a p1SelectClass instance
self.desc = ("Welcome to Anguem, a fully fledged Python adventure game. Please select Of the four major classes below\n") # instance variables need to be addressed with "self."
self.op1 = ("Knight: 6 STR, 9 END, 4 DEX, 5 WIS")
self.op2 = ("Barbarian: 9 STR, 6 END, 5 DEX, 4 WIS")
self.op3 = ("Ranger: 6 STR, 4 END, 9 DEX, 5 WIS")
self.op4 = ("Wizard: 4 STR, 5 END, 6 DEX, 9 WIS")
self.op5 = ("")
self.extra = ("")
#execute
def pp(x): # I defined this function to be able to call "print" from an expression
print x
class p0MainMenu:
def __init__(self):
self.desc = ("Would you like to...")
self.op1 = ("1. Start a new game")
self.op2 = ("2. Load game")
self.op3 = ("3. Quit game")
self.op4 = ("")
self.op5 = ("")
self.extra = ("Tips of the play: " + tips[randomTip])
# If you intend to run this later, then the variable must be executable.
# In this case, I created a lambda (an unnamed function)
# The proper way would be to define a method.
#
# This was the first error that you caught: exeOne = changeTo(p1SelectClass)
# runs changeTo immediately, it will not wait until exeOne is "called".
self.exeOne = lambda: changeTo(p1SelectClass())
self.exeTwo = lambda: pp("Loading and saving is currently unsupported in Anguem 0.2")
self.exeThree = lambda: pp("quit goes here") #placeholder
self.exeFour = lambda: pp("Please select a valid option")
self.exeFive = lambda: pp("Please select a valid option")
print("Hello user, welcome to Anguem 0.2\n")
time.sleep(2)
# p0MainMenu is the class, p0MainMenu() is a new instance of the class
changeTo(p0MainMenu())