将第一个玩家放在左侧Python2.7

2024-03-29 07:56:30 发布

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

你能给我一些提示或者帮我在python上写一个代码来显示左侧的起始播放器吗?事实上,有时候char1,有时候char2启动游戏,我怎么写一个代码,在左边显示启动游戏的char,直到游戏结束,在这个代码中它总是显示char1,我试图通过改变语句来做一些改变,但是每个回合都在改变char的顺序,事实上,我想留下来,例如,如果char2开始比赛,我想让他在左边显示,直到比赛结束。在代码的末尾是实际代码的输出。所以在这里的例子中,第二个玩家显示在右边,但我希望他显示在左边,直到游戏结束,因为他是开始游戏的玩家。 谢谢您。 尊敬的阿里夫元帅

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: 

Tags: andthegameinputrawifbetweenelse
1条回答
网友
1楼 · 发布于 2024-03-29 07:56:30

你的“应用程序”目前是硬编码的char1/char2序列,最好的办法是打破这个。一种方法是让char1和char2成为列表的成员。e、 g.characters= [char1, char2]因此,围绕“toss”的部分变成:

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

在那里你做的统计维护和打印部分将需要以类似的方式更新(所以你的健康分数也进入一个列表)。希望这有帮助。。。你知道吗

相关问题 更多 >