Python 使用一个函数循环到其他函数。

标签 python loops

我正在制作一款 RPG 游戏,每场战斗都引用 fightmode()。 所以对于我的第一个场景,我将它设为“def game1()”,然后从那里开始,它会导致 fightmode()。 fightmode() 中的战斗结束后,我希望它转到 game2() 或 game3() 等。我有办法做到这一点吗?到目前为止,这是我的代码。

虽然我还没有编写出 game2(),但我希望我说的是可以理解的。

import time
import random
global myhp
myhp = 20
mydmg = random.randint(2,5)
global mgold
mgold = 0

def start():
    print "Hello there.";   time.sleep(.5)
    myname = raw_input("What is your name? ")
    print "Welcome %s, this is..." %myname; time.sleep(.5)
    uname = myname.upper()
    print "\t\t\tTHE ADVENTURES OF %s" %uname;  time.sleep(.5)
    choice0 = ''
    allowed = ["y", "n"]
    while choice0.lower() not in allowed:
        choice0 = raw_input("\nWould you like to play the game? Y/N "); time.sleep(.5)
        choice0 = choice0.lower()
    if choice0 == "y":
        game1()
    if choice0 == "n":
        print "Alright, bye!"

def fightmode(name, hp, dmg_min, dmg_max, gold):
    global myhp
    print '\n\n\nYou are in a fight with %s' %name; time.sleep(.5)
    print '%s has %sHP' %(name, hp);    time.sleep(.5)
    while myhp > 0 and hp > 0:
        print '\n\t1. Attack \n\t2. Guard \n\t3. Run away.';    time.sleep(.2)
        opt1= ''
        allowed = ["1", "2", "3"]   
        while opt1 not in allowed:
            mydmg = random.randint(2,5)
            dmg = random.randint(dmg_min, dmg_max)
            opt1 = raw_input("\nWhat will you do? ")
            if opt1 == "1":
                hp = hp - mydmg 
                myhp = myhp - dmg
                print "\nYou have inflicted %d damage on %s. %s's HP is %s" %(mydmg, name, name, hp);   time.sleep(.5)
                if hp >= 1:
                    print "%s attacked you and did %d damage. Your HP fell down to %s" %(name, dmg, myhp);  time.sleep(.5)
                    if myhp <= 0 :
                        print"\n\tYou have been defeated...";   time.sleep(.5)
                        restart = ''
                        allowed =["y", "n"]
                        restart = restart.lower()
                        restart = raw_input("\n\tWould you like to start over? ");  time.sleep(.5)
                        if restart == "y":
                            myhp = 20
                            game1()     
            if opt1 == "2":
                myhp = myhp + 5
                print "You are now guarding yourself. Your HP is now %d" %myhp; time.sleep(.5)
                myhp = myhp - dmg
                print "%s attacked you and did %d damage. Your HP fell down to %s" %(name, dmg, myhp);  time.sleep(.5)
            if opt1 == "3":
                chance = random.randint(0,11)
                if chance == 1:
                    print "You have sucessfully run away";  time.sleep(.5)
                else:
                    myhp = myhp - dmg
                    print "You failed to run away.";    time.sleep(.5)
                    print "%s attacked you and did %d damage. Your HP fell down to %s" %(name, dmg, myhp);  time.sleep(.5)
    if myhp <= 0 :
        print"\n\tYou have been defeated...";   time.sleep(.5)
        restart = ''
        allowed =["y", "n"]
        restart = restart.lower()
        restart = raw_input("\n\tWould you like to start over? ");  time.sleep(.5)
        if restart == "y":
            myhp = 20
            game1() 
    if hp <= 1 :
        global mgold
        print"You have defeated %s and earned %d gold." %(name, gold);  time.sleep(.5)
        mgold = mgold + gold
        print"You have %d gold." %mgold;    time.sleep(.5)


def fightmode0():
    print """\n\nThis is your first fight. You have 3 seconds each turn
if you fail to make a move in 3 seconds, you will lose your turn.

By "Attacking", you inflict damage on the enemy\'s HP, get it down to 0 to defeat him.
If your HP reaches 0, you will be defeated. 

By "Guarding", you will regain 10HP back, but that counts as your turn.
By defeating enemies, you gain gold Use gold to purchase upgrades 
when you come across a shop.

You can choose to "Run Away", but you will only have a 1/10 chance of it being sucessful
"""
    raw_input("\nPress any key to continue to battle"); time.sleep(.5)
    fightmode("Scrawny Thug", 15, 1, 5, 4)

def game1():
    print "\nYou wake up and find yourself locked in a room...";    time.sleep(.5)
    print "You think you're kidnapped.";    time.sleep(.5)
    print "Yea, you're probably kidnapped.";    time.sleep(.5)
    print "You hear footsteps approaching the door..."; time.sleep(.5)
    print "\n\t1. Remain in fetal position \n\t2. Attempt a sneak attack" ; time.sleep(.5)
    choice1 = ''
    allowed = ["1", "2"]
    while choice1 not in allowed:
        choice1 = raw_input("\nWhat will you do? ");    time.sleep(.5)
    print "\nThe doorknob rattles...";  time.sleep(.5)
    print "...";    time.sleep(.5)
    print "...";    time.sleep(.5)
    if choice1 == "1":
        print '"Hey!"'; time.sleep(.7)
        print '"Get up maggot!"';   time.sleep(.7)
        print 'You see that the thug is small and scrawny'; time.sleep(1)
        print 'He grabs you by your hair and pulls you up'; time.sleep(1)
        print "\n\t1. Punch him in the face. \n\t2. Do nothing" ;   time.sleep(1)
        choice1_1 = ''
        allowed = ["1", "2",]
        while choice1_1 not in allowed:
            choice1_1= raw_input("\nWhat will you do?? ");  time.sleep(.5)
        if choice1_1 == "1":
            print '\nYou punch the scrawny thug and he lets you go';    time.sleep(.5)
            print '"You\'re going to pay for that."';   time.sleep(.5)
            print '\n\t\t>>>>>>>>>>ENTERING FIGHT MODE';    time.sleep(.5)
            print '\n\t\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>';    time.sleep(.5)
            print '\n\t\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>';    time.sleep(.5)
            print '\n\t\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>';    time.sleep(.5)
            print '\n\t\t>>>>>>>>>>>>>>>>>>>>>>>>>>>>>';    time.sleep(.5)
            fightmode0()



game1()

最佳答案

在调用 game1() 的底部,添加对 game2() 的调用,等等:

game1()
game2()
game3()

关于Python 使用一个函数循环到其他函数。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12613248/

相关文章:

python - 了解 Python 中的文件迭代

java - Android自定义数字时钟,while循环运行出错 "Unhandled exception type InterruptedException"

python - 如何从 Python 中的数据框列元素创建字符串?

python - Matplotlib:更改索引中某个点之后线条的颜色

c - 仅使用循环来区分两个字符串

ruby - Kernel.loop 方法需要 'do' 。不允许使用分号?

PHP 循环通过 GPX 来计算赛道的总距离

jquery - 如何使用 jQuery 对元素列表进行动画处理?

python - 如何将 X 分钟、Y 秒字符串转换为时间戳

python - 判断两个字典是否相等Python错误