Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

python - Combat sequence for a text-based RPG

This is the code.

#When I try to make the variable ‘enemy’ save globally, it says ‘Variable referenced before assignment.’, or something along those lines. How do I fix this?

#If you are going to suggest to make it all one function, I tried and it didn't work out. I am doing this so I can keep repeating the turn function until the player enters a vaild command.

#Player and enemy stat blocks. Order of stat blocks is: Health, Weapon, Weapon damage, Armor, and Armor type.

PC = [50, 'shortsword', 20, 'chain mail', 10]
goblin = [30, 'dagger', 15, 'leather', 5]
# Function for a single turn.
def turn(enemy):
    turn1 = input('Your turn. What would you like to do? ').lower()
    if turn1 != 'attack':
        print('Invalid command.')
        turn(enemy)
    elif turn == 'attack':
        global enemy
        enemy[0] = enemy[0] - PC[2]
    
# Function for the entirety of combat.  
def combat(enemy):
    while enemy[0] > 0:
        turn1 = input('Your turn. What would you like to do?')
        turn(enemy) 
    
    
combat(goblin)
question from:https://stackoverflow.com/questions/65660903/combat-sequence-for-a-text-based-rpg

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...