我正在尝试创建一个基于文本的游戏,我需要将用户的输入转换为类的属性,我有2个不同的脚本,一个存储所有“能力”,一个做实际的“战斗”
脚本1
> #Abilities
import Nstats
class Ability():
def __init__(self,AP,Desc):
self.AP=int(AP)
self.Desc=Desc
SHP=Ability(10,"Super Hard Punch!")
SWP=Ability(32,"Literally kills you!!")
脚本2
>
def convert(inp):
cmnd2 = {'SHP':Abilities.SHP}
cmnd2[inp]
for inp in [Abilities.SHP,Abilities.SWP]:
inp = inp.AP
def Battle():
while Nstats.NHealth >= 0:
roll = randint(0,50)
print("Your Avaialbe Ability's are:", Nstats.NAbilities,)
inp = input("What would you like to use?")
if inp in Nstats.NAbilities:
convert(inp)
print(inp)
Battle()
我试图以某种方式将Input转换为值然后在算法中使用来计算出“损坏”,但我不知道如何实现这一点。
避免混淆NStats是一个带有此代码的简单文档
> Nlevel = 1
NHealth = 500
NMP = 500
NAbilities = ['SHP']
Ninventory = []
这是我常用的但无法获得理想的结果
inp = input("What would you like to use?")
cmnd = {'SHP':Abilities.SHP.AP,'SWP':Abilities.SWP}
if inp in Nstats.NAbilities:
cmnd[inp]
damage = int((inp*2)) + (roll*2)
答案 0 :(得分:0)
根据您的标题,我假设您正在尝试以编程方式获取或设置属性。您可以使用setattr(object, attr, value)
和getattr(object, attr)
以编程方式获取和设置属性:
>>> help(setattr)
setattr(obj, name, value, /)
Sets the named attribute on the given object to the specified value.
setattr(x, 'y', v) is equivalent to ``x.y = v''
>>> help(getattr):
getattr(...)
getattr(object, name[, default]) -> value
Get a named attribute from an object; getattr(x, 'y') is equivalent to x.y.
When a default argument is given, it is returned when the attribute doesn't
exist; without it, an exception is raised in that case.
答案 1 :(得分:0)
您可以将其用作开始/查找点:
import random
class Ability():
"""Holds data of attacks:
ap = cost, desc= description, damage = ap + randint(0,ap), key = key to use it"""
def __init__(self,ap,desc,key):
self.ap = int(ap)
self.desc = desc
self.key = key
def __str__(self, **kwargs):
"""Print one ability."""
return f' {self.key} -> {self.desc} for {self.ap} AP'
class NStats :
"""Stats of player/monster"""
def __init__(self, health:int, actionpoints:int):
self.health = health
self.ap = actionpoints
self.abilities = { "SHP" :Ability( 5,"Super Hard Punch", "SHP" ),
"WP" :Ability( 1,"Weak Punch", "WP" ),
"KAME" :Ability(50,"All Out Attack", "KAME" ),
"SA" :Ability(10,"Your Special Attack", "SA" ) }
def printAbs(self):
"""Print own abilities"""
for n in sorted(self.abilities, key = lambda x: self.abilities[x].ap):
print(str(self.abilities[n]))
def attack(self, other):
"""Attack other. TODO: other should retaliate. Currently you
die only by exausting all your ap."""
print("FIIIIIGHT")
while other.health > 0 and self.health > 0:
print(f'Rest Health: {self.health} Rest AP: {self.ap} vs {other.health}')
self.printAbs()
what = input("What to do...\n" ).upper()
# dict.get(key,default) - if key not in dict, it returns None
ab = self.abilities.get(what,None)
# only if attack found in dict AND ap are enough
if ab and ab.ap <= self.ap:
other.health -= ab.ap + random.randint(0,ab.ap) # damage of attack
self.ap -= ab.ap # cost of attack
# only if attack found in dict AND ap too low
elif ab and ab.ap > self.ap:
print("you are too exhausted and loose another 10 AP")
self.ap -= 10
# no else : typos simply query for new input
if other.health <= 0:
print("You win")
break
elif self.health <= 0 or self.ap <= 0:
print("You loose")
break
# lets fight, me vs you ;o)
Me = NStats(100,100)
You = NStats(100,5)
Me.attack(You)
输出:
FIIIIIGHT
Rest Health: 100 Rest AP: 100 vs 100
WP -> Weak Punch for 1 AP
SHP -> Super Hard Punch for 5 AP
SA -> Your Special Attack for 10 AP
KAME -> All Out Attack for 50 AP
What to do...
shp
Rest Health: 100 Rest AP: 95 vs 93
WP -> Weak Punch for 1 AP
SHP -> Super Hard Punch for 5 AP
SA -> Your Special Attack for 10 AP
KAME -> All Out Attack for 50 AP
What to do...
kame
Rest Health: 100 Rest AP: 45 vs 15
WP -> Weak Punch for 1 AP
SHP -> Super Hard Punch for 5 AP
SA -> Your Special Attack for 10 AP
KAME -> All Out Attack for 50 AP
What to do...
kame
you are too exhausted and loose another 10 AP
Rest Health: 100 Rest AP: 35 vs 15
WP -> Weak Punch for 1 AP
SHP -> Super Hard Punch for 5 AP
SA -> Your Special Attack for 10 AP
KAME -> All Out Attack for 50 AP
What to do...
sa
Rest Health: 100 Rest AP: 25 vs 2
WP -> Weak Punch for 1 AP
SHP -> Super Hard Punch for 5 AP
SA -> Your Special Attack for 10 AP
KAME -> All Out Attack for 50 AP
What to do...
sa
You win
如果你杀了你就赢了,如果你的AP是0或更低你就松了