Python 尝试为类提供两个参数的元组
所以我希望我的代码中的某些坐标有一个范围,这是我代码的一个片段Python 尝试为类提供两个参数的元组,python,Python,所以我希望我的代码中的某些坐标有一个范围,这是我代码的一个片段 enemy_spawns = [ (48, random.randint(470, 535)), (random.randint(920, 980), 26), (1842, random.randint(470, 535)), (random.randint(920, 980), 1034) 然后我想把它们分成一个类,分别是x和y class Enemy: def __init__(self, x, y,
enemy_spawns = [
(48, random.randint(470, 535)), (random.randint(920, 980), 26),
(1842, random.randint(470, 535)), (random.randint(920, 980), 1034)
然后我想把它们分成一个类,分别是x和y
class Enemy:
def __init__(self, x, y, speed):
但当我把它分配给类时,它就充当了一个参数
我试过这么做
enemy_coords = []
coords = random.choice(enemy_spawns)
enemy_coords.append(coords.split(','))
print(enemy_coords)
但是我不能拆分元组你好,祝你好运,
元组对象没有函数拆分如果要提取其中的一个成员,可以使用
coords[0]
split适用于string类如果要使用split,可以执行以下操作:
enemy_spawns = ["48 , " + str(randint(470, 535)), str(randint(980, 920)) + ", 26"]
enemy_spawns[0].split(",")
现在split()可以工作了为什么不做:
enemy_coords = random.choice(enemy_spawns)
并在init中将一个元组作为参数,然后从那里继续?您可以访问元组中的两个元素,它们的索引为coords[0]、coords[1]
敌方coords.append(coords)
??不清楚您试图做什么-最后一块中的代码没有任何部分与您的类有关。请澄清。因为我想要分开x和y,否则我将不得不改变很多东西。虽然我只是将x设置为敌方坐标[0]和敌方坐标[1],但这很好。谢谢你。