如何向类中添加其他属性?

时间:2016-12-29 16:00:08

标签: python python-3.x pygame

如何在程序运行时向列表中添加其他locationx和locationy变量?

班级示例:

class Building:
    'Common base class for all buildings'

    def __init__(self, name, hp, img, locationx, locationy, height, width):
        self.name = name
        self.hp = hp
        self.img = img
        self.locationx = locationx
        self.locationy = locationy
        self.height = height
        self.width = width

building_shipyard = Building("Shipyard",500,shipyard,[1000,2500,5000],[1000,2500,5000],300,300)

例如,在用户放置新建筑物之后,我可能希望将self.locationx和self.locationy从[1000,2500,5000]更新为[1000,2500,5000,7000]。

1 个答案:

答案 0 :(得分:0)

只需访问这些属性:

...
def place_building ():
    global building_shipyard 
    ...
    building_shipyard.locationx =  [1000,2500,5000,7000]
    # or
    building_shipyard.locationx.append(7000)