简体   繁体   English

Python:创建一个类的许多实例

[英]Python: Creating many instances of a class

I have a class called Unit which is a generic unit in a game. 我有一个称为Unit的类,它是游戏中的通用单元。 An instance of this Unit can be Cavalry , Infantry etc. and each unit has coordinates which indicate its initial position on a grid. Unit一个实例可以是CavalryInfantry等,每个单位的坐标均指示其在网格上的初始位置。 I want to create many instances of this Unit class so as to populate the map. 我想创建这个Unit类的许多实例,以便填充地图。

Here is what I have: 这是我所拥有的:

#create units for team 1
#(self, name, x, y, char, color, move_range, atk_range, unit_size, morale, power, fatigue, team)

team_1_cavalry =[(Unit('Cavalry', i+20, 10, 'A', libtcod.yellow, 6, 1, 1000, 100, 1.5, 500, 1)) for i in range(11) ]
team_1_infantry = [(Unit('Heavy Infantry', i+20, 9, '=', libtcod.yellow, 3, 1, 2000, 100, 1.0, 200, 1)) for i in range(11)]
team_1_elephants = [(Unit('War Elephants', 20, 8, 'R', libtcod.yellow, 5, 1, 37, 100, 100, 800, 1)) for i in range(2)]

#create units for team 2
team_2_cavalry = [(Unit('Cavalry', 20+i, 15, 'A', libtcod.green, 6, 1, 500, 1000, 1.5, 500, 2)) for i in range (5)]
team_2_infantry = [(Unit('Roman Infantry', 20+i, 16, '=', libtcod.green, 3, 1, 2000, 100, 1.0, 200, 2)) for i in range(20)]

#the list of objects with those two
team1 = []
team2 = []
for unit in team_1_cavalry, team_1_infantry, team_1_elephants:
    team1.append(unit)
for unit in team_2_cavalry, team_2_infantry:
    team2.append(unit)

teams = [team1, team2]

What I'm trying to do is populate two lists with generated instances of the class. 我正在尝试用生成的类实例填充两个列表。 Then iterate over those lists to do various things to the instances therein. 然后遍历这些列表以对其中的实例执行各种操作。 However, whenever I try to do anything I get an error saying List object has no attribute [attribute] 但是,每当我尝试执行任何操作时,都会出现错误,指出List object has no attribute [attribute]

I am well aware that I should make each category of unit into a class which inherits from the unit class, I'm just wondering if that is absolutely necessary to make this work and why the way I'm doing it is not working. 我很清楚我应该将单元的每个类别都做成一个从单元类继承的类,我只是想知道这样做是否绝对必要,为什么我的工作方式不起作用。

Your problem is here: 您的问题在这里:

for unit in team_1_cavalry, team_1_infantry, team_1_elephants:

This is iterating over the tuple team_1_cavalry, team_1_infantry, team_1_elephants . 这是在元组team_1_cavalry, team_1_infantry, team_1_elephants So, first unit is the whole list team_1_cavalry , then it's the whole list team_1_infantry , then it's the whole list team_1_elephants . 因此,第一个unit是整个列表team_1_cavalry ,然后是整个列表team_1_infantry ,然后是整个列表team_1_elephants

What you want is one of the following: 您想要的是以下之一:

for unit_group in team_1_cavalry, team_1_infantry, team_1_elephants:
    for unit in unit_group:

for unit in team_1_cavalry + team_1_infantry + team_1_elephants:

for unit in itertools.chain(team_1_cavalry, team_1_infantry, team_1_elephants)

The second one is probably the simplest—as long as you're aware that you're making an unnecessary big temporary list, and that this list will always be small enough that there are no performance implications to doing so. 第二个可能是最简单的-只要您知道要制作一个不必要的大临时列表,并且此列表将始终足够小,以至于这样做不会影响性能。


But if you're never actually going to need the three separate lists at all, why even create them? 但是,如果您实际上根本不需要三个独立的列表,为什么还要创建它们呢? It might be simpler if you just do this: 如果您这样做,可能会更简单:

team_1 = [(Unit('Cavalry', i+20, 10, 'A', libtcod.yellow, 6, 1, 1000, 100, 1.5, 500, 1)) for i in range(11) ]
team_1 += [(Unit('Heavy Infantry', i+20, 9, '=', libtcod.yellow, 3, 1, 2000, 100, 1.0, 200, 1)) for i in range(11)]
team_1 += [(Unit('War Elephants', 20, 8, 'R', libtcod.yellow, 5, 1, 37, 100, 100, 800, 1)) for i in range(2)]

Now, team_1 is just all the units for team 1, of all three kinds. 现在, team_1只是这三种团队1的所有单位。

In: 在:

teams = [team1, team2]

You are building a List of Lists (team1 and team2 are lists). 您正在构建列表列表(team1和team2是列表)。

If you want to create one single list out of 2: 如果要从2中创建一个列表:

teams = team1 + team2

As you said, you should really consider using classes. 如您所说,您应该真正考虑使用类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM