用python中的健康条组织多个切换字符

2024-06-30 12:53:28 发布

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

我试图使一个2人文本为基础的口袋妖怪战斗副本与多个口袋妖怪在它可以切换出来,有自己的健康和移动

我已经成功地制作了一个类似的脚本,其中一个人对抗电脑,而电脑播放器基本上什么也没做,每个播放器也只有一个口袋妖怪。现在我正在尝试扩展它,使之成为两个人可以在同一台设备上打对方,玩热座椅的风格

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

但有单独的字典的问题是,我需要一种方法来告诉多少口袋妖怪在每一个小组剩下。每队有三人,战斗结束时,一个队被完全消灭

任何帮助都将不胜感激。我将在这里留下我的原始播放器与计算机脚本供参考:

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

我目前的代码有两种形式,而且还在编写中,所以我有一些东西比其他东西更完整。在这个例子中,我只填写了ratata的内容,而我测试的是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

Tags: andiftimeissleepusedprintscratch
1条回答
网友
1楼 · 发布于 2024-06-30 12:53:28

语法或功能错误

我快速浏览了你的代码:

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)

相关问题 更多 >