设计一个RPG类,坚持我的循环

2024-10-03 11:13:33 发布

您现在位置:Python中文网/ 问答频道 /正文

所以首先为糟糕的代码风格道歉,这是我的第一个大项目(我的python类的最后一个项目)。我卡在循环设置我需要从“主菜单”显示菜单功能移动到移动菜单功能,因为它现在运行时,选择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()

Tags: thetrueifdef菜单notelseprint
1条回答
网友
1楼 · 发布于 2024-10-03 11:13:33

如果我理解你的问题,我会这样做的。。。然后你只需要为每个菜单设置一个菜单列表的格式,它就可以处理几乎所有的事情

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()          

相关问题 更多 >