我想我自己陷入了俄罗斯娃娃。
我试图制作一个规模较小的口袋妖怪"引擎",并决定开展实施动作。一件事是,当我试图调动此举时,我卡住了。代码在这里(它不是很干净,大多数属性甚至还没有使用)
import math
import random
import time
class type (object):
def __init__(self, name, relat):
self.name = name
self.relat = relat
#define types
fire = type("fire", None)
normal = type("normal", None)
water = type("water", None)
grass = type("grass", None)
#add relationships
fire.relat = {fire: 0.5, normal: 1, water: 0.5, grass: 2}
normal.relat = {fire: 1, normal: 1, water: 1, grass: 1}
water.relat = {fire: 2, normal: 1, water: 0.5, grass: 0.5}
grass.relat = {fire: 0.5, normal: 1, water: 2, grass: 0.5}
#call the variable of fire relationship with water
print fire.relat[water]
class move (object):
def __init__(self, name, damage, type, ps):
self.name = name
self.damage = damage
self.type = type
self.ps = ps
tackle = move("Tackle", 5, normal, "physical")
searingburst = move("Searing Burst", 5, fire, "special")
pinesmack = move("Pine Smack", 5, grass, "special")
refresher = move("Refresher", 5, water, "special")
class Pokemon(object):
# Creates a Pokemon!
def __init__(self, speciesname, nickname, gender, type1, type2, chp, thp, exp, acc, moves):
self.speciesname = speciesname
self.nickname = nickname
self.gender = gender
self.type1 = type1
self.type2 = type2
self.chp = chp
self.thp = thp
self.exp = exp
self.damage = 2
self.bacc = 0
self.acc = acc
self.moves = moves
def displayhp(self):
print str(self.chp) + "/" + str(self.thp) + " HP"
def damageenemy(self, opponent):
if ((opponent.type1).upper() == 'GHOST'):
opponent.chp = opponent.chp
print("%s's attack passed through %s!" % (self.nickname, opponent.nickname))
opponent.displayhp()
else:
opponent.chp = opponent.chp - self.damage
opponent.displayhp()
#make a Pokemon by defining a variable = Pokemon(speciesname, nickname etc...)
treecko = Pokemon("Treecko", "Treecko", "male", grass, None, 20, 20, 0, 0, [tackle, pinesmack])
krabby = Pokemon("Krabby", "Krabby", "male", water, None, 20, 20, 0, 0, [tackle, refresher])
def choose(o1, o2):
o1r = input("What will %s do? " % o1.speciesname)
if o1r in o1.moves:
print o1r.moves
else:
print "no"
choose(treecko, krabby)
当前代码将访问move类;我需要的是移动的名称。因此,我不想检查移动列表中的对象处理,而是检查字符串" Tackle"在移动类中,在对象内的Moves列表内。
答案 0 :(得分:0)
我认为当你想要操作的选择对象时,一些技巧就足够了。
请不要使用type作为类名。它是内置函数的名称。
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import math
import random
import time
class myType (object):
def __init__(self, name, relat):
self.name = name
self.relat = relat
#define myTypes
fire = myType("fire", None)
normal = myType("normal", None)
water = myType("water", None)
grass = myType("grass", None)
#add relationships
fire.relat = {fire: 0.5, normal: 1, water: 0.5, grass: 2}
normal.relat = {fire: 1, normal: 1, water: 1, grass: 1}
water.relat = {fire: 2, normal: 1, water: 0.5, grass: 0.5}
grass.relat = {fire: 0.5, normal: 1, water: 2, grass: 0.5}
#call the variable of fire relationship with water
print fire.relat[water]
class move (object):
def __init__(self, name, damage, myType, ps):
self.name = name
self.damage = damage
self.myType = myType
self.ps = ps
tackle = move("Tackle", 5, normal, "physical")
searingburst = move("Searing Burst", 5, fire, "special")
pinesmack = move("Pine Smack", 5, grass, "special")
refresher = move("Refresher", 5, water, "special")
class Pokemon(object):
# Creates a Pokemon!
def __init__(self, speciesname, nickname, gender, myType1, myType2, chp, thp, exp, acc, moves):
self.speciesname = speciesname
self.nickname = nickname
self.gender = gender
self.myType1 = myType1
self.myType2 = myType2
self.chp = chp
self.thp = thp
self.exp = exp
self.damage = 2
self.bacc = 0
self.acc = acc
self.moves = moves
def displayhp(self):
print str(self.chp) + "/" + str(self.thp) + " HP"
def damageenemy(self, opponent):
if ((opponent.myType1).upper() == 'GHOST'):
opponent.chp = opponent.chp
print("%s's attack passed through %s!" % (self.nickname, opponent.nickname))
opponent.displayhp()
else:
opponent.chp = opponent.chp - self.damage
opponent.displayhp()
#make a Pokemon by defining a variable = Pokemon(speciesname, nickname etc...)
treecko = Pokemon("Treecko", "Treecko", "male", grass, None, 20, 20, 0, 0, [tackle, pinesmack])
krabby = Pokemon("Krabby", "Krabby", "male", water, None, 20, 20, 0, 0, [tackle, refresher])
def choose(o1, o2):
o1r = input("What will %s do? enter 1 for tackle, 2 for pinesmack: " % o1.speciesname)
print type(o1r)
if 1 == o1r:
print o1.moves[0]
elif 2 == o1r:
print o1.moves[1]
else:
assert 0, 'wrong input!'
# if o1r in o1.moves:
# print o1r.moves
# else:
# print "no"
choose(treecko, krabby)
告诉我它是否适合你。
答案 1 :(得分:0)
if o1r in (lambda move : move.name)(o1.moves):
print o1r.moves
是你想要的。这将取出移动的名称
答案 2 :(得分:0)
好的,这是我在评论中的意思: 在口袋妖怪类init函数中:
self.moves = {move.name:move for move in moves}
然后在你的选择功能中:
if o1r in o1.moves:
print 'Move {o} caused damage {1}'.format(o1r, o1.moves[o1r].damage)