My finest game. Write your own gett_butt() and enjoy. :)
Note:The pokemon and moves takes a long time to load. Also this won't work on Codewars, so you'll have to copy and paste it to someplace elsewhere.
Extra Note: De-comment line 215, delete line 217 and 233, then you'll want to take the indent on lines 218, 219, 224, and 225. I did this so it would pass the tests, but it prevents the game from running
import random
import time
class Write_your_own_code_here:
def __init__(self):
pass
def get_butt(self):#Should return "up", "dn", "lf", "rt" or "sl"
pass
btn = Write_your_own_code_here()
class pokemon:
def __init__(self, name, element, max_hp):
self.element = element
self.hp = max_hp
self.maxh = max_hp
self.moves = {}
self.Pname = name
def hurt(self,hrt):
self.hp -= int(hrt)
if self.hp <= 0: self.hp = 0
def element_pr(self,othrElmnt):
eldic = {"fire":"grass","grass":"water","water":"fire"}
if self.element == "normal" or othrElmnt == "normal": return 1
if othrElmnt == eldic[self.element]: return 2
return 0.5
def learn(self, name, element, power):
if (element == self.element or element == "normal") and not name in self.moves.keys():
self.moves[name] = power
return self.Pname + " learned " + name + "."
raise Exception("This does not match "+self.Pname+"'s element or "+self.Pname+" already knows this.")
def forget(self, name):
if not name in self.moves.keys(): raise Exception(self.Pname+" doesn't know this move.")
del self.moves[name]
return self.Pname+" forgot "+name+"."
def attack(self,target,move):
des = self.moves[move]*self.element_pr(target.element)
target.hurt(des)
return self.Pname + " has hit "+target.Pname+" for "+str(des)+" HP!" if target.hp>0 else self.Pname + " has hit "+target.Pname+" for "+str(des)+" HP! "+target.Pname+" has fainted..."
def restore(self, amount):
mrestore = self.maxh-self.hp
if amount>=mrestore:
self.hp += mrestore
return self.Pname+" has healed for "+str(mrestore)+" HP."
self.hp += amount
return self.Pname+" has healed for "+str(amount)+" HP."
btn = Write_your_own_code_here()
class Poke:#This is the game class.
def __init__(self,length):
self.length = length
self.p1 = []
self.p2 = []
self.wild = []
self.pmoves = {'fire':[],'water':[],'grass':[],'normal':[]}
print("Game is\n"+str(length)+" long")
def makeP(self):#Get the pokemon
name = ''.join([chr(random.randint(ord("a"),ord("z"))) for i in range(random.randint(4,10))])
ele = random.choice(['fire','grass','water','normal'])
if not name in [i.Pname for i in self.wild]:
self.wild.append(pokemon(name,ele,random.randint(50,300)))
def makeM(self):#Get the possible moves
elem = random.choice(["fire","grass","water","normal"])
dam = random.randint(1,10)*10
name = "".join([chr(random.randint(ord("a"),ord("z"))) for i in range(5)])
if not name in [i[0] for i in self.pmoves[elem]]:
self.pmoves[elem].append([name,dam])
def batTurn(self,plyr,othr,wld=False):#This is a single battle turn. plyr = The player who is doing the move, othr = The enemy player, wld = This it is a wild pokemons turn.
if wld:# If it is wild wait for get_butt() then choose a random move
la = btn.get_butt()
butt = random.choice(["dn","up","lf","rt"])
else:#Other wise get the players move. "dn" = Attack the other pokemon with one of your moves, "up" = Heal yourself, "rt" = Learn a move, "lf" = Make the other pokemon forget a move.
butt = btn.get_butt()
if butt == "dn":
try:
movea = random.choice([i for i in plyr.moves.keys()])
print(movea)
print(plyr.attack(othr,movea))
except IndexError:
print(plyr.Pname+" tried to attack.")
if butt == 'up':
print(plyr.restore(random.randint(1,4)*10))
if butt == "lf":
try:
print(othr.forget(random.choice([i for i in othr.moves.keys()])))
except IndexError:
print(othr.Pname+" doesn't know anything")
if butt == "rt":
ele = random.choice([plyr.element,"normal"])
movel = random.choice(self.pmoves[ele])
try:
print(plyr.learn(movel[0],ele,movel[1]))
except:
print(plyr.Pname+" couldn't learn.")
def battle(self):#A battle between players.
for i in range(random.randint(10,45)):#The battle can go on for a limited amout of turns.
print("ply1 "+str(self.p1[-1].hp)+str(self.p1[-1].element))
try:
self.batTurn(self.p1[-1],self.p2[-1])
except IndexError:
print("One of them is dead")
break
if self.p2[-1].hp == 0:
print("WINNER \nPLY1!!!")
self.p2.pop(-1)
break
print("ply2 "+str(self.p2[-1].hp)+str(self.p2[-1].element))
try:
self.batTurn(self.p2[-1],self.p1[-1])
except IndexError:#If you lose your last pokemon.
print("One of them are dead")
break
if self.p1[-1].hp == 0:
print("WINNER \nPLY2!!!")
self.p1.pop(-1)
break
def capture(self,p1):#Capture a wild pokemon. p1 = The player doing the capturing.
p2 = self.wild
l = random.randint(0,len(p2)-1)
if p2[-1].maxh < 20: p2[-1].maxh = random.randint(20,50)
for i in range(random.randint(10,45)):
print("plyr "+str(p1[-1].hp)+str(p1[-1].element))
self.batTurn(p1[-1],p2[l])
if p2[l].hp == 0:
print("WINNER \nPLYR!!!")
p1.append(p2[l])
p1[-1].hp = p1[-1].maxh
break
print("wild "+str(p2[l].hp)+str(p2[l].element))
self.batTurn(p2[l],p1[-1],wld=True)
if p1[-1].hp == 0:
print("WINNER \nWILD!!!")
p1.pop(-1)
break
def Turn(self,playerOne=True):#An entire turn. If playerOne is False it will skip player one's turn
if playerOne:
print("ply1")
butt = btn.get_butt()#If butt is: "sl": Shuffle your pokemon. Your pokemon that you use in battle will change, "lf": Try to capture a wild pokemon.,"rt":Battle with your opponent., "dn":This doesn't count as a move, but it shows your pokemon, their most powerful move and type,"up":Skip your turn.
if butt == "sl":
print("shuffle")
self.p1 = shuffle(self.p1)
if butt == "lf":
print("capture")
self.capture(self.p1)
if butt == "rt":
print("BATTLE!")
self.battle()
if butt == "dn":
self.show(self.p1)
self.Turn()
print("ply2")
butt = btn.get_butt()
if butt == "sl":
print("shuffle")
self.p2 = shuffle(self.p2)
if butt == "lf":
print("capture")
self.capture(self.p2)
if butt == "rt":
print("BATTLE!")
self.battle()
if butt == "dn":
self.show(self.p2)
self.Turn(playerOne=False)
return
def show(self,ply):
for i in range(len(ply)):
print(ply[i].Pname)
print(ply[i].element)
if len(ply[i].moves)>0: print(str(max([ply[i].moves[j] for j in ply[i].moves.keys()]))+",")
else: print(0)
time.sleep(1)
def shuffle(lst):
l = []
while len(lst)>0:
l.append(lst.pop(random.randint(0,len(lst)-1)))
return l
def game():#This runs the game
poke = Poke(random.randint(20,50))#Get the game
print("Loading...")
powL = []
for i in range(102):#Get the pokemon and moves
poke.makeP()
poke.makeM()
time.sleep(0.1)
for j in range(100):#Get more moves
poke.makeM()
time.sleep(0.1)
#Give each player a pokemon.
poke.p1.append(poke.wild.pop(0))#
poke.p2.append(poke.wild.pop(0))
for k in range(poke.length):#Game loop
poke.Turn(playerOne=True)#Do the turns
#See if someone has won by defeating all enemy pokemon
if len(poke.p1) == 0:
print("PLYR2\nWINNNS!!!")
break
if len(poke.p2) == 0:
print("PLYR1\nWINNNS!!!")
break
#Game is done, see who has the most pokemon. Else tie.
if len(poke.p1) > len(poke.p2): print(print("PLYR1\nWINNNS!!!"))
elif len(poke.p1) < len(poke.p2): print(print("PLYR2\nWINNNS!!!"))
else: print("TIE!")
#This returns all the pokemon
return poke.wild
butt = btn.get_butt()
print(random.choice(["Heads","Tails"]))#To decide who goes first.
#p = game()#Run the game
#This bit down here shows all the wild pokemon, their most powerful move, their health, their type, and what percent of pokemon our weaker than them.
try:
powL = []
for i in p:
try:
powL.append(i.hp+max([i[n] for n in i.moves.keys()]))
except:
powL.append(i.hp+5)
powL.sort()
for i in p:
print(i.Pname,i.element,i.hp)
try:
bigM = max([i[n] for n in i.moves.keys()])
print("MaxM",bigM,"%"+str(powL.index(i.hp+bigM)))
except:
print("MaxM 0","%"+str(powL.index(i.hp+5)))
time.sleep(5)
except: pass
import codewars_test as test
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)