如何将我的游戏循环回s

2024-06-23 18:29:50 发布

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

我正在做一个简单的格斗游戏,如果你死了,我希望它循环到开始,如果你赢了第二阶段是解锁,他们造成更多的伤害,我已经到了一个点,你进入或不开始第二阶段,但它没有做任何事,完全卡住。这是我的密码。你知道吗

import random

xp = 0

level = 1

player1 = raw_input("player1 enter name")

enemyhealth = 100

playerhealth = 100

stage2 = "false"

difficulty = raw_input("choose difficulty, rookie,pro,master,legend or god")

if difficulty == "rookie":

    enemyhealth = enemyhealth - 15

if difficulty == "pro":

    enemyhealth = enemyhealth + 10

if difficulty == "master":

    enemyhealth = enemyhealth + 35

if difficulty == "legend":

    enemyhealth = enemyhealth + 40

if difficulty == "god":

    enemyhealth = enemyhealth + 60                                                             

print ("A quick tutorial: Make sure you type you actions with no spaces,  heres a list of the moves you can perform")


while(1):
 while enemyhealth >0:

    fight = raw_input("fight")

    if fight == "punch":

        print ("You punched the enemy")

        enemyhealth = enemyhealth - random.randint(5,10)

        xp = xp + 1

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (0,5)

        print "you now have..", playerhealth

    elif fight =="flyingkick":

        print "you flying kicked the enemy"

        enemyhealth = enemyhealth - random.randint(5,25)

        xp = xp + 1  

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!" 

        playerhealth = playerhealth - random.randint(5,15)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (0,5)

        print "you now have..", playerhealth

    elif fight =="uppercut":

        print "you uppercutted the enemy"

        enemyhealth = enemyhealth - random.randint(10,25)                                       

        xp = xp + 1  

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,15)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="rko":

        print "you rko'ed the enemy, out of nowhere!"

        enemyhealth = enemyhealth - random.randint(9,29)                                       

        xp = xp + 1  

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,12)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="kick":

        print "you kicked the enemy"

        enemyhealth = enemyhealth - random.randint(5,10)

        xp = xp + 1  

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="ninjastar":

        print "you ninjastarred the enemy"

        enemyhealth = enemyhealth - random.randint(5,20)

        xp = xp + 1  

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="headbutt":

        print "you headbutted the enemy"

        enemyhealth = enemyhealth - random.randint(5,18)

        xp = xp + 1  

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(5,10)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="deathray":

        print "you deathrayed the enemy"

        enemyhealth = enemyhealth - random.randint(1,50)

        xp = xp + 1  

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(1,30)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="stab":

        print "you stabbed the enemy"

        enemyhealth = enemyhealth - random.randint(3,40)

        xp = xp + 1  

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(2,20)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    elif fight =="shoot":

        print "you shot the enemy"

        enemyhealth = enemyhealth - random.randint(1,50)

        xp = xp + 1  

        print "the enemy now has", enemyhealth, "health"

        print "they fight back!"

        playerhealth = playerhealth - random.randint(1,30)

        if stage2 == "true":

            playerhealth = playerhealth - random.randit (1,5)

        print "you now have..", playerhealth

    if xp == 5:

       print "you leveled up! move unlocked: 'rko'"

    if xp == 7:
       print "you leveled up! move unlocked: 'ninjastar'"

    if xp == 10:
       print "you leveled up! move unlocked: 'headbutt'"

    if xp == 100:
       print " you reached the max level! move 'deathray' is now unlocked"

    if playerhealth <1:

        again = raw_input('you died! Play agian? [y/n]')
        if again is "y":
            next
        else:
            print('Thanks for playing!\n')
            exit()


    if enemyhealth < 1:


        again = raw_input('You Won! stage 2 is unlocked would you like to try and beat it?[y/n]:')

        if again is "y":
            next
        else:
            print('Thanks for playing!\n')
            exit()

        stage2 = "true"

else:

        None

Tags: theyoutrueifrandomnowxphas
2条回答

将程序放入while循环while true;
如果他们赢了,那么你break
如果没有,它就会继续循环

所以看起来

while true:
   (a bunch of code, all indented)  

当一个人赢了就加上break

试试这个:

# code for game
while playerhealth < 1:
    # code for game

相关问题 更多 >

    热门问题