凡人kombat控制台游戏python2

时间:2017-12-21 23:39:52

标签: python python-2.7

我只是尝试制作一款可以在控制台上运行的格斗游戏。它几乎已经完成,但我有2个问题。

  1. 当用户的hp减少到1时,游戏应该结束。当hp为0时,它不会完成。它给其他用户一个机会。

  2. 另外一个是,在比赛结束后,我想问用户“想再玩一次?”如果用户说“是”,游戏必须先开始而不是抛硬币。

  3. 如何快速解决这些问题?我的错在哪里?

    import random
    import time
    
    
    while True:    
        first_heros_name = raw_input("----- First Hero ----- \n Please type your hero's name: ")
        if len(first_heros_name) > 1:
            print ("First hero's name is", first_heros_name.capitalize())
            break
        else:
            print "Length of hero's name must be longer than 1 character."
    
    while True:    
        second_heros_name = raw_input("----- Second Hero ----- \n Please type your hero's name: ")
        if len(second_heros_name) <= 1:
            print "Length of hero's name must be longer than 1 character."        
        elif second_heros_name == first_heros_name:
            print first_heros_name, "is taken, please choose another name!"
    
        else:
            print("Second hero's name is"), second_heros_name.capitalize()
            break
    
    players_list = [first_heros_name, second_heros_name]
    coin_toss = random.choice(players_list)
    players_list.remove(coin_toss)
    print "Coin toss result: %s starts first!" %coin_toss #Coin toss result
    print "The game begins in 5 seconds!" #countdown
    ##time.sleep(1)
    ##print "---4---"
    ##time.sleep(1)
    ##print "---3---"
    ##time.sleep(1)
    ##print "---2---"
    ##time.sleep(1)
    ##print "---1---"
    ##time.sleep(1)
    
    
    
    def attack1(current_hp):
        hp2=current_hp
        chance_of_damaging=random.randint(0,100)
        print "--------------- %s Attacks !! ---------------"%coin_toss
        while True:
            attack_magnitute=input("Choose your attack magnitude between 1 and 50: ")
    
            if attack_magnitute > 50:
                print "The attack magnitude must be between 1 and 50."
    
            elif attack_magnitute < 1:
                print "The attack magnitude must be between 1 and 50."
            else:
                break
    
    
        while True:
            if chance_of_damaging > attack_magnitute:
                print coin_toss, " hits %s damage!!"%attack_magnitute
                hp2=hp2-attack_magnitute
                return hp2
            else:
                print "Ooopsy! %s missed the attack!"%coin_toss
                return hp2
    
    def attack2(current_hp):
        hp1=current_hp
        chance_of_damaging=random.randint(0,100)
        print "--------------- %s Attacks !! ---------------"%players_list
        while True:
            attack_magnitute=input("Choose your attack magnitude between 1 and 50: ")
    
            if attack_magnitute > 50:
                print "The attack magnitude must be between 1 and 50."
    
            elif attack_magnitute < 1:
                print "The attack magnitude must be between 1 and 50."
            else:
                break
    
    
        while True:
            if chance_of_damaging > attack_magnitute:
                print players_list, " hits %s damage!!"%attack_magnitute
                hp1=hp1-attack_magnitute
                return hp1
            else:
                print "Ooopsy! %s missed the attack!"%players_list
                return hp1
    
    def main():
        hp1, hp2 = (100,100)
        while hp2<=1:
            print players_list, " win"
            break
    
        while hp1 > 1 and hp2 > 1:
                hp1 = attack1(hp1)
                print coin_toss,"                                                                 ", players_list
    
                print "HP [%s]:"%hp2, hp2/2 * "|" ,"        ", "HP [%s]:"%hp1, hp1/2 * "|" 
                hp2 = attack2(hp2)
                print coin_toss,"                                                                 ", players_list
    
                print "HP [%s]:"%hp2, hp2/2 * "|" ,"        ", "HP [%s]:"%hp1, hp1/2 * "|"
    
        while hp1<=1:
            print coin_toss, " win"
            break
    
    
    
    
    
    main()
    

1 个答案:

答案 0 :(得分:0)

  1. 在第二位玩家移动之前,您必须先检查hp1
  2. 你必须把它放在while循环中,它会:hp设置为100,运行游戏,询问你是否想再玩一次。
  3. 我做了其他更改

    • attack1attack2几乎完全相同,因此我创建了一个带有更多参数的函数attack()
    • 你要求两个地方的大人物,所以我创建了函数ask_magnitute()
    • 您在两个地方显示结果,因此我创建了函数display()
    • 你在两个地方要求玩家,所以我创建了函数ask_name()

    包含其他更改的代码

    import random
    import time
    
    # --- functions ---
    
    def ask_for_magnitute():
        while True:
            value = input("Choose your attack magnitude between 1 and 50: ")
    
            if value > 50:
                print "The attack magnitude must be between 1 and 50."
            elif value < 1:
                print "The attack magnitude must be between 1 and 50."
            else:
                return value
    
    
    def attack(points, name):
    
        chance_of_damaging = random.randint(0, 100)
    
        print "---------------", name, "Attacks !! ---------------"
    
        attack_magnitute = ask_for_magnitute()
    
        if chance_of_damaging > attack_magnitute:
            print name, "hits", attack_magnitute, "damage!!"
            points -= attack_magnitute
        else:
            print "Ooopsy!", name, "missed the attack!"
    
        return points
    
    
    def display(points1, points2, name1, name2):
        print name1, "                                                                 ", name2
        print "HP [%s]:" % points2, points2/2 * "|" ,"        ",  # comma at the end and it doesn't add `"\n"`
        print "HP [%s]:" % points1, points1/2 * "|" 
    
    
    def ask_name(text, other_heros_list):
        while True:
            print '-----', text, 'Hero -----'
    
            name = raw_input("Please type your hero's name: ")
            name = name.capitalize()
    
            if len(name) <= 1:
                print "Length of hero's name must be longer than 1 character."        
            elif name in other_heros_list:
                print name, "is taken, please choose another name!"
            else:
                print text, "hero's name is", name
                break
    
        return name
    
    
    def main():
    
        players_list = []
    
        hero_name = ask_name("First", players_list)
        players_list.append(hero_name)
    
        hero_name = ask_name("Second", players_list)
        players_list.append(hero_name)
    
        random.shuffle(players_list)
    
        coin_toss = players_list[0]
        player    = players_list[1] 
    
        print "Coin toss result: %s starts first!" % coin_toss #Coin toss result
        print "The game begins in 5 seconds!" #countdown
    
        # --- loop ----
    
        running = True
    
        while running:
    
            # --- init --- 
    
            # (re)set points
            hp1, hp2 = (100, 100)
    
            # --- game ---
    
            while hp1 > 1 and hp2 > 1:
                hp1 = attack(hp1, coin_toss)
                display(hp1, hp2, coin_toss, player)
    
                if hp1 <= 1:
                    break
    
                hp2 = attack(hp2, player)
                display(hp1, hp2, coin_toss, player)
    
            # --- game over ---
    
            if hp1 <= 1:
                print coin_toss, " win"
            if hp2 <= 1:
                print player, " win"
    
            # --- play again ---
    
            answer = raw_input('Play again [Y/n]?').lower().strip()
    
            # exit only if `n` - other answers starts again  
            if answer == 'n':
                running = False
    
    # --- main ---
    
    main()