在python中使用健康栏组织多个切换字符

时间:2017-07-25 23:54:31

标签: python python-2.7

我正在尝试制作一个2人游戏的基于文本的口袋妖怪战斗复制品,其中有多个口袋妖怪,可以切换出来并拥有自己的健康和动作。

我已经成功地制作了一个类似的剧本,其中一个人与计算机玩的计算机玩家基本上什么都不做,每个玩家也只有一个小精灵。现在我试图扩展它,使两个人可以在同一台设备上互相争斗,玩热门座位的风格。

我遇到的问题是我还没有找到一种方法来组织和回调每个个人口袋妖怪,而玩家可以切换哪一个是活跃的口袋妖怪。我尝试过使用词典,但是当我想要破坏口袋妖怪时它们并不是很有用,因为在使用它们时我似乎无法改变每个健康栏中的值。我还没有尝试为每个个人口袋妖怪制作一本字典,每个玩家只需要一个字典。

但是,拥有单独词典的问题在于,我需要一种方法来告诉每个团队留下多少个口袋妖怪。每个团队有三个,当一个团队完全被消灭时,战斗就结束了。

任何帮助将不胜感激。我将离开原来的玩家VS.这里有计算机脚本供参考:

import time
Hm=150
Hr=60
TackleMove=50
ScratchMove=40
BiteMove=60
#Intro
print 'Difficulty? Type "Easy", "Normal", "Hard"'
EsMo = input("Answer: ")   
print "A wild Magikarp appeared!"
time.sleep(0.5)
print "Go! Rattata!"
time.sleep(0.5)

#Using a move
def PlayerTurn():
    print 'Moves are: "Tackle", "Scratch", "HyperFang", "Bite"'
    time.sleep(0.5)
    Move = input("Use: ")
    #Tackle
    if Move is "Tackle":
        print "Rattata used Tackle!"
        global Hm
        Hm-=TackleMove
        time.sleep(0.5)
        if EsMo is "Hard":
            pass
        elif EsMo is "Easy" or "Normal":
            print "Magikarp has", Hm, "HP Left"

    #Scratch
    elif Move is "Scratch":
        print "Rattata used Scratch!"
        Hm-=ScratchMove
        time.sleep(0.5)
        if EsMo is "Hard":
            pass
        elif EsMo is "Easy" or "Normal":
            print "Magikarp has", Hm, "HP Left"

    #HyperFang
    elif Move is "HyperFang":
        print "Rattata used HyperFang!"
        Hm/=2
        time.sleep(0.5)
        if EsMo is "Hard":
            pass
        elif EsMo is "Easy" or "Normal":
            print "Magikarp has", Hm, "HP Left"

    #Bite
    elif Move is "Bite":
        print "Rattata used Bite!"
        Hm-=BiteMove
        time.sleep(0.5)
        if EsMo is "Hard":
            pass
        elif EsMo is "Easy" or "Normal":
            print "Magikarp has", Hm, "HP Left"

    #Cheat
    elif Move is "Cheat":
        print "CHEATER!"
        Hm-=Hm
        print "Magikarp has", Hm, "HP Left"
    else:
        print "Oops"
    return

#AI Turn
def AITurn():
    if Hm>50 or EsMo is "Easy":
        print "Magikarp used Splash!"
        time.sleep(1)
        print "But nothing happened..."
    elif Hm<=50 and EsMo is "Normal" or "Hard":
        print "Magikarp used Tackle!"
        global Hr
        Hr-=TackleMove
        time.sleep(1)
        print "Rattata has", Hr, "HP Left"
    else:
        print "Oops"
    return

#Full Loop
while Hm>0:
    #Action Choice
    print 'Actions are: "Fight", "Pokeball", "Run"'
    time.sleep(0.5)
    Action = input("What do you want to do?: ")
    time.sleep(0.5)
    #If Choose Fight
    if Action is "Fight":
        PlayerTurn()
        if Hm<=0:
            time.sleep(1)
            print "Magikarp Fainted!"
            time.sleep(1)
            print "You Win!"
            break
        time.sleep(0.5)
        AITurn()
        if Hm<=0:
            time.sleep(1)
            print "Magikarp Fainted!"
            time.sleep(1)
            print "You Win!"
            break
        elif Hr<=0:
            time.sleep(1)
            print "Rattata Fainted!"
            time.sleep(1.5)
            print "You Lost!"
            break
        time.sleep(0.5)
        continue
    #If Choose Run
    if Action is "Run":
        print "Ran away Safley!"
        break
    #If Choose Pokeball
    if Action is "Pokeball":
        time.sleep(0.5)
        print "You threw the Pokeball!"
        time.sleep(1)
        if Hm>100:
            print "..."
            time.sleep(1)
            print "Darn! It Broke Free!"
        elif 50<Hm<=100:
            print "..."
            time.sleep(1)
            print "..."
            time.sleep(1)
            print "Almost had it!"
        elif 30<Hm<=50:
            print "..."
            time.sleep(1)
            print "..."
            time.sleep(1)
            print "..."
            time.sleep(1)
            print "Shoot! So Close!"
        elif Hm<=30:
            print "..."
            time.sleep(1)
            print "..."
            time.sleep(1)
            print "..."
            time.sleep(1)
            print "You Caught a Magikarp!"
            time.sleep(1.5)
            print "Congratulations!"
            break
        else:
            print "Oops"
        time.sleep(1)
        AITurn()
        if Hm<=0:
            time.sleep(1)
            print "Magikarp Fainted!"
            time.sleep(1)
            print "You Win!"
            break
        elif Hr<=0:
            time.sleep(1)
            print "Rattata Fainted!"
            time.sleep(1.5)
            print "You Lost!"
            break
        time.sleep(0.5)
        continue
time.sleep(2)
print "Please, try again!"
#End

我目前的代码有两种形式,现在仍在制定中,所以我有些东西比其他东西更完整。在这种情况下,我只填写了Rattata的东西,而我正在测试的那一步是Scratch。

import time
P1Rat=50
P1Abra=100
P1Pidgey=100
P2Rat=50
P2Abra=100
P2Pidgey=100
Play1PokeSt=["Rattata", "Abra", "Pidgey"]
ActPl1Poke=[]
ActPl1PokeSt=[]
Play2PokeSt=["Rattata", "Abra", "Pidgey"]
ActPl2Poke=[]
ActPl2PokeSt=[]

#Player 1's Rattata Attacks
def Rattata1():
    global ActPl2Poke
    print 'Moves are: "Tackle", "Scratch", "HyperFang", "Bite"'
    time.sleep(0.5)
    R1Move = input("Use: ")
    #Tackle
    if R1Move is "Tackle":
        print "Rattata used Tackle!"
        ActPl2Poke[0]-=50

    #Scratch
    elif R1Move is "Scratch":
        print "Rattata used Scratch!"
        ActPl2Poke[0]-=40
        print ActPl2Poke[:]
        print P2Rat

    #HyperFang
    elif R1Move is "HyperFang":
        print "Rattata used HyperFang!"
        ActPl2Poke[0]/=2

    #Bite
    elif R1Move is "Bite":
        print "Rattata used Bite!"
        ActPl2Poke[0]-=60

def Play1Sel():
    while True:
        print "What Pokemon do you want to use?"
        print "Remaining Pokemon are:"
        print ", ".join(Play1PokeSt)
        Pokemon1=input()
        if Pokemon1 is "Rattata" and "Rattata" in Play1PokeSt:
            del ActPl1Poke[:]
            ActPl1Poke.append(int(P1Rat))
            del ActPl1PokeSt[:]
            ActPl1PokeSt.append("Rattata")
            break
        elif Pokemon1 is "Rattata" and "Rattata" not in Play1PokeSt:
            print "Rattata has fainted! Choose another!"
            continue
        if Pokemon1 is "Abra" and "Abra" in Play1PokeSt:
            del ActPl1Poke[:]
            ActPl1Poke.append(P1Abra)
            del ActPl1PokeSt[:]
            ActPl1PokeSt.append("Abra")
            break
        elif Pokemon1 is "Abra" and "Abra" not in Play1PokeSt:
            print "Abra has fainted! Choose another!"
            continue
        if Pokemon1 is "Pidgey" and "Pidgey" in Play1PokeSt:
            del ActPl1Poke[:]
            ActPl1Poke.append(P1Pidgey)
            del ActPl1PokeSt[:]
            ActPl1PokeSt.append("Pidgey")
            break
        elif Pokemon1 is "Pidgey" and "Pidgey" not in Play1PokeSt:
            print "Pidgey has fainted! Choose another!"
            continue


def Play2Sel():
    while True:
        print "What Pokemon do you want to use?"
        print "Remaining Pokemon are:"
        print ", ".join(Play2PokeSt)
        Pokemon2=input()
        if Pokemon2 is "Rattata" and "Rattata" in Play2PokeSt:
            del ActPl2Poke[:]
            ActPl2Poke.append(P2Rat)
            del ActPl2PokeSt[:]
            ActPl2PokeSt.append("Rattata")
            break
        elif Pokemon2 is "Rattata" and "Rattata" not in Play2PokeSt:
            print "Rattata has fainted! Choose another!"
            continue
        if Pokemon2 is "Abra" and "Abra" in Play2PokeSt:
            del ActPl2Poke[:]
            ActPl2Poke.append(P2Abra)
            del ActPl2PokeSt[:]
            ActPl2PokeSt.append("Abra")
            break
        elif Pokemon2 is "Abra" and "Abra" not in Play2PokeSt:
            print "Abra has fainted! Choose another!"
            continue
        if Pokemon2 is "Pidgey" and "Pidgey" in Play2PokeSt:
            del ActPl2Poke[:]
            ActPl2Poke.append(P2Pidgey)
            del ActPl2PokeSt[:]
            ActPl2PokeSt.append("Pidgey")
            break
        elif Pokemon2 is "Pidgey" and "Pidgey" not in Play2PokeSt:
            print "Pidgey has fainted! Choose another!"
            continue
#Player 2's Rattata Attacks
def Rattata2():
    global ActPl2Poke
    print 'Moves are: "Tackle", "Scratch", "HyperFang", "Bite"'
    time.sleep(0.5)
    R2Move = input("Use: ")
    #Tackle
    if R2Move is "Tackle":
        print "Rattata used Tackle!"
        ActPl1Poke[0]-=50

    #Scratch
    elif R2Move is "Scratch":
        print "Rattata used Scratch!"
        ActPl1Poke[0]-=40
        print P1Rat


    #HyperFang
    elif R2Move is "HyperFang":
        print "Rattata used HyperFang!"
        ActPl1Poke[0]/=2

    #Bite
    elif R2Move is "Bite":
        print "Rattata used Bite!"
        ActPl1Poke[0]-=60        

#Player 1's Turn
def Play1():
    print "Actions are: Fight, Change"
    time.sleep(0.5)
    Act1=input("What do you want to do?: ")
    if Act1 is "Fight" and str(*ActPl1PokeSt) is "Rattata":
        Rattata1()
    elif Act1 is "Change":
        Play1Sel()
    else:
        print "oops"


#Player 2's Turn
def Play2():
    print "Actions are: Fight"
    time.sleep(0.5)
    Act2=input("What do you want to do?: ")
    if Act2 is "Fight" and str(*ActPl2PokeSt) is "Rattata":
        Rattata2()
    else:
        print "oops"

#Whole Loop
print "Player 1! Select Your Pokemon!"
Play1Sel()
print ActPl1Poke
print "Player 2! Select Your Pokemon!"
Play2Sel()
print ActPl2Poke
while True:
    print "Player 1, your turn."
    Play1()
    if P1Rat <=0 or P2Rat <=0:
        print "Finish"
        break
    else:
        print "Player 2, your turn."
        Play2()
        if P1Rat <=0 or P2Rat <=0:
            print "Finish"
            break
        continue

然后这是另一个版本:

import time

Poke1={"Rattata" : 100 , "Abra" : 100 , "Pidgey" : 100}
Poke2={"Rattata" : 100 , "Abra" : 100 , "Pidgey" : 100}
ActPo1={}
ActPo2={}

#Player 1's Rattata's Moves
def RattataAtk1():
    print 'Moves are: "Tackle", "Scratch", "HyperFang", "Bite"'
    time.sleep(0.5)
    MoveR1 = input("Use: ")
    #Tackle
    if MoveR1 is "Tackle":
        print "Rattata used Tackle!"
        ActPo2-=50

    #Scratch
    elif MoveR1 is "Scratch":
        print "Rattata used Scratch!"
        ActPo2-=40

    #HyperFang
    elif MoveR1 is "HyperFang":
        print "Rattata used HyperFang!"
        ActPo2/=2   

    #Bite
    elif MoveR1 is "Bite":
        print "Rattata used Bite!"
        ActPo2-=60

#Player 2's Rattata's Moves
def RattataAtk2():
    print 'Moves are: "Tackle", "Scratch", "HyperFang", "Bite"'
    time.sleep(0.5)
    MoveR2 = input("Use: ")
    #Tackle
    if MoveR2 is "Tackle":
        print "Rattata used Tackle!"
        ActPo1-=50

    #Scratch
    elif MoveR2 is "Scratch":
        print "Rattata used Scratch!"
        ActPo1-=40

    #HyperFang
    elif MoveR2 is "HyperFang":
        print "Rattata used HyperFang!"
        ActPo1/=2   

    #Bite
    elif MoveR2 is "Bite":
        print "Rattata used Bite!"
        ActPo1-=60


#Selection of Player 1's Pokemon
def Play1Sel():
    while True:
        print "What Pokemon do you want to use?"
        print "Remaining Pokemon are:"
        print Poke1.keys()
        Choose1=input()
        if Choose1 is "Rattata" and "Rattata" in Poke1:
            ActPo1["Rattata"]=Poke1["Rattata"]
            break
        else:
            print "Oops"
            break

#Selection of Player 2's Pokemon
def Play2Sel():
    while True:
        print "What Pokemon do you want to use?"
        print "Remaining Pokemon are:"
        print Poke2.keys()
        Choose2=input()
        if Choose2 is "Rattata" and "Rattata" in Poke2:
            ActPo2["Rattata"]=Poke2["Rattata"]
            break
        else:
            print "Oops"
            break

#Player 1's Choice of Action
def Play1():
    print "Actions are: Fight, Change"
    time.sleep(0.5)
    Act1=input("What do you want to do?: ")
    if Act1 is "Fight" and ActPo1.has_key("Rattata") is True:
        RattataAtk1()
    else:
        print "Oops"

#Player 2's Choice of Action
def Play2():
    print "Actions are: Fight, Change"
    time.sleep(0.5)
    Act2=input("What do you want to do?: ")
    if Act2 is "Fight" and ActPo2.has_key("Rattata") is True:
        RattataAtk2()
    else:
        print "Oops"

#Full Loop
print "Player 1! Select Your Pokemon!"
Play1Sel()
print ActPo1.items()
print "Player 2! Select Your Pokemon!"
Play2Sel()
print ActPo2.items()
while True:
    print "Player 1, your turn."
    Play1()
    if ActPo1.values()is 0:
        print "Finish"
        break
    else:
        print "Player 2, your turn."
        Play2()
        if ActPo2.values()is 0:
            print "Finish"
            break
        continue

1 个答案:

答案 0 :(得分:0)

语法或功能错误

我很快浏览了你的代码:

ActPo2-=50:你不能在字典上做数学。我想你的意思是

current_pokemon = "Rattata"
ActPo2[current_pokemon] -= 50

is:此运算符不会按您的想法执行操作,它会比较内存中的值。

h = "hello"
h is "hello" # False 
h == "hello" # True

此外,ActPo1.values() is 0永远不会成立,因为ActPo1.values()会返回一个列表。你可能意思是len(ActPo1)