from random import randint
class Item(object):
def __init__(self,name,hp,atk,armor,slot):
self.hp = hp
self.name = name
self.atk = atk
self.armor = armor
self.slot = slot
def getName(self):
return self.name
class Human(object):
def __init__(self,name,hp,atk,armor,weapon=Item(None,0,0,0,"weapon"),\
shield=Item(None,0,0,0,"shield")):
self.maxHP = hp
self.curHP = hp
self.name = name
self.atk = atk
self.armor = armor
self.weapon = weapon
self.shield = shield
def equip(self,Item):
if Item.slot == "weapon":
self.atk -= self.weapon.atk
self.maxHP -= self.weapon.hp
self.armor -= self.weapon.armor
self.atk += Item.atk
self.maxHP += Item.hp
self.armor += Item.armor
self.weapon = Item
if Item.slot == "shield":
self.atk -= self.shield.atk
self.maxHP -= self.shield.hp
self.armor -= self.shield.armor
self.atk += Item.atk
self.maxHP += Item.hp
self.armor += Item.armor
self.shield = Item
if self.curHP > self.maxHP:
self.curHP = self.maxHP
def gainHP(self,potation):
self.curHP += potation.hp
if self.curHP > self.maxHP:
self.curHP = self.maxHP
def loseHP(self,point):
self.curHP -= point
def info(self):
print "Name: ",self.name
print "HP: ",self.curHP,"/",self.maxHP
print "Atk: ",self.atk
print "Def: ",self.armor
print "weapon:",self.weapon.getName()
print "shield:",self.shield.getName()
class potation(object):
def __init__(self,hp):
self.hp = hp
class GameMap(object):
def __init__(self):
self.GameMap=dict()
def setBlock(self,event,index):
self.GameMap[str(index)]=event
def get(self,index):
return self.GameMap[index]
def buildMap(size):
'''
build map
'''
game = GameMap()
sysMap = [0 for x in range(0,size)]
for i in range(0,size):
opt = randint(1,10)
if opt <= 3:
'''
healing pot
'''
game.setBlock(potation(randint(20,60)),i)
sysMap[i]="Potation"
elif opt <= 5:
'''
gear
'''
wos = randint(1,2)
if wos == 1:
dmg = randint(30,80)
name = "swordType_"+str(dmg)
game.setBlock(Item(name,0,dmg,0,"weapon"),i)
sysMap[i]="Weapon"
else:
armor = randint(5,20)
hp = randint(10,40)
name = "ShieldType_"+str(armor)
game.setBlock(Item(name,hp,0,armor,"shield"),i)
sysMap[i]="Shield"
elif opt <= 9:
'''
enemy
'''
dmg = randint(50,70)
hp = randint(90,140)
armor = randint(20,30)
game.setBlock(Human("enemy",hp,dmg,armor),i)
sysMap[i]="Enemy"
else:
'''
boss
'''
dmg = randint(60,80)
hp = randint(190,420)
armor = randint(20,40)
game.setBlock(Human("boss",hp,dmg,armor),i)
sysMap[i]="Boss"
return game,sysMap
def Play(size):
game,sysMap = buildMap(size)
roomList = [str(x) for x in range(size)]
name = raw_input("hero's name: ")
sword = Item("Hero's sword",5,5,5,"weapon")
shield = Item("Hero's shield",5,5,5,"shield")
hero = Human(name,200,50,35)
hero.equip(sword)
hero.equip(shield)
while True:
if len(roomList) == 0:
print ""
print "Victory, ",name," has cleared the dungeon!"
break
print ""
print "Status: "
hero.info()
print "Room: ",' '.join(roomList)
room = int(raw_input('Choose a room: '))
if str(room) not in roomList:
print "Room not exist or already visited"
else:
print "Your hero met ",sysMap[room]," in Room: ",room
hero = visit(hero,game.get(str(room)),sysMap[room])
roomList.remove(str(room))
if hero.curHP <= 0:
print "Hero, ",name," is dead."
break
print "End."
def visit(hero, event, eventType):
if eventType == "Potation":
hero.gainHP(event)
print hero.name," is healed for ",event.hp,"HP!"
elif eventType == "Weapon":
print "You found a sword!"
print "If you replace with this new sword, you will..."
print "HP: ",(event.hp - hero.weapon.hp)
print "Atk:",(event.atk - hero.weapon.atk)
print "Def:",(event.armor - hero.weapon.armor)
opt = raw_input('Enter r for replacement, enter others for pass: ')
if opt == "r":
hero.equip(event)
else:
print "You pass the loot."
elif eventType == "Shield":
print "You found a shield!"
print "If you replace with this new shield, you will..."
print "HP: ",(event.hp - hero.shield.hp)
print "Atk: ",(event.atk - hero.shield.atk)
print "Def: ",(event.armor - hero.shield.armor)
opt = raw_input('Enter r for replacement, enter others for pass: ')
if opt == "r":
hero.equip(event)
else:
print "You pass the loot."
elif eventType == "Enemy" or eventType == "Boss":
count = 0
multi = 1
while hero.curHP>0 and event.curHP>0:
if count >= 7:
print event.name," is enraged!"
multi = multi*2
count = 0
heroDmg = hero.atk - (event.armor * multi)
enemyDmg = (event.atk * multi) - hero.armor
if heroDmg < 0:
heroDmg = 1
if enemyDmg < 0:
enemyDmg = 1
hero.loseHP(enemyDmg)
print hero.name," takes ",enemyDmg," from ",event.name,
print ",",hero.name," has ",hero.curHP," HP left."
event.loseHP(heroDmg)
print event.name," takes ",heroDmg," from ",hero.name,
print ",",event.name," has ",event.curHP," HP left."
if event.curHP <= 0:
print event.name,"is dead!"
print hero.name," win!"
elif hero.curHP <= 0:
print hero.name," is dead!"
count+=1
return hero
评论0