将第一个玩家放在左侧的python 2.7上

时间:2017-12-21 16:48:48

标签: python python-2.7

你能给我一些提示或者帮我写一下python上的代码来显示左侧的起始玩家吗?实际上,有时char1和char2开始游戏,我怎么能写一个代码在左边显示开始游戏的char,直到游戏结束,这里的代码总是显示char1,我试图制作通过更改语句进行一些更改,但随后每个回合都在改变字符的顺序,实际上我想保留例如如果char2启动游戏我希望他在左侧显示直到游戏结束。在代码的最后是实际代码的输出。所以对于这里的例子,第二个玩家在右侧显示,但我希望他在左侧显示,直到游戏结束,因为他是开始游戏的玩家。 谢谢。 尊重Marshal Alife

import random

# ---

def game():

    char1 = raw_input("Player One: ")

    while char1 == "":
        char1 = raw_input("Please enter your name: ")

    char2 = raw_input("Player two: ")
    while char2 == char1:
        char2 = raw_input(char1 + " name is taken, please choose another name: ")
    while char2 == "":
        char2 = raw_input("Please enter your name: ")
    print char1, "and", char2, "welcome to the game."

    # ---

    health1 = health2 =100

    print char1,"[HP]"+"[%d]"%health1, '|' * (health1/2),
    print char2,"[HP]"+"[%d]"%health2, '|' * (health2/2)

    toss = random.randint(0, 1)

    if toss == 0:
        print char1, "will start the game"
    else:
        print char2, "will start the game"

    # ---

    while health1 > 0 and health2 > 0:
        if toss == 0:
            n = input(char1 + " select magnitude force between 1 and 50: ")
            if 50>=n>=1:
                if n > random.randint(1, 51):
                    print "you missed the chance"
                    print char1,"[HP]" + "[%d]" % health1, '|' * (health1 / 2), " " * (50 - health1 / 2),
                    print char2,"[HP]" + "[%d]" % health2, '|' * (health2 / 2), " " * (50 - health2 / 2)
                    toss= 1
                else:
                    health2 -= n
                    print char1,"[HP]" + "[%d]" % health1, '|' * (health1 / 2), " " * (50-health1/2),
                    print char2,"[HP]" + "[%d]" % health2, '|' * (health2 / 2), " " * (50-health2/2)
                    toss = 1 # change player
            else:
                print "n should be between 1 and 50"
        else:
            n = input(char2 + " select magnitude force between 1 and 50: ")
            if 50 >= n >= 1:
                if n > random.randint(1, 51):
                    print "you missed the chance"
                    print char1,"[HP]" + "[%d]" % health1, '|' * (health1 / 2), " " * (50 - health1 / 2),
                    print char2,"[HP]" + "[%d]" % health2, '|' * (health2 / 2)
                    toss= 0
                else:
                    health1 -= n
                    print char1,"[HP]" + "[%d]" % health1, '|' * (health1 / 2), " " * (50 - health1 / 2),
                    print char2,"[HP]" + "[%d]" % health2, '|' * (health2 / 2)
                    toss = 0 # change player
            else:
                print "n should be between 1 and 50"
    # ---

    if health1 > 0:
        print char1, 'wins'
    else:
         print char2, 'wins'

game()

    # ---

while True:
    c=raw_input("Do you want to continue Yes or No: ")
    if c=="Yes":
        print "Welcome to the game again"
        game()
    elif c=="No":
        print "Good bye"
        break #return game()

Player One: marshal
Player two: alife
marshal and alife welcome to the game.
marshal [HP][100] |||||||||||||||||||||||||||||||||||||||||||||||||| alife [HP][100] ||||||||||||||||||||||||||||||||||||||||||||||||||
alife will start the game
alife select magnitude force between 1 and 50: 10
you missed the chance
marshal [HP][100] ||||||||||||||||||||||||||||||||||||||||||||||||||  alife [HP][100] ||||||||||||||||||||||||||||||||||||||||||||||||||
marshal select magnitude force between 1 and 50: 

1 个答案:

答案 0 :(得分:0)

你的'app'目前在char1 / char2序列上是硬编码的,最好是打破它。一种方法是让char1和char2成为列表的成员。例如characters= [char1, char2]所以围绕'折腾'的部分变成:

first = toss second = 1 - toss print characters[first], "will start the game"

您需要以类似的方式更新执行统计数据维护和打印的部分(因此您的健康评分也会列入清单)。希望这会有所帮助...