Organizing multiple switching characters with health bars in python -
i trying make 2 player text-based pokemon battle replica multiple pokemon in can switched out , have own health , moves.
i have made similar script 1 person fought against computer computer player did nothing , there 1 pokemon per player. trying expand on making 2 people can fight each other on same device, playing hot seat style.
the problem i'm having have yet find way organize , call each individual pokemon while players switch around 1 active one. have tried using dictionaries, not useful when want damage pokemon, because cannot seem change value in each heath bar when using them. have not yet tried make dictionary each individual pokemon, 1 per player containing them.
but problem having individual dictionaries, need way tell how many pokemon left on each team. there 3 per team, , battle ends when 1 team wiped out.
any appreciated. i'll leave original player vs. computer script here reference:
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 move def playerturn(): print 'moves are: "tackle", "scratch", "hyperfang", "bite"' time.sleep(0.5) move = input("use: ") #tackle if move "tackle": print "rattata used tackle!" global hm hm-=tacklemove time.sleep(0.5) if esmo "hard": pass elif esmo "easy" or "normal": print "magikarp has", hm, "hp left" #scratch elif move "scratch": print "rattata used scratch!" hm-=scratchmove time.sleep(0.5) if esmo "hard": pass elif esmo "easy" or "normal": print "magikarp has", hm, "hp left" #hyperfang elif move "hyperfang": print "rattata used hyperfang!" hm/=2 time.sleep(0.5) if esmo "hard": pass elif esmo "easy" or "normal": print "magikarp has", hm, "hp left" #bite elif move "bite": print "rattata used bite!" hm-=bitemove time.sleep(0.5) if esmo "hard": pass elif esmo "easy" or "normal": print "magikarp has", hm, "hp left" #cheat elif move "cheat": print "cheater!" hm-=hm print "magikarp has", hm, "hp left" else: print "oops" return #ai turn def aiturn(): if hm>50 or esmo "easy": print "magikarp used splash!" time.sleep(1) print "but nothing happened..." elif hm<=50 , esmo "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 want do?: ") time.sleep(0.5) #if choose fight if action "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 "run": print "ran away safley!" break #if choose pokeball if action "pokeball": time.sleep(0.5) print "you threw pokeball!" time.sleep(1) if hm>100: print "..." time.sleep(1) print "darn! 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! close!" elif hm<=30: print "..." time.sleep(1) print "..." time.sleep(1) print "..." time.sleep(1) print "you caught 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
my current code in 2 forms , still being worked out have things more complete others. in instance, have rattata's stuff filled out, , move i'm testing 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 "tackle": print "rattata used tackle!" actpl2poke[0]-=50 #scratch elif r1move "scratch": print "rattata used scratch!" actpl2poke[0]-=40 print actpl2poke[:] print p2rat #hyperfang elif r1move "hyperfang": print "rattata used hyperfang!" actpl2poke[0]/=2 #bite elif r1move "bite": print "rattata used bite!" actpl2poke[0]-=60 def play1sel(): while true: print "what pokemon want use?" print "remaining pokemon are:" print ", ".join(play1pokest) pokemon1=input() if pokemon1 "rattata" , "rattata" in play1pokest: del actpl1poke[:] actpl1poke.append(int(p1rat)) del actpl1pokest[:] actpl1pokest.append("rattata") break elif pokemon1 "rattata" , "rattata" not in play1pokest: print "rattata has fainted! choose another!" continue if pokemon1 "abra" , "abra" in play1pokest: del actpl1poke[:] actpl1poke.append(p1abra) del actpl1pokest[:] actpl1pokest.append("abra") break elif pokemon1 "abra" , "abra" not in play1pokest: print "abra has fainted! choose another!" continue if pokemon1 "pidgey" , "pidgey" in play1pokest: del actpl1poke[:] actpl1poke.append(p1pidgey) del actpl1pokest[:] actpl1pokest.append("pidgey") break elif pokemon1 "pidgey" , "pidgey" not in play1pokest: print "pidgey has fainted! choose another!" continue def play2sel(): while true: print "what pokemon want use?" print "remaining pokemon are:" print ", ".join(play2pokest) pokemon2=input() if pokemon2 "rattata" , "rattata" in play2pokest: del actpl2poke[:] actpl2poke.append(p2rat) del actpl2pokest[:] actpl2pokest.append("rattata") break elif pokemon2 "rattata" , "rattata" not in play2pokest: print "rattata has fainted! choose another!" continue if pokemon2 "abra" , "abra" in play2pokest: del actpl2poke[:] actpl2poke.append(p2abra) del actpl2pokest[:] actpl2pokest.append("abra") break elif pokemon2 "abra" , "abra" not in play2pokest: print "abra has fainted! choose another!" continue if pokemon2 "pidgey" , "pidgey" in play2pokest: del actpl2poke[:] actpl2poke.append(p2pidgey) del actpl2pokest[:] actpl2pokest.append("pidgey") break elif pokemon2 "pidgey" , "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 "tackle": print "rattata used tackle!" actpl1poke[0]-=50 #scratch elif r2move "scratch": print "rattata used scratch!" actpl1poke[0]-=40 print p1rat #hyperfang elif r2move "hyperfang": print "rattata used hyperfang!" actpl1poke[0]/=2 #bite elif r2move "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 want do?: ") if act1 "fight" , str(*actpl1pokest) "rattata": rattata1() elif act1 "change": play1sel() else: print "oops" #player 2's turn def play2(): print "actions are: fight" time.sleep(0.5) act2=input("what want do?: ") if act2 "fight" , str(*actpl2pokest) "rattata": rattata2() else: print "oops" #whole loop print "player 1! select pokemon!" play1sel() print actpl1poke print "player 2! select pokemon!" play2sel() print actpl2poke while true: print "player 1, turn." play1() if p1rat <=0 or p2rat <=0: print "finish" break else: print "player 2, turn." play2() if p1rat <=0 or p2rat <=0: print "finish" break continue
and other version:
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 "tackle": print "rattata used tackle!" actpo2-=50 #scratch elif mover1 "scratch": print "rattata used scratch!" actpo2-=40 #hyperfang elif mover1 "hyperfang": print "rattata used hyperfang!" actpo2/=2 #bite elif mover1 "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 "tackle": print "rattata used tackle!" actpo1-=50 #scratch elif mover2 "scratch": print "rattata used scratch!" actpo1-=40 #hyperfang elif mover2 "hyperfang": print "rattata used hyperfang!" actpo1/=2 #bite elif mover2 "bite": print "rattata used bite!" actpo1-=60 #selection of player 1's pokemon def play1sel(): while true: print "what pokemon want use?" print "remaining pokemon are:" print poke1.keys() choose1=input() if choose1 "rattata" , "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 want use?" print "remaining pokemon are:" print poke2.keys() choose2=input() if choose2 "rattata" , "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 want do?: ") if act1 "fight" , actpo1.has_key("rattata") 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 want do?: ") if act2 "fight" , actpo2.has_key("rattata") true: rattataatk2() else: print "oops" #full loop print "player 1! select pokemon!" play1sel() print actpo1.items() print "player 2! select pokemon!" play2sel() print actpo2.items() while true: print "player 1, turn." play1() if actpo1.values()is 0: print "finish" break else: print "player 2, turn." play2() if actpo2.values()is 0: print "finish" break continue
syntax or functional errors
i skimmed code:
actpo2-=50
: can't math on dictionary. think mean
current_pokemon = "rattata" actpo2[current_pokemon] -= 50
is
: operator doesn't think, compares values in memory.
h = "hello" h "hello" # false h == "hello" # true
also, actpo1.values() 0
never true, because actpo1.values()
returns list. mean len(actpo1)
Comments
Post a Comment