首先,对于糟糕的代码风格道歉,这是我的第一个大项目(我的python类的最后一个)。我已经停留在循环设置中,我需要从"主菜单"显示菜单功能到movemenu功能,因为它现在运行时从显示菜单中选择A没有任何反应,选择b和c工作正常。我的猜测是我如何设计循环,以及从一个循环到另一个循环的断开/移动。
就实际运动而言,我对它进行了很好的尝试,但是让菜单工作对于能够测试运动至关重要,这就是我在这方面所处的位置。
非常感谢任何投入或建设性的批评。谢谢
#map movement functions---------------------------------------------------------
def mapFunc(x,y):
if x == 0:
if y == 0:
print("Sh*ts Swampy yo")
elif y == 1:
print("Sh*ts Sandy broski")
elif y == 2:
print("trees everywhere bro")
else:
print("how the f**k did you get here")
elif x == 1:
if y == 0:
print("Sh*ts Swampy yo")
elif y == 1:
print("Sh**s Sandy broski")
elif y == 2:
print("trees everywhere bro")
else:
print("how the f**k did you get here")
elif x == 2:
if y == 0:
print("S**s Swampy yo")
elif y == 1:
print("Sh*s Sandy broski")
elif y == 2:
print("trees everywhere bruh")
else:
print("how the f**k did you get here")
def displayMenu():
choice = None
choice = input("""
Choose from the following options:
A.)Map Movement
B.) Display Inventory and Stats
C.) Configure Power Treads
""").upper()
if choice == 'A':
moveMenu()
completed = True
elif choice == 'B':
displayStats(player)
elif choice == 'C':
askPowerTread()
else:
choice not in ['A','B','C']
print("Invalid choice.")
def moveMenu():
choice = None
validMapMovement = False
#while choice not in ('A', 'B', 'C', 'D'):
while not validMapMovement:
choice = input
("""
Choose a direction:
A.) North
B.) South
C.) East
D.) West
""").upper()
if choice == 'A':
if playerY (+1) >= 0:
playerY -= 1
mapFunc(playerX, playerY)
validMapMovement = True
else:
print("Invalid Choice")
elif choice == 'B':
if playerY +1 <= 2:
playerY += 1
mapFunc(playerX, playerY)
validMapMovement = True
else:
print("Invalid Choice")
elif choice == 'C':
if playerX +1 <= 2:
playerX += 1
mapFunc(playerX, playerY)
validMapMovement = True
else:
print("Invalid Choice")
elif choice == 'D':
if playerY -1 >= 0:
playerX -= 1
mapFunc(playerX, playerY)
validMapMovement = True
else:
print("Invalid Choice")
else:
choice not in ('A', 'B', 'C', 'D')
print("Invalid Choice")
roll()
然后我的主文件在其中的文件
#ExampleQuest, v5.5
from ExampleQuestFunctions import *
import random #rolls
import time #sleep
# Introduction #
#Main Loop()---------------------------------------------------------------------------
player = ["",20,5,10,3,7,0]
playerInventory = []
playerX = 0
playerY = 0
def mapPhase():
completed = False
while not completed:
displayMenu()
#Battle Loop()---------------------
def battlePhase():
#Instance Variables
playerTurn = True
completed = False
playAgain = True
while playAgain:
#Create the enemy pool, and generate a random enemy
pool = createEnemyPool()
enemy = random.choice(pool)
#Start the battle
player = battle(player, enemy, playerTurn)
#Continue Adventure
if getString("Would you like to continue your adventure?").upper() not in ['Y','YES']:
playAgain = False
else:
playAgain = False
inventoryUpdate()
#-----------------------
def main():
mapPhase()
#Run it!
main()
答案 0 :(得分:0)
类似这样的事情就是我如何做到这一点,假设我理解你的问题......那么你只需要为每个菜单格式化一个menulist并且几乎可以处理所有事情
def get_player_choice(choices):
labels,actions = zip(*choices)
choice_actions = dict(enumerate(actions,1))
for choice_id,label in enumerate(labels,1):
print "%d. %s"%(choice_id,label)
while True:
result = raw_input("Select Option:")
try:
return choice_actions[int(result)]
except TypeError:
print "ERROR: Please enter a number choice!"
except KeyError:
print "ERROR: Unknown Choice!"
def LookAround():
print "You See Something!"
def Quit():
print sys.exit
menu_a = [
("Look Around",LookAround),
("Quit!",Quit)
]
fn = get_player_choice(menu_a)
fn()